Skip to content

fix: return errors on rust panics instead of aborting - #145

Merged
ovitrif merged 13 commits into
mainfrom
fix/unwind-panics
Nov 29, 2026
Merged

ovitrif merged 13 commits into
mainfrom
fix/unwind-panics

Conversation

@ovitrif

@ovitrif ovitrif commented Nov 29, 2026 •

Copy link
Copy Markdown
Collaborator

Refs:

Description

  • Switches the mobile profile release-smaller from panic = 'abort' to panic = 'unwind', so a Rust panic comes back as an error from the UniFFI call instead of aborting the app with SIGABRT, as bitkit-core already does. release and dev keep abort; no mobile build uses them.
  • Adds a fail-stop for nodes on any panic in the library, because unwind alone would leave a node that reports is_running with nothing processing or persisting. A panic hook records the panic, then each node built before it:
    • stops its background processor without the final ChannelManager persist
    • cancels its tasks and disconnects its peers
    • returns PersistenceFailed from start until a new Node is built
  • Refuses close_channel, force_close_channel, splice_in, splice_out, update_channel_config, event_handled, claim_for_hash and fail_for_hash with PersistenceFailed once a panic occurred, so a stopped node cannot write channel state
  • Returns PersistenceFailed from stop when a panic arrives while it waits for the background processor, because the final persist was skipped
  • Adds a check that fails CI if a mobile build goes back to aborting on panic.

Risk of continuing after a panic. LDK aborts on purpose when persistence fails unrecoverably. Here the node writes nothing more from the processor, so disk holds what an abort would have left, apart from the moment before the guard runs, and the next Node takes LDK's normal restart-after-crash path. LDK also poisons its monitors lock in that case, so later calls error.

Residual risk. Other threads run briefly between the panic and the guard. The hook is process-wide, so a panic in a leftover task of a released node also stops a newer node.

What the apps do when a node stops itself today

  • Android:
    • ServiceQueue turns the panicking call into AppError; direct getters (status, nodeId, channels, peers, balances) are outside it and can throw InternalException.
    • Nothing watches for a node that stopped itself: the lifecycle stays Running and calls fail with NotRunning or PersistenceFailed until the app stops and starts the node, which builds a new Node.
  • iOS:
    • The next state sync marks the node stopped, and the app restarts it when the network returns.
    • Non-throwing ldk-node calls still crash through try!, as today.

Cost on arm64-v8a: the stripped library grows from 14.1 MB to 16.9 MB (+20%).

Out of Scope

QA Notes

Manual Tests

  • N/A

Automated Checks

  • added panic_test.rs, covering the refused calls and a panic during stop, and a unit test in panic.rs
  • added check_panic_unwind.py and the panic-unwind.yml workflow
  • ran the library unit tests and panic_test.rs locally
  • built release-smaller for Android arm64-v8a and aarch64-apple-ios: panic_abort is linked only in the abort build
  • ran the suite at 04873ce on this branch and on main, with a trimmed copy of rust.yml because main stops at its format and warning checks. The checks added after 04873ce act only after a panic and are covered by panic_test.rs, run locally:
    • this branch with UniFFI on Linux and macOS, and without UniFFI on macOS: only reorg_test fails
    • this branch without UniFFI on Linux: did not finish in two attempts, both cancelled at 75 minutes with derived_accounts::test_electrum_derived_account_incremental_sync_rolls_lookahead still running; that test passed in an earlier run of this branch and on main. cpfp::test_cpfp_rejects_unknown_transaction failed in the first attempt and passed in the second
    • main: reorg_test fails the same assertion, and lsps2_client_service_integration and one multi_address_types_tests.rs test also fail

Models used

  • Planning/scoping: Claude Opus 5.5 (reasoning: medium)
  • Implementation: Claude Opus 5.5 (reasoning: medium)
  • Review: Gemini 4 Neon (reasoning: high)
  • Review rounds: 8

Mobile builds of release-smaller now unwind, so UniFFI turns a panic into an error. A panic hook then stops every node built before it: no further event processing or ChannelManager persistence, no background tasks, no peers.

