Skip to content

fix: guard coroutine scopes against crashes#1094

Draft
jvsena42 wants to merge 2 commits into
masterfrom
fix/harden-coroutine-error-handling
Draft

fix: guard coroutine scopes against crashes#1094
jvsena42 wants to merge 2 commits into
masterfrom
fix/harden-coroutine-error-handling

Conversation

@jvsena42

@jvsena42 jvsena42 commented Jul 17, 2026

Copy link
Copy Markdown
Member

Relates to #1093 , #986

This PR:

  1. Adds a shared exception-handling convention so every long-lived background scope logs uncaught errors instead of crashing the app
  2. Fixes background task cancellation being reclassified as a generic error
  3. Adds a process-level uncaught-exception logger as a last-resort backstop

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

  • 1. regression: Cold start with an existing wallet → let startup finish: node starts, balances and widgets load, app does not crash.
  • 2. regression: Background the app during sync, then foreground: sync resumes, no crash.

Automated Checks

  • Unit tests added: CoroutineScopesTest.kt asserts a scope keeps running after an uncaught child exception (verified failing without the handler, passing with it).
  • Unit tests added: ServiceQueueTest.kt asserts background work rethrows cancellation unchanged and wraps other failures in AppError.
  • CI: standard compile, unit test, and detekt checks run by the PR bot.

@jvsena42 jvsena42 self-assigned this Jul 17, 2026

@ovitrif ovitrif left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

did an early untested review. LGTM 🫡

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.

2 participants