-
Notifications
You must be signed in to change notification settings - Fork 7
fix(signaling): resolve ForegroundServiceDidNotStartInTimeException on Xiaomi #1099
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
a1b410b
chore(signaling): umbrella branch for Xiaomi FGS crash fixes
SERDUN 95acf7e
fix(signaling): call startForeground() first in onCreate() to reduce …
SERDUN 8f57a38
fix(signaling): parallelize Pigeon IPC calls in _startService() to re…
SERDUN f4fb2a6
fix(signaling): persistent mode FGS recovery after OS kill (#1103)
SERDUN 8a31663
fix: push isolate reuses FGS hub WebSocket (1-socket invariant) (#1104)
SERDUN File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
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
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
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
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
81 changes: 81 additions & 0 deletions
81
...e_android/android/src/main/kotlin/com/webtrit/signaling_service/SignalingRestartWorker.kt
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| package com.webtrit.signaling_service | ||
|
|
||
| import android.content.Context | ||
| import android.os.Build | ||
| import android.util.Log | ||
| import androidx.work.ExistingWorkPolicy | ||
| import androidx.work.OneTimeWorkRequestBuilder | ||
| import androidx.work.WorkManager | ||
| import androidx.work.Worker | ||
| import androidx.work.WorkerParameters | ||
| import java.util.concurrent.TimeUnit | ||
|
|
||
| /// WorkManager worker that restarts [SignalingForegroundService] after it is killed. | ||
| /// | ||
| /// Enqueued by [SignalingForegroundService.onDestroy] (15 s delay) and | ||
| /// [SignalingForegroundService.onTaskRemoved] (1 s delay) when in persistent mode. | ||
| /// The [ExistingWorkPolicy.REPLACE] policy resets the timer if both triggers fire | ||
| /// close together, preventing duplicate restarts. | ||
| /// | ||
| /// On Android 12+ (API 31+), [startForegroundService] can throw | ||
| /// [ForegroundServiceStartNotAllowedException] when the process has left the BFGS | ||
| /// window. This is a transient condition — [Result.retry] is returned so WorkManager | ||
| /// retries with exponential back-off until the process re-enters foreground. | ||
| /// All other exceptions indicate permanent failures and return [Result.failure] | ||
| /// to avoid unbounded retry loops. | ||
| class SignalingRestartWorker( | ||
| context: Context, | ||
| workerParams: WorkerParameters, | ||
| ) : Worker(context, workerParams) { | ||
|
|
||
| override fun doWork(): Result = try { | ||
| if (!SignalingForegroundService.isRunning && | ||
| !StorageDelegate.isPushBound(applicationContext) && | ||
| StorageDelegate.getCoreUrl(applicationContext).isNotEmpty() && | ||
| StorageDelegate.getTenantId(applicationContext).isNotEmpty() && | ||
| StorageDelegate.getToken(applicationContext).isNotEmpty() && | ||
| StorageDelegate.getCallbackDispatcher(applicationContext) != 0L | ||
| ) { | ||
| Log.w(TAG, "SignalingRestartWorker: restarting persistent FGS") | ||
| SignalingForegroundService.start(applicationContext) | ||
| } | ||
| Result.success() | ||
| } catch (e: Exception) { | ||
| // ForegroundServiceStartNotAllowedException is expected on Android 12+ when the process | ||
| // has left the BFGS window. Log at warning level (transient) and schedule a retry. | ||
| // All other exceptions are permanent failures -- log at error level and stop retrying. | ||
| if (isForegroundServiceStartNotAllowed(e)) { | ||
| Log.w(TAG, "Cannot restart FGS: process not in BFGS state, will retry", e) | ||
| Result.retry() | ||
| } else { | ||
| Log.e(TAG, "Failed to restart FGS (permanent)", e) | ||
| Result.failure() | ||
| } | ||
| } | ||
|
|
||
| companion object { | ||
| private const val TAG = "SignalingRestartWorker" | ||
| private const val WORK_TAG = "signaling_fgs_restart" | ||
|
|
||
| fun enqueue(context: Context, delayMillis: Long = 15_000) { | ||
| val request = OneTimeWorkRequestBuilder<SignalingRestartWorker>() | ||
| .addTag(WORK_TAG) | ||
| .setInitialDelay(delayMillis, TimeUnit.MILLISECONDS) | ||
| .build() | ||
| WorkManager.getInstance(context) | ||
| .enqueueUniqueWork(WORK_TAG, ExistingWorkPolicy.REPLACE, request) | ||
| } | ||
|
|
||
| fun remove(context: Context) { | ||
| WorkManager.getInstance(context).cancelUniqueWork(WORK_TAG) | ||
| } | ||
|
|
||
|
SERDUN marked this conversation as resolved.
|
||
| @Suppress("NewApi") | ||
| @androidx.annotation.RequiresApi(Build.VERSION_CODES.S) | ||
| private fun isFgsNotAllowed(e: Exception) = | ||
| e is android.app.ForegroundServiceStartNotAllowedException | ||
|
|
||
| private fun isForegroundServiceStartNotAllowed(e: Exception) = | ||
| Build.VERSION.SDK_INT >= Build.VERSION_CODES.S && isFgsNotAllowed(e) | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.