Repository navigation
Don't panic when background event processing fails - #1260
ajaysehwal wants to merge 2 commits into
Conversation
A failing background processor used to `panic!`, which aborted the host process or, for embedders that unwind, left a node that kept running without persisting anything. `Node::stop` then panicked again while holding the `is_running` write lock, poisoning it for all later calls. Have the background processor task return its result instead. On failure it now logs, signals the other background tasks to stop and disconnects all peers, so no further channel state changes happen. `wait_on_background_processor_task` reports a failed or panicked task as `Error::PersistenceFailed`, which `Node::stop` returns after marking the node as not running.
|
👋 Hi! This PR is now in draft status. |
|
Field report: this failure mode cost a production node all of its channels We run ldk-node 0.6.1 (lightning 0.1.4, lightning-background-processor 0.1.0) Timeline (UTC, one day):
No funds were lost, but one transient |
A failing background processor used to `panic!`, which aborted the host process or, for embedders that unwind, left a node that kept running without persisting anything. `Node::stop` then panicked again while holding the `is_running` write lock, poisoning it for all later calls. Have the background processor task return its result instead. On failure it now logs, signals the other background tasks to stop and disconnects all peers, so no further channel state changes happen. `wait_on_background_processor_task` reports a failed or panicked task as `Error::PersistenceFailed`, which `Node::stop` returns after marking the node as not running. From the open upstream PR. Its one conflict, after lightningdevkit#1229, was Runtime's fields: the background processor task takes upstream's Result type, and lightningdevkit#1229's monitor_persistence_cancellation_token stays. (cherry picked from commit a3fff85) Upstream-PR: lightningdevkit#1260
Previously a failed background processor stopped all background tasks, including chain syncing and transaction broadcasting, so the node no longer reacted to the chain until restarted. Only cut off peers instead, which is what keeps channel state from changing while the `ChannelManager` can't be persisted. To this end, `ConnectionManager` can now refuse connections at runtime, which also covers reconnects, LSP discovery and `Node::connect`, and the listener drops inbound connections while connections are disabled.
Thanks a lot for the detailed report, this is exactly the failure mode this PR is about. With the latest push, a failed background processor no longer panics. Instead the node disconnects all peers and refuses new connections (inbound, reconnects, |
|
@tnull Updated after the field report above. The second commit keeps chain sync and broadcasting running on a background processor failure and only cuts off peers, via a runtime switch on the existing Two questions before I go further:
|
tnull
left a comment
There was a problem hiding this comment.
I don't think we want to do this as a simple/small PR right now. We held off on this so far because to properly address it we need to add full Node lifecycle management, i.e., some logic that cleanly shuts down and restarts the node on persistence failures. This requires more design discussion first, otherwise we'd already have tackled the remaining items on #381.
That makes sense. I'll move the discussion to #381 and put together a short outline of what lifecycle management could look like. I'll convert this to a draft so it can serve as a reference for the pieces that are already needed (the background processor returning its result, |
A failing background processor used to `panic!`, which aborted the host process or, for embedders that unwind, left a node that kept running without persisting anything. `Node::stop` then panicked again while holding the `is_running` write lock, poisoning it for all later calls. Have the background processor task return its result instead. On failure it now logs, signals the other background tasks to stop and disconnects all peers, so no further channel state changes happen. `wait_on_background_processor_task` reports a failed or panicked task as `Error::PersistenceFailed`, which `Node::stop` returns after marking the node as not running. From the open upstream PR. Its one conflict, after lightningdevkit#1229, was Runtime's fields: the background processor task takes upstream's Result type, and lightningdevkit#1229's monitor_persistence_cancellation_token stays. (cherry picked from commit a3fff85) Upstream-PR: lightningdevkit#1260
Previously a failed background processor stopped all background tasks, including chain syncing and transaction broadcasting, so the node no longer reacted to the chain until restarted. Only cut off peers instead, which is what keeps channel state from changing while the `ChannelManager` can't be persisted. To this end, `ConnectionManager` can now refuse connections at runtime, which also covers reconnects, LSP discovery and `Node::connect`, and the listener drops inbound connections while connections are disabled. Picked from the open upstream PR, which pushed this commit on 2026-12-02 (Esra's pick, 2026-12-02). Upstream writes it on top of lightningdevkit#1064 (disable_lightning_connections), which we don't take: here connections start enabled (AtomicBool::new(false)), and only this commit's runtime switch can disable them. (cherry picked from commit 14c6059) Upstream-PR: lightningdevkit#1260
A failing background processor used to `panic!`, which aborted the host process or, for embedders that unwind, left a node that kept running without persisting anything. `Node::stop` then panicked again while holding the `is_running` write lock, poisoning it for all later calls. Have the background processor task return its result instead. On failure it now logs, signals the other background tasks to stop and disconnects all peers, so no further channel state changes happen. `wait_on_background_processor_task` reports a failed or panicked task as `Error::PersistenceFailed`, which `Node::stop` returns after marking the node as not running. From the open upstream PR. Its one conflict, after lightningdevkit#1229, was Runtime's fields: the background processor task takes upstream's Result type, and lightningdevkit#1229's monitor_persistence_cancellation_token stays. (cherry picked from commit a3fff85) Upstream-PR: lightningdevkit#1260
Previously a failed background processor stopped all background tasks, including chain syncing and transaction broadcasting, so the node no longer reacted to the chain until restarted. Only cut off peers instead, which is what keeps channel state from changing while the `ChannelManager` can't be persisted. To this end, `ConnectionManager` can now refuse connections at runtime, which also covers reconnects, LSP discovery and `Node::connect`, and the listener drops inbound connections while connections are disabled. Picked from the open upstream PR, which pushed this commit on 2026-12-02 (Esra's pick, 2026-12-02). Upstream writes it on top of lightningdevkit#1064 (disable_lightning_connections), which we don't take: here connections start enabled (AtomicBool::new(false)), and only this commit's runtime switch can disable them. (cherry picked from commit 14c6059) Upstream-PR: lightningdevkit#1260
A failing background processor used to `panic!`, which aborted the host process or, for embedders that unwind, left a node that kept running without persisting anything. `Node::stop` then panicked again while holding the `is_running` write lock, poisoning it for all later calls. Have the background processor task return its result instead. On failure it now logs, signals the other background tasks to stop and disconnects all peers, so no further channel state changes happen. `wait_on_background_processor_task` reports a failed or panicked task as `Error::PersistenceFailed`, which `Node::stop` returns after marking the node as not running. From the open upstream PR. Its one conflict, after lightningdevkit#1229, was Runtime's fields: the background processor task takes upstream's Result type, and lightningdevkit#1229's monitor_persistence_cancellation_token stays. (cherry picked from commit a3fff85) Upstream-PR: lightningdevkit#1260
Previously a failed background processor stopped all background tasks, including chain syncing and transaction broadcasting, so the node no longer reacted to the chain until restarted. Only cut off peers instead, which is what keeps channel state from changing while the `ChannelManager` can't be persisted. To this end, `ConnectionManager` can now refuse connections at runtime, which also covers reconnects, LSP discovery and `Node::connect`, and the listener drops inbound connections while connections are disabled. Picked from the open upstream PR, which pushed this commit on 2026-12-02 (Esra's pick, 2026-12-02). Upstream writes it on top of lightningdevkit#1064 (disable_lightning_connections), which we don't take: here connections start enabled (AtomicBool::new(false)), and only this commit's runtime switch can disable them. (cherry picked from commit 14c6059) Upstream-PR: lightningdevkit#1260
…ses peers (lightningdevkit#1260) Our pick of lightningdevkit#1260 (14c6059, "Keep watching the chain when background processing fails") came without a test. failed_background_processing_keeps_chain_sync_and_refuses_peers fails the ChannelManager's persistence on a node with background chain sync, mines a block, and checks that the node cuts off its peer, refuses an outbound connect (ConnectionFailed) and an inbound one, keeps following the chain, and reports PersistenceFailed on stop. With the old behaviour (every background task stopped on the failure) it fails: the chain is no longer followed. Known race, not covered: a connect that passes the disabled check just before disable_connections() runs survives disconnect_all_peers.
A failing background processor used to `panic!`, which aborted the host process or, for embedders that unwind, left a node that kept running without persisting anything. `Node::stop` then panicked again while holding the `is_running` write lock, poisoning it for all later calls. Have the background processor task return its result instead. On failure it now logs, signals the other background tasks to stop and disconnects all peers, so no further channel state changes happen. `wait_on_background_processor_task` reports a failed or panicked task as `Error::PersistenceFailed`, which `Node::stop` returns after marking the node as not running. From the open upstream PR. Its one conflict, after lightningdevkit#1229, was Runtime's fields: the background processor task takes upstream's Result type, and lightningdevkit#1229's monitor_persistence_cancellation_token stays. (cherry picked from commit a3fff85) Upstream-PR: lightningdevkit#1260
Previously a failed background processor stopped all background tasks, including chain syncing and transaction broadcasting, so the node no longer reacted to the chain until restarted. Only cut off peers instead, which is what keeps channel state from changing while the `ChannelManager` can't be persisted. To this end, `ConnectionManager` can now refuse connections at runtime, which also covers reconnects, LSP discovery and `Node::connect`, and the listener drops inbound connections while connections are disabled. Picked from the open upstream PR, which pushed this commit on 2026-12-02 (Esra's pick, 2026-12-02). Upstream writes it on top of lightningdevkit#1064 (disable_lightning_connections), which we don't take: here connections start enabled (AtomicBool::new(false)), and only this commit's runtime switch can disable them. (cherry picked from commit 14c6059) Upstream-PR: lightningdevkit#1260
…ses peers (lightningdevkit#1260) Our pick of lightningdevkit#1260 (14c6059, "Keep watching the chain when background processing fails") came without a test. failed_background_processing_keeps_chain_sync_and_refuses_peers fails the ChannelManager's persistence on a node with background chain sync, mines a block, and checks that the node cuts off its peer, refuses an outbound connect (ConnectionFailed) and an inbound one, keeps following the chain, and reports PersistenceFailed on stop. With the old behaviour (every background task stopped on the failure) it fails: the chain is no longer followed. Known race, not covered: a connect that passes the disabled check just before disable_connections() runs survives disconnect_all_peers.
Fixes the panic-on-background-processor-failure part of #381 (the "P0, easy" item in the discussion there).
Problem
If
process_events_asyncreturns an error (e.g. a persistence failure), the background processor task panicked:panic = "abort"(bindings, mobile) this aborts the host process.is_running, but theChannelManageris never persisted again and no events are handled. Peers stay connected and keep applying channel updates, so the persistedChannelManagerfalls further behind theChannelMonitors. On restart LDK then force-closes those channels as stale. A laterNode::stopalso hitspanic!("Stopping event handling failed...")while holding theis_runningwrite lock, which poisons the lock so every subsequent API call panics too.The field report below shows the second case in production: a single transient
EMFILEon oneChannelManagerwrite led to seven force closes.Changes
Resultinstead of panicking.Node::connect). Chain syncing and transaction broadcasting keep running, so the node still reacts to on-chain events until it is restarted.ConnectionManagercan now refuse connections at runtime, reusing the existingdisable_lightning_connectionscheck.Runtime::wait_on_background_processor_taskreturnsError::PersistenceFailedfor a failed or panicked task instead of panicking.Node::stopmarks the node as not running and returns that error.Node::startstill just winds down on a failed startup and reports the startup error.Not in this PR
is_runningstaystrue, only an error is logged). This touches the public API; see the question below.Ok, as before.Testing
Added unit tests in
runtime.rsfor a clean exit, a failed task and a panicking task.cargo test --libpasses. There is no end-to-end test with a store that starts failing while the node runs.