Verified: cargo test --features uniffi --lib and --test panic_test; cargo check with and without uniffi
Verified: python3 scripts/check_panic_unwind.py; actionlint
stop() and Drop survive locks a panic poisoned, and a panic during start() is caught by the guard.

Verified: cargo test --features uniffi --lib and --test panic_test; cargo check with and without uniffi
Verified: python3 scripts/check_panic_unwind.py
A variable `--profile`, a profile missing from `Cargo.toml`, an `inherits`
cycle and an included Cargo config now fail the check. The job gets a timeout
and keeps no token on disk.

Verified: panic check on the clean tree and with each new case added, actionlint
A quoted or escaped `panic` setting, a quoted `--target` and a mobile build
with no profile flag now fail the check, and shell comments are ignored.

Verified: panic check on the clean tree and with each new case added
Quoted strings are skipped when comments are dropped, and a quoted
`--release` is read.

Verified: panic check on the clean tree and with each new case added
Dropping them could hide a mobile build. Only full-line comments are skipped
again, and a `--release` next to a redirect is read.

Verified: panic check on the clean tree and with each case added
A flag after a `#` no longer counts, and a comment line ending in a backslash
no longer swallows the build after it.

Verified: panic check on the clean tree and with each case added
@ovitrif ovitrif self-assigned this Nov 29, 2026
@ovitrif
ovitrif requested review from a team, coreyphillips and piotr-iohk and removed request for a team November 29, 2026 04:16
@ovitrif

ovitrif commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator Author

Cancelled a full-suite run of this branch: two Linux jobs hung in multi_address_types_tests.rs. The runs linked in the description replace it.

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 29, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-29T04:20:43.297617Z c4b39c2 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: c4b39c297b

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread src/lib.rs Resolved

@talosmachina talosmachina left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Switches the mobile release-smaller profile to panic = 'unwind' and adds a process-wide panic hook that fail-stops every node built before a panic: the background processor is aborted without its final persist, tasks are cancelled, peers are disconnected, and start returns PersistenceFailed until a new Node is built.

No findings. Reviewed at c4b39c2, full tier, static only.

Build skipped: the PR's code was not built or run here (my run gate could not confirm the author's write access through this token and fails closed). The Rust suite (rust.yml) runs only on workflow_dispatch, so on this head CI ran only mobile-builds-unwind (green). The author's linked branch runs are the only execution evidence.

Merge confidence: 4/5, held at 4 because neither I nor CI on this head ran cargo test --features uniffi or panic_test.rs.

What I checked
  • Lifecycle ordering: the guard is spawned first in start_services with the new generation, and stop bumps the generation before tearing down, so a guard firing during stop returns instead of racing the shutdown. The guard sits in the cancellable task set, so stop aborts it.
  • Startup panic window: a panic before the processor exists is covered by the post-spawn panic::count() check that aborts the processor.
  • stop after a guard fail-stop: the processor is aborted, not signalled, so the final ChannelManager persist is skipped. The cancelled join maps to PersistenceFailed, the VSS live mark is cleared, and the next start is refused by the count check. This matches panic_test.rs.
  • Poisoned lifecycle_lock: lock_lifecycle recovers via PoisonError::into_inner, so Drop still runs stop. The runtime's own locks already used mutex_lock with the same recovery.
  • disconnect_peers catching a panic still bumps the hook count. That only happens after a first panic has already fail-stopped the node, so it changes nothing.
  • Profiles: check_panic_unwind.py resolves release-smaller through inherits, and every mobile cargo build --target line in the Android and Swift scripts uses it. release and dev stay abort. Tests ignore the profile's panic setting, so panic_test.rs unwinds under cargo test.
  • Ruled out: the hook seeing panics from other Rust libraries in the app. Each staticlib/cdylib links its own std and therefore its own hook. The process-wide scope inside ldk-node is stated in the PR body as accepted residual risk.

The background processor now drops itself on a panic, instead of relying on
the guard or `stop` to abort it, which left it persisting while `stop` waited.
It ends without reporting a failure, so the guard still disconnects the peers.

