fix: guard coroutine scopes against crashes#1094
Open
jvsena42 wants to merge 4 commits into
Open
Conversation
ovitrif
previously approved these changes
Jul 18, 2026
ovitrif
left a comment
Collaborator
There was a problem hiding this comment.
did an early untested review. LGTM 🫡
jvsena42
marked this pull request as ready for review
July 20, 2026 17:46
Greptile SummaryThis PR adds shared crash handling for long-lived coroutine scopes. The main changes are:
Confidence Score: 4/5Long-lived background observers can stop permanently after a handled exception, so that path needs a fix before merging.
app/src/main/java/to/bitkit/async/CoroutineScopes.kt, app/src/main/java/to/bitkit/repositories/LightningRepo.kt, and app/src/main/java/to/bitkit/App.kt
|
| Filename | Overview |
|---|---|
| app/src/main/java/to/bitkit/async/CoroutineScopes.kt | Adds shared supervised scopes, but failed long-lived child jobs are logged without being restarted. |
| app/src/main/java/to/bitkit/repositories/LightningRepo.kt | Moves long-lived observers and retry work to the shared handler, exposing permanent child-job termination after an error. |
| app/src/main/java/to/bitkit/App.kt | Installs the global logger before logger storage initialization, leaving an early-startup diagnostic gap. |
| app/src/main/java/to/bitkit/async/ServiceQueue.kt | Preserves coroutine cancellation while continuing to wrap ordinary failures in AppError. |
| app/src/main/java/to/bitkit/async/BaseCoroutineScope.kt | Adds tagged exception handling to the existing supervised base scope. |
Reviews (1): Last reviewed commit: "Merge branch 'master' into fix/harden-co..." | Re-trigger Greptile
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.
Relates to #1093 , #986
This PR:
Description
A recent crash came from an uncaught error escaping a background coroutine. None of the app's long-lived scopes had an exception handler, so any unhandled throw in a background task reached the system's default handler and killed the process — often in a restart loop.
This introduces a single helper used across the repositories, services, and background workers so that every long-lived scope shares the same behavior: an uncaught error is logged with its owning tag and the scope stays alive, rather than taking the app down. The same handler is applied to the shared base scope used by the Lightning service and the keychain.
Background queue work now preserves coroutine cancellation instead of wrapping it into a generic error, so structured cancellation keeps working as intended.
Finally, a global uncaught-exception logger is installed at startup that records anything still slipping through before delegating to the platform's default handler, so normal crash behavior is preserved while we gain a log trail.
This is the safety net for the broader hardening tracked in #1093. Guarding the specific background bodies so a failure is handled meaningfully instead of only logged remains a follow-up.
Preview
N/A — no UI changes.
QA Notes
Manual Tests
regression:Cold start with an existing wallet → let startup finish: node starts, balances and widgets load, app does not crash.regression:Background the app during sync, then foreground: sync resumes, no crash.Automated Checks
CoroutineScopesTest.ktasserts a scope keeps running after an uncaught child exception (verified failing without the handler, passing with it).ServiceQueueTest.ktasserts background work rethrows cancellation unchanged and wraps other failures in AppError.Runtime demonstration (crash simulation)
Verified the two runtime guards on a device (
to.bitkit.dev, dev/regtest) bytemporarily injecting uncaught crashes (not committed — reverted after each
run), building & installing the dev build, and driving the app with the journeys
below via the
androidCLI. Each case shows the injected diff, the journey, andthe resulting logcat + persisted device log (
files/logs/…).Case A — background coroutine crash is contained (scope handler)
A child launched in a long-lived
appScopethrows; the app must stay aliveand the throw must be logged with the owning tag.
Injected diff (reverted):
Journey:
Result: PASSED — home screen rendered fully, process stayed alive (pid 9270).
logcat + persisted log (
files/logs/bitkit_2026-07-20_17-56-47.log):The throw is logged under the owning tag (
WalletRepo); startup proceeds and theUI loads — the scope survives instead of crashing the process.
Case B — startup uncaught crash is recorded then delegated (backstop / P2)
An uncaught throw on a background thread during early startup must be written to
the log file (the P2 fix) and then follow normal crash behavior.
Injected diff (reverted), on top of the
5949a023creorder:Journey:
Result: PASSED — backstop logged the crash, then the default handler killed
the process; the entry is present in the session log file.
logcat:
Persisted log (
files/logs/bitkit_2026-07-20_18-00-48.log) — the P2 proof:The
App storage path: …line (frominitAppStoragePath) is written beforethe crash entry, confirming the reorder: the storage path — and therefore the
Logger delegate's session file — is initialized before the backstop fires, so the
early-startup crash is captured on disk instead of dropped.