Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

StrictMode getSharedPreferences failure on API 27 #1981

Closed
pyricau opened this issue Oct 17, 2020 · 4 comments · Fixed by #1984
Closed

StrictMode getSharedPreferences failure on API 27 #1981

pyricau opened this issue Oct 17, 2020 · 4 comments · Fixed by #1984

Comments

@pyricau
Copy link
Member

pyricau commented Oct 17, 2020

Steps to Reproduce

Just start the app on an API 27 device

Updating the config triggers a check for whether heap dumping is enabled in the UI, which in turns triggers a shared preferences load which apparently triggers a strict mode failure. This is likely a stupid AOSP bug (SharedPref loading used to be fully async when loading and block if necessary of first getter call)

D/LeakCanary: LeakCanary is running and ready to detect memory leaks.
D/StrictMode: StrictMode policy violation; ~duration=7 ms: android.os.StrictMode$StrictModeDiskReadViolation: policy=327743 violation=2
        at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1440)
        at java.io.UnixFileSystem.checkAccess(UnixFileSystem.java:251)
        at java.io.File.exists(File.java:807)
        at android.app.ContextImpl.getDataDir(ContextImpl.java:2197)
        at android.app.ContextImpl.getPreferencesDir(ContextImpl.java:517)
        at android.app.ContextImpl.getSharedPreferencesPath(ContextImpl.java:714)
        at android.app.ContextImpl.getSharedPreferences(ContextImpl.java:368)
        at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:167)
        at leakcanary.internal.activity.screen.AboutScreenKt.getDumpEnabledInAboutScreen(AboutScreen.kt:63)
        at leakcanary.internal.HeapDumpControl.iCanHasHeap(HeapDumpControl.kt:58)
        at leakcanary.internal.InternalLeakCanary$invoke$2.run(InternalLeakCanary.kt:140)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.leakcanary, PID: 29107
    android.os.StrictMode$StrictModeViolation: policy=327743 violation=2
        at android.os.StrictMode.executeDeathPenalty(StrictMode.java:1686)
        at android.os.StrictMode.-wrap3(Unknown Source:0)
        at android.os.StrictMode$AndroidBlockGuardPolicy.handleViolation(StrictMode.java:1679)
        at android.os.StrictMode$AndroidBlockGuardPolicy$1.run(StrictMode.java:1557)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
@pyricau
Copy link
Member Author

pyricau commented Oct 17, 2020

Need to gather more context on why strict mode is failing on this, then figure out possible fixes.

@opatry
Copy link
Contributor

opatry commented Nov 7, 2020

AFAIK, the first time shared prefs are loaded, they are read from file system, so I/O access on UI Thread raises such StrictMode issue. Seems legit to me 🤔

@pyricau pyricau added this to the 2.6 milestone Dec 2, 2020
@pyricau
Copy link
Member Author

pyricau commented Dec 3, 2020

Looks like this is still happening on Android 10, slightly different stacktrace:

    Process: com.example.leakcanary, PID: 11907
    java.lang.RuntimeException: StrictMode ThreadPolicy violation
        at android.os.StrictMode$AndroidBlockGuardPolicy.onThreadPolicyViolation(StrictMode.java:1768)
        at android.os.StrictMode$AndroidBlockGuardPolicy.lambda$handleViolationWithTimingAttempt$0$StrictMode$AndroidBlockGuardPolicy(StrictMode.java:1687)
        at android.os.-$$Lambda$StrictMode$AndroidBlockGuardPolicy$9nBulCQKaMajrWr41SB7f7YRT1I.run(Unknown Source:6)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: android.os.strictmode.DiskReadViolation
        at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1556)
        at android.app.SharedPreferencesImpl.awaitLoadedLocked(SharedPreferencesImpl.java:261)
        at android.app.SharedPreferencesImpl.getBoolean(SharedPreferencesImpl.java:330)
        at leakcanary.internal.InternalLeakCanary.getDumpEnabledInAboutScreen$leakcanary_android_core_debug(InternalLeakCanary.kt:121)
        at leakcanary.internal.HeapDumpControl.iCanHasHeap(HeapDumpControl.kt:57)
        at leakcanary.internal.InternalLeakCanary$invoke$2.run(InternalLeakCanary.kt:161)
        at android.os.Handler.handleCallback(Handler.java:883) 
        at android.os.Handler.dispatchMessage(Handler.java:100) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356)

Basically retrieving it works fine but then reading the values starts blocking and thrown. Grrr

@pyricau pyricau reopened this Dec 3, 2020
@pyricau
Copy link
Member Author

pyricau commented Dec 7, 2020

Fixed by #1995

@pyricau pyricau closed this as completed Dec 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants