fix-Android: foregroundTaskOptions UninitializedPropertyAccessException in onDestroy#375
Open
alexsvtmdev wants to merge 1 commit intoDev-hwang:masterfrom
Conversation
…on in onDestroy When Android destroys ForegroundService before onStartCommand() is called (e.g. process killed under memory pressure, aggressive battery optimization, or quick swipe from recents on some OEMs), the lateinit property `foregroundTaskOptions` is never initialized via `loadDataFromPreferences()`. Accessing `foregroundTaskOptions.allowAutoRestart` in `onDestroy()` then throws UninitializedPropertyAccessException, crashing the app. This is the same pattern that was already fixed for `foregroundServiceStatus` in commit 67da414. This commit applies the identical `::isInitialized` guard to `foregroundTaskOptions`. Crashlytics stack trace: Fatal Exception: java.lang.RuntimeException: Unable to stop service ...ForegroundService: lateinit property foregroundTaskOptions has not been initialized at ForegroundService.onDestroy(ForegroundService.kt:207) Co-authored-by: Cursor <cursoragent@cursor.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ForegroundService.onDestroy()crashes withUninitializedPropertyAccessExceptionwhen Android destroys the service beforeonStartCommand()is called.Root cause: The
lateinit var foregroundTaskOptionsis only initialized inloadDataFromPreferences()(called fromonStartCommand()). If the system destroys the service before that call,onDestroy()accesses the uninitialized property and crashes.Fix: Added
::foregroundTaskOptions.isInitializedguard — the exact same pattern already used forforegroundServiceStatus(fixed in commit 67da414).When does this happen?
onStartCommand()runsCrashlytics stack trace (production)
Changes
File:
android/.../service/ForegroundService.kt—onDestroy()methodThis follows the same pattern as the existing
foregroundServiceStatusguard in the same method (commit 67da414).Risk assessment
foregroundTaskOptionsis not initialized,allowAutoRestartdefaults tofalse(no auto-restart), which is the correct behavior when the service was never properly startedonDestroy)