coil-kt / coil

Image loading for Android and Compose Multiplatform.
https://coil-kt.github.io/coil/
Apache License 2.0
10.84k stars 665 forks source link

[Coil 3] Android StrictMode causes crashes with DiskReadViolation #2007

Closed siarhei-luskanau closed 9 months ago

siarhei-luskanau commented 10 months ago

Android StrictMode causes crashes with DiskReadViolation Coil version is 3.0.0-alpha01

To Reproduce:

  1. Add the next code to the samples/compose/src/androidMain/kotlin/sample/compose/MainActivity.kt. Or checkout the 3.x_StrictMode branch https://github.com/siarhei-luskanau/coil/tree/3.x_StrictMode
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        StrictMode.setThreadPolicy(
            StrictMode.ThreadPolicy.Builder()
                .detectDiskReads()
                .detectDiskWrites()
                .detectNetwork()
                // .detectAll() for all detectable problems
                .penaltyLog()
                .penaltyDeath()
                .build()
        )
        StrictMode.setVmPolicy(
            StrictMode.VmPolicy.Builder()
                .detectLeakedSqlLiteObjects()
                .detectLeakedClosableObjects()
                .penaltyLog()
                .penaltyDeath()
                .build()
        )
        setContent {
            App(
                viewModel = viewModel<AndroidMainViewModel>(),
                debug = BuildConfig.DEBUG,
            )
        }
    }
  1. Launch the samples.compose application and see the app crash

Stacktrace:

                 FATAL EXCEPTION: main
                 Process: sample.compose, PID: 29839
                 java.lang.RuntimeException: StrictMode ThreadPolicy violation
                    at android.os.StrictMode$AndroidBlockGuardPolicy.onThreadPolicyViolation(StrictMode.java:1878)
                    at android.os.StrictMode$AndroidBlockGuardPolicy.lambda$handleViolationWithTimingAttempt$0(StrictMode.java:1792)
                    at android.os.StrictMode$AndroidBlockGuardPolicy.$r8$lambda$8cEr3yopH2m54_yi5FS7GfNsTW0(Unknown Source:0)
                    at android.os.StrictMode$AndroidBlockGuardPolicy$$ExternalSyntheticLambda1.run(D8$$SyntheticClass:0)
                    at android.os.Handler.handleCallback(Handler.java:958)
                    at android.os.Handler.dispatchMessage(Handler.java:99)
                    at android.os.Looper.loopOnce(Looper.java:205)
                    at android.os.Looper.loop(Looper.java:294)
                    at android.app.ActivityThread.main(ActivityThread.java:8470)
                    at java.lang.reflect.Method.invoke(Native Method)
                    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:878)
                 Caused by: android.os.strictmode.DiskReadViolation
                    at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1661)
                    at libcore.io.BlockGuardOs.access(BlockGuardOs.java:74)
                    at java.io.UnixFileSystem.checkAccess(UnixFileSystem.java:313)
                    at java.io.File.exists(File.java:813)
                    at com.android.server.locales.LocaleManagerService.getOverrideLocaleConfig(LocaleManagerService.java:766)
                    at com.android.server.locales.LocaleManagerService$LocaleManagerBinderService.getOverrideLocaleConfig(LocaleManagerService.java:215)
                    at android.app.ILocaleManager$Stub.onTransact(ILocaleManager.java:175)
                    at android.os.Binder.execTransactInternal(Binder.java:1491)
                    at android.os.Binder.execTransact(Binder.java:1435)

Expected result: No crashes with Android StrictMode

colinrtwhite commented 10 months ago

Does it still occur if you set this to false?

siarhei-luskanau commented 10 months ago

I have tried to add the .addLastModifiedToFileCacheKey(false) in my pet project, but the DiskReadViolation still occur

colinrtwhite commented 10 months ago

@siarhei-luskanau I'm not able to reproduce the strict mode violation in this sample. Can you post the full exception? Strict mode should print the callsite in the logs before the exception posted above.

From your callsite it looks like it's triggered by Android and is unrelated to Coil:

                    at java.io.File.exists(File.java:813)
                    at com.android.server.locales.LocaleManagerService.getOverrideLocaleConfig(LocaleManagerService.java:766)
                    at com.android.server.locales.LocaleManagerService$LocaleManagerBinderService.getOverrideLocaleConfig(LocaleManagerService.java:215)
                    at android.app.ILocaleManager$Stub.onTransact(ILocaleManager.java:175)
colinrtwhite commented 10 months ago

Closing this out since the strict mode warning appears to be caused by LocaleManagerService.

siarhei-luskanau commented 10 months ago

@siarhei-luskanau I'm not able to reproduce the strict mode violation in this sample. Can you post the full exception? Strict mode should print the callsite in the logs before the exception posted above.

From your callsite it looks like it's triggered by Android and is unrelated to Coil:

                  at java.io.File.exists(File.java:813)
                  at com.android.server.locales.LocaleManagerService.getOverrideLocaleConfig(LocaleManagerService.java:766)
                  at com.android.server.locales.LocaleManagerService$LocaleManagerBinderService.getOverrideLocaleConfig(LocaleManagerService.java:215)
                  at android.app.ILocaleManager$Stub.onTransact(ILocaleManager.java:175)

@colinrtwhite My pet project in main branch uses io.github.qdsfdhvh:image-loader image loader library. In that case I have no any issue with Android StrictMode.

In coin branch I have migrated to coil3 image library and faced with Android StrictMode issue.

I see that stacktrace doesn't mentioned the coil3. Anyway I'm sure that Android StrictMode issue appear due adding coil3 image library.

colinrtwhite commented 10 months ago

I think there might be a strict mode failure in the sample related to a leaked closable - not file system access on the main thread. Let's reopen to track.

colinrtwhite commented 9 months ago

Closing as fixed by Compose 1.6.0-alpha01. https://github.com/coil-kt/coil/pull/2093#issue-2111048894