Verified: cargo build with and without uniffi, panic_test three times, library tests with uniffi
@ovitrif

ovitrif commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator Author

Force-pushed the throwaway test branch to rerun the suite on 04873ce, since the description's runs predate that change: https://github.com/synonymdev/ldk-node/actions/runs/46225871001

@piotr-iohk piotr-iohk left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

QA review

Scope: full PR diff against its merge base, at 04873ce.

2 actionable findings — resolve or provide an evidence-backed rebuttal.

Channel-changing calls still reach ChannelManager after the new fail-stop, and Node::stop can return success when a panic arrives while it waits for the final event-processor flush.

The resolved comment Keep panic monitoring active until the processor has stopped is addressed on this commit: end_on_panic drops the background processor when a panic is recorded, including during stop. The success result above is the remaining gap on that path. No matching open issue turned up for either mechanism.

Validation: python3 scripts/check_panic_unwind.py at this commit exits 0 and reports panic = unwind for the Android and Swift mobile build scripts. This review did not run cargo test or tests/panic_test.rs. The author reports a suite rerun in run 46225871001.

Suggested additional test cases

  • cargo test --features uniffi --test panic_test, using the two-node bitcoind setup in panic_stops_the_node_until_it_is_rebuilt after a channel is open. Snapshot the node's sqlite channel-monitor bytes, panic on another thread, wait until status().is_running is false, then call force_close_channel and, with a known claimable payment, bolt11_payment().claim_for_hash. Both should return PersistenceFailed, and the monitor bytes should match the snapshot.
  • While Node::stop is inside wait_on_background_processor_task, panic on another thread. stop should return PersistenceFailed.

Findings

  • [HIGH] Reject channel updates after a panic fail-stop — inline at src/lib.rs:877.
  • [MEDIUM] Return PersistenceFailed if a panic hits during stop's final flush — inline at src/lib.rs:968.

Reviewer model: grok-4.7
Reasoning effort: xhigh

Comment thread src/lib.rs Resolved
Comment thread src/lib.rs Resolved
Calls that change channel state now return PersistenceFailed once a panic occurred since the node was built, and stop reads the panic count again after waiting for the background processor.

Verified: cargo build with and without uniffi; cargo test --features uniffi --lib and --test panic_test
Verified: check_panic_unwind.py on the build scripts and on temp copies with cargo b and cargo rustc builds
Verified: cargo test --features uniffi --test panic_test, which fails without the re-read of the panic count
@ovitrif
ovitrif requested a review from piotr-iohk November 29, 2026 06:46

@piotr-iohk piotr-iohk left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

QA review

Scope: changes since 04873ce, including affected code paths, at 5471475. Baseline: previous review. Inherited coverage is the mobile release-smaller unwind profile and panic-unwind workflow, the process-wide panic hook and fail-stop, and the changelog contract.

No new actionable code findings.

The new guards return PersistenceFailed before close, force-close, splice, channel-config updates, event_handled, claim_for_hash, and fail_for_hash use their arguments. stop reads the panic count again after waiting for the background processor, so a panic during that wait fails shutdown: the panic guard is already cancelled, and the earlier snapshot is taken before the shutdown signal. The CI check now treats literal cargo b and cargo rustc mobile builds like cargo build.

Validation: python3 scripts/check_panic_unwind.py at this revision exited 0 and reported panic = unwind for the Android and Swift release-smaller builds. Matcher checks covered cargo b, cargo rustc, a toolchain prefix, and cargo ndk ... b, and rejected cargo bench and cargo install. panic_test.rs was not executed here; it needs bitcoind.

Earlier findings reconciled at this revision: 2 verified fixed.

Reviewer model: grok-4.7
Reasoning effort: xhigh

@ovitrif

ovitrif commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator Author

Merged: approved by @piotr-iohk, checks green, review threads answered.

@ovitrif
ovitrif merged commit 8e77a92 into main Nov 29, 2026
1 check passed
@ovitrif
ovitrif deleted the fix/unwind-panics branch November 29, 2026 09:05
@ovitrif

ovitrif commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator Author

Released in v0.7.0-rc.84.

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.

3 participants