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

Enable the thread police 👮 for debug builds #1050

Merged
merged 3 commits into from
Dec 11, 2024
Merged

Conversation

keyboardsurfer
Copy link
Member

What I have done and why

App will now crash if someone does funky stuff on the main thread for debug builds.
This enforces programming thread safe programming (even though there are no obvious violations at the moment).
g
Do tests pass?

  • Run local tests on DemoDebug variant: ./gradlew testDemoDebug
  • Check formatting: ./gradlew --init-script gradle/init.gradle.kts spotlessApply

Is this your first pull request?

@lihenggui
Copy link
Contributor

After enabling the StrictMode, the application will crash when launched.
Looks like there are some tasks remaining that need to be completed.

Process: com.google.samples.apps.nowinandroid.demo.debug, PID: 5363
java.lang.RuntimeException: StrictMode ThreadPolicy violation
	at android.os.StrictMode$AndroidBlockGuardPolicy.onThreadPolicyViolation(StrictMode.java:1883)
	at android.os.StrictMode$AndroidBlockGuardPolicy.lambda$handleViolationWithTimingAttempt$0(StrictMode.java:1797)
	at android.os.StrictMode$AndroidBlockGuardPolicy.$r8$lambda$AF9hglpXLeeg2_1ZxHCfZb2exEY(Unknown Source:0)
	at android.os.StrictMode$AndroidBlockGuardPolicy$$ExternalSyntheticLambda1.run(Unknown Source:6)
	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:8176)
	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:971)
Caused by: android.os.strictmode.DiskReadViolation
	at android.os.StrictMode$AndroidBlockGuardPolicy.onReadFromDisk(StrictMode.java:1666)
	at libcore.io.BlockGuardOs.access(BlockGuardOs.java:74)
	at libcore.io.ForwardingOs.access(ForwardingOs.java:128)
	at android.app.ActivityThread$AndroidOs.access(ActivityThread.java:8053)
	at java.io.UnixFileSystem.checkAccess(UnixFileSystem.java:313)
	at java.io.File.exists(File.java:813)
	at android.app.ContextImpl.ensurePrivateDirExists(ContextImpl.java:790)
	at android.app.ContextImpl.ensurePrivateDirExists(ContextImpl.java:781)
	at android.app.ContextImpl.getPreferencesDir(ContextImpl.java:737)
	at android.app.ContextImpl.getSharedPreferencesPath(ContextImpl.java:962)
	at android.app.ContextImpl.getSharedPreferences(ContextImpl.java:583)
	at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:221)
	at com.google.firebase.remoteconfig.RemoteConfigComponent.getMetadataClient(RemoteConfigComponent.java:299)
	at com.google.firebase.remoteconfig.RemoteConfigComponent.get(RemoteConfigComponent.java:162)
	at com.google.firebase.perf.config.RemoteConfigManager.isFirebaseRemoteConfigAvailable(RemoteConfigManager.java:395)
	at com.google.firebase.perf.config.RemoteConfigManager.triggerRemoteConfigFetchIfNecessary(RemoteConfigManager.java:331)
	at com.google.firebase.perf.config.RemoteConfigManager.getRemoteConfigValue(RemoteConfigManager.java:295)
	at com.google.firebase.perf.config.RemoteConfigManager.getBoolean(RemoteConfigManager.java:207)
	at com.google.firebase.perf.config.ConfigResolver.getRemoteConfigBoolean(ConfigResolver.java:867)
	at com.google.firebase.perf.config.ConfigResolver.getIsExperimentTTIDEnabled(ConfigResolver.java:826)
	at com.google.firebase.perf.metrics.AppStartTrace.onActivityResumed(AppStartTrace.java:352)
	at android.app.Application.dispatchActivityResumed(Application.java:431)
	at android.app.Activity.dispatchActivityResumed(Activity.java:1483)
	at android.app.Activity.onResume(Activity.java:2065)
	at com.google.samples.apps.nowinandroid.MainActivity.onResume(MainActivity.kt:157)
	at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1603)
	at android.app.Activity.performResume(Activity.java:8743)
	at android.app.ActivityThread.performResumeActivity(ActivityThread.java:4951)
	at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:4994)
	at android.app.servertransaction.ResumeActivityItem.execute(ResumeActivityItem.java:57)
	at android.app.servertransaction.ActivityTransactionItem.execute(ActivityTransactionItem.java:45)
	at android.app.servertransaction.TransactionExecutor.executeLifecycleState(TransactionExecutor.java:180)
	at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:98)
	at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2443)
	at android.os.Handler.dispatchMessage(Handler.java:106)

@SimonMarquis
Copy link
Contributor

Quick question: should we also enable strict VmPolicy?

@keyboardsurfer keyboardsurfer force-pushed the bw/threadPolicy branch 2 times, most recently from b4e077c to d45ad54 Compare November 21, 2023 10:26
@keyboardsurfer
Copy link
Member Author

The RemoteConfig issue is being tracked here: firebase/firebase-android-sdk#5347 and I'm following up with the team.

@keyboardsurfer keyboardsurfer force-pushed the bw/threadPolicy branch 3 times, most recently from 6812237 to 7af51af Compare November 27, 2023 20:29
@keyboardsurfer keyboardsurfer changed the title Enable the thread police 👮 for debug builds DO NOT MERGE: WILL BREAK RUNTIME. See conversation. Enable the thread police 👮 for debug builds Nov 28, 2023
Copy link

github-actions bot commented Sep 5, 2024

Combined test coverage report

Overall Project 42.32% -0.03% 🍏
Files changed 0%

Module Coverage
app 48.05% -0.24%
Files
Module File Coverage
app NiaApplication.kt 0% -27.27%

* App will now crash if someone does funky stuff on the main thread.

Change-Id: I9026c100705f2fec6963a1d888b40906186f9d28
@keyboardsurfer keyboardsurfer changed the title DO NOT MERGE: WILL BREAK RUNTIME. See conversation. Enable the thread police 👮 for debug builds Enable the thread police 👮 for debug builds Dec 11, 2024
@keyboardsurfer
Copy link
Member Author

This PR does not break runtime any longer and can be accepted after positive review.

Copy link
Collaborator

@dturner dturner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor refactoring required

keyboardsurfer and others added 2 commits December 11, 2024 13:41
Change-Id: If170b2b05859ebfca7bc91ccc790be5b43a1b772
@keyboardsurfer
Copy link
Member Author

All requested changes addressed

@keyboardsurfer keyboardsurfer requested review from dturner and jdkoren and removed request for jdkoren December 11, 2024 12:46
Copy link
Collaborator

@dturner dturner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's.....beautiful.... 😍

@dturner dturner merged commit 0134248 into main Dec 11, 2024
3 checks passed
@dturner dturner deleted the bw/threadPolicy branch December 11, 2024 13:28
@SimonMarquis
Copy link
Contributor

FYI: the issue firebase/firebase-android-sdk#5347 has been closed and "locked and limited conversation to collaborators" while it seems to not be entirely fixed:

@keyboardsurfer any idea what was fixed exactly on their side?

(I'm not able to reproduce the issue, though I don't have a similar device)

@keyboardsurfer
Copy link
Member Author

I have no insights to share but the last comment on the issue shows where we can expect this to work firebase/firebase-android-sdk#5347 (comment)

@klemensz
Copy link

The crash still happens with the latest main branch. I also tried it with the latest Firebase BOM 33.10.0.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants