Skip to content

Keep an unlock when Android refuses to foreground the lock service - #2273

Draft
mpretty-cyro wants to merge 1 commit into
session-foundation:devfrom
mpretty-cyro:fix/key-caching-foreground-refused
Draft

mpretty-cyro wants to merge 1 commit into
session-foundation:devfrom
mpretty-cyro:fix/key-caching-foreground-refused

Conversation

@mpretty-cyro

Copy link
Copy Markdown
Collaborator

Contributor checklist

  • I have tested my contribution on these devices:
  • Virtual device: Pixel 6 profile, API 37 (arm64), play flavour, qa
  • My contribution is fully baked and ready to be merged as is
  • I ensure that all the open issues my contribution fixes are mentioned in the commit message of my first commit using the Fixes #1234 syntax

Description

New crash cluster in 1.33.9: ForegroundServiceStartNotAllowedException from KeyCachingService.foregroundService, reached from ScreenLockActivity.handleAuthenticated → setMasterSecret after the device-credential prompt returns (6 users / 6 events).

#2263 stopped onResume crashing when Android refuses to start KeyCachingService because the app still counts as background. The same unlock now carries on to setMasterSecret, whose startForeground Android 12+ also refuses in that state. 1.33.8's onResume cluster (29 users / 30 events) is gone from 1.33.9's top crashes, and this is what's left of that path.

foregroundService now catches the refusal (ForegroundServiceStartNotAllowedException is an IllegalStateException) and logs it. The secret is set before the call, so the unlock goes through. If the service then dies without being foregrounded, onDestroy clears the key and the app asks for the lock again, so it fails locked rather than unlocked.

Confidence: high that it stops the crash.

Testing: a local-only hook (not in this PR) made startForeground throw the way Android refuses it. With app lock on and a device PIN, a cold launch and PIN unlock went through onActivityResult → handleAuthenticated → setMasterSecret, the same path as the crash. The refusal was logged and caught, and the home screen opened with no crash. Turning app lock on, which goes through handleLockToggled, was caught the same way. Without the hook, a cold launch and PIN unlock still foregrounds the service (dumpsys activity services: isForeground=true) and opens the home screen. The real background refusal was not reproduced. Tested on a qa build, because app lock doesn't engage in debug.

Since the lock screen stopped crashing on a refused start in onResume, the
same background unlock now reaches startForeground through setMasterSecret,
which Android 12+ also refuses there. Catch it like the start; the secret is
already set, and an unforegrounded service that dies clears it, so the app
asks for the lock again rather than crashing.

@mpretty-cyro mpretty-cyro left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

.

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.

1 participant