osfans / trime

同文安卓輸入法平臺3.x/Android-rime/Rime Input Method Engine for Android
http://osfans.github.io/trime/
GNU General Public License v3.0
3.09k stars 372 forks source link

Crash on phone restart #1158

Closed Bambooin closed 9 months ago

Bambooin commented 9 months ago

Describe the bug A clear and concise description of what the bug is. Crash on phone restart.

To Reproduce Steps to reproduce the bug:

  1. Restart phone
  2. Unlock the phone
  3. See bug

Expected behavior A clear and concise description of what you expected to happen. No crash on restart.

Log If applicable, add crash log to quick focus your problem.

--------- Device Info
OS Name: lineage_xxx-userdebug 13
OS API Level: 33
Tags: release-keys
Screen Size: 1080 x 2194
Screen Density: 2.75
Screen orientation: Portrait
--------- Build Info
...
--------- Crash stacktrace
java.io.IOException: No such file or directory
    at java.io.UnixFileSystem.createFileExclusively0(Native Method)
    at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:317)
    at java.io.File.createNewFile(File.java:1006)
    at com.osfans.trime.data.DataManager.sync(DataManager.kt:89)
    at com.osfans.trime.core.Rime$Companion.startup(Rime.kt:67)
    at com.osfans.trime.core.Rime$Companion.access$startup(Rime.kt:42)
    at com.osfans.trime.core.Rime.<init>(Rime.kt:39)
    at com.osfans.trime.core.Rime$Companion.getInstance(Rime.kt:47)
    at com.osfans.trime.ime.core.RimeWrapper$startup$2.invokeSuspend(RimeWrapper.kt:40)
    at kotlin.coroutines.jvm.internal.BaseContinuationImpl.resumeWith(ContinuationImpl.kt:33)
    at kotlinx.coroutines.DispatchedTask.run(DispatchedTask.kt:108)
    at kotlinx.coroutines.internal.LimitedDispatcher$Worker.run(LimitedDispatcher.kt:115)
    at kotlinx.coroutines.scheduling.TaskImpl.run(Tasks.kt:103)
    at kotlinx.coroutines.scheduling.CoroutineScheduler.runSafely(CoroutineScheduler.kt:584)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.executeTask(CoroutineScheduler.kt:793)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.runWorker(CoroutineScheduler.kt:697)
    at kotlinx.coroutines.scheduling.CoroutineScheduler$Worker.run(CoroutineScheduler.kt:684)
    Suppressed: kotlinx.coroutines.internal.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@b61b7c8, Dispatchers.IO]

--------- beginning of main
12-23 12:09:12.486 D/CompatibilityChangeReporter( 3349): Compat change id reported: 171979766; UID 10167; state: ENABLED
12-23 12:09:12.655 V/GraphicsEnvironment( 3349): ANGLE Developer option for 'com.osfans.trime' set to: 'default'
12-23 12:09:12.656 V/GraphicsEnvironment( 3349): ANGLE GameManagerService for com.osfans.trime: false
12-23 12:09:12.656 V/GraphicsEnvironment( 3349): Neither updatable production driver nor prerelease driver is supported.
12-23 12:09:12.661 D/NetworkSecurityConfig( 3349): No Network Security Config specified, using platform default
12-23 12:09:12.662 D/NetworkSecurityConfig( 3349): No Network Security Config specified, using platform default
12-23 12:09:12.954 D/WindowTokenClient( 3349): Only apply configuration update to Resources because shouldReportConfigChange is false.
12-23 12:09:12.954 D/WindowTokenClient( 3349): android.window.WindowTokenClient.attachToDisplayArea:118 android.window.WindowContextController.attachToDisplayArea:107 android.window.WindowProviderService.attachBaseContext:208 android.app.Service.attach:915 android.app.ActivityThread.handleCreateService:4484 
12-23 12:09:13.034 D/CompatibilityChangeReporter( 3349): Compat change id reported: 237531167; UID 10167; state: DISABLED
12-23 12:09:13.087 W/Parcel  ( 3349): Expecting binder but got null!
12-23 12:09:13.090 E/FileIOUtils( 3349): create file </rime/opencc/HKVariants.txt> failed.
12-23 12:09:13.094 E/FileIOUtils( 3349): create file </rime/opencc/HKVariantsRev.txt> failed.
12-23 12:09:13.099 D/WindowTokenClient( 3349): Configuration not dispatch to IME because configuration is up to date. Current config={1.0 460mcc11mnc [en_US] ldltr sw393dp w393dp h798dp 440dpi nrml long port finger -keyb/v/h -nav/h winConfig={ mBounds=Rect(0, 0 - 1080, 2340) mAppBounds=Rect(0, 80 - 1080, 2274) mMaxBounds=Rect(0, 0 - 1080, 2340) mDisplayRotation=ROTATION_0 mWindowingMode=fullscreen mDisplayWindowingMode=fullscreen mActivityType=undefined mAlwaysOnTop=undefined mRotation=ROTATION_0} as.3 s.54 fontWeightAdjustment=0}, reported config={1.0 460mcc11mnc [en_US] ldltr sw393dp w393dp h798dp 440dpi nrml long port finger -keyb/v/h -nav/h winConfig={ mBounds=Rect(0, 0 - 1080, 2340) mAppBounds=Rect(0, 80 - 1080, 2274) mMaxBounds=Rect(0, 0 - 1080, 2340) mDisplayRotation=ROTATION_0 mWindowingMode=fullscreen mDisplayWindowingMode=fullscreen mActivityType=undefined mAlwaysOnTop=undefined mRotation=ROTATION_0} as.3 s.54 fontWeightAdjustment=0}, updated config={1.0 460mcc11mnc [en_US] ldltr sw393dp w393dp h798dp 440dpi nrml long port finger -keyb/v/h -nav/h winConfig={ mBounds=Rect(0, 0 - 1080, 2340) mAppBounds=Rect(0, 80 - 1080, 2274) mMaxBounds=Rect(0, 0 - 1080, 2340) mDisplayRotation=ROTATION_0 mWindowingMode=fullscreen mDisplayWindowingMode=fullscreen mActivityType=undefined mAlwaysOnTop=undefined mRotation=ROTATION_0} as.3 s.54 fontWeightAdjustment=0}
12-23 12:09:13.102 E/FileIOUtils( 3349): create file </rime/opencc/HKVariantsRevPhrases.txt> failed.
12-23 12:09:13.105 E/FileIOUtils( 3349): create file </rime/opencc/JPShinjitaiCharacters.txt> failed.
12-23 12:09:13.107 E/FileIOUtils( 3349): create file </rime/opencc/JPShinjitaiPhrases.txt> failed.
12-23 12:09:13.116 E/FileIOUtils( 3349): create file </rime/opencc/JPVariants.txt> failed.
12-23 12:09:13.120 E/FileIOUtils( 3349): create file </rime/opencc/STCharacters.txt> failed.
12-23 12:09:13.124 E/FileIOUtils( 3349): create file </rime/opencc/STPhrases.txt> failed.
12-23 12:09:13.129 E/FileIOUtils( 3349): create file </rime/opencc/TSCharacters.txt> failed.
12-23 12:09:13.132 E/FileIOUtils( 3349): create file </rime/opencc/TSPhrases.txt> failed.
12-23 12:09:13.136 E/FileIOUtils( 3349): create file </rime/opencc/TWPhrases.txt> failed.
12-23 12:09:13.139 E/FileIOUtils( 3349): create file </rime/opencc/TWPhrasesRev.txt> failed.
12-23 12:09:13.143 E/FileIOUtils( 3349): create file </rime/opencc/TWVariants.txt> failed.
12-23 12:09:13.147 E/FileIOUtils( 3349): create file </rime/opencc/TWVariantsRev.txt> failed.
12-23 12:09:13.150 E/FileIOUtils( 3349): create file </rime/opencc/TWVariantsRevPhrases.txt> failed.
12-23 12:09:13.155 E/FileIOUtils( 3349): create file </rime/opencc/hk2s.json> failed.
12-23 12:09:13.159 E/FileIOUtils( 3349): create file </rime/opencc/hk2t.json> failed.
12-23 12:09:13.163 E/FileIOUtils( 3349): create file </rime/opencc/jp2t.json> failed.
12-23 12:09:13.166 E/FileIOUtils( 3349): create file </rime/opencc/s2hk.json> failed.
12-23 12:09:13.169 E/FileIOUtils( 3349): create file </rime/opencc/s2t.json> failed.
12-23 12:09:13.171 E/FileIOUtils( 3349): create file </rime/opencc/s2tw.json> failed.
12-23 12:09:13.174 E/FileIOUtils( 3349): create file </rime/opencc/s2twp.json> failed.
12-23 12:09:13.177 E/FileIOUtils( 3349): create file </rime/opencc/t2hk.json> failed.
12-23 12:09:13.180 E/FileIOUtils( 3349): create file </rime/opencc/t2jp.json> failed.
12-23 12:09:13.183 E/FileIOUtils( 3349): create file </rime/opencc/t2s.json> failed.
12-23 12:09:13.186 E/FileIOUtils( 3349): create file </rime/opencc/t2tw.json> failed.
12-23 12:09:13.189 E/FileIOUtils( 3349): create file </rime/opencc/tw2s.json> failed.
12-23 12:09:13.191 E/FileIOUtils( 3349): create file </rime/opencc/tw2sp.json> failed.
12-23 12:09:13.194 E/FileIOUtils( 3349): create file </rime/opencc/tw2t.json> failed.
12-23 12:09:13.198 E/FileIOUtils( 3349): create file </rime/tongwenfeng.trime.yaml> failed.
12-23 12:09:13.203 E/FileIOUtils( 3349): create file </rime/trime.yaml> failed.
12-23 12:09:13.293 I/om.osfans.trime( 3349): System.exit called, status: 10
12-23 12:09:13.293 I/AndroidRuntime( 3349): VM exiting with result code 10, cleanup skipped.

Screenshots If applicable, add screenshots to help explain your problem.

Smartphone (please complete the following information):

Bambooin commented 9 months ago

Found the root cause of this crash: the media is not mounted when setup Trime in another thread.

    /**
     * Return the path of /storage/emulated/0.
     *
     * @return the path of /storage/emulated/0
     */
    public static String getExternalStoragePath() {
>>>     if (!UtilsBridge.isSDCardEnableByEnvironment()) return "";
        return getAbsolutePath(Environment.getExternalStorageDirectory());
    }

Steps to triage:

// with the this log in DataManager.kt
        Timber.i(
            """
                profile: ${prefs.profile}
        user data path: ${userDataDir.absolutePath}
        shared data path: ${sharedDataDir.absolutePath}
        default custom data path: ${customDefault.absolutePath}
        """,
        )

// media mounted
12-23 16:19:59.247 I/[DefaultDispatcher-worker-1](23339):                 profile: com.osfans.trime.data.AppPrefs$Profile@1cf583e
12-23 16:19:59.247 I/[DefaultDispatcher-worker-1](23339):         user data path: /storage/emulated/0/rime
12-23 16:19:59.247 I/[DefaultDispatcher-worker-1](23339):         shared data path: /storage/emulated/0/rime
12-23 16:19:59.247 I/[DefaultDispatcher-worker-1](23339):         default custom data path: /storage/emulated/0/rime/default.custom.yaml

// media not mounted
12-23 16:23:47.046 I/[DefaultDispatcher-worker-2]( 3134):                 profile: com.osfans.trime.data.AppPrefs$Profile@a5e5e60
12-23 16:23:47.046 I/[DefaultDispatcher-worker-2]( 3134):         user data path: /rime
12-23 16:23:47.046 I/[DefaultDispatcher-worker-2]( 3134):         shared data path: /rime
12-23 16:23:47.046 I/[DefaultDispatcher-worker-2]( 3134):         default custom data path: /rime/default.custom.yaml
goofyz commented 9 months ago

Strange. Does it mean that it requires some time before storage is mounted? Or need to mount manually?

goofyz commented 9 months ago

I think that I find out where the problem lies. In DataManager, the sharedDataDir and userDataDir are instantiated when the App first starts. Without calling DataChangeListener.Listener.onDataDirectoryChange(), they won't be updated even if you choose other directories in "Preference".

In your case, the sharedDataDir will be /rime at the phone start, and will remain at /rime even after the media is mounted. The fix will be either call onDataDirectoryChange() when media mounted is detected, or change the implementation in DataManager so it will always return the latest directory.