Skip to content

Don't panic when background event processing fails - #1260

Open
ajaysehwal wants to merge 2 commits into
lightningdevkit:mainfrom
ajaysehwal:fix-bp-failure-panic
Open

ajaysehwal wants to merge 2 commits into
lightningdevkit:mainfrom
ajaysehwal:fix-bp-failure-panic

Conversation

@ajaysehwal

@ajaysehwal ajaysehwal commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Fixes the panic-on-background-processor-failure part of #381 (the "P0, easy" item in the discussion there).

Problem

If process_events_async returns an error (e.g. a persistence failure), the background processor task panicked:

  • With panic = "abort" (bindings, mobile) this aborts the host process.
  • Without it, only the task dies. The node keeps running and reports is_running, but the ChannelManager is never persisted again and no events are handled. Peers stay connected and keep applying channel updates, so the persisted ChannelManager falls further behind the ChannelMonitors. On restart LDK then force-closes those channels as stale. A later Node::stop also hits panic!("Stopping event handling failed...") while holding the is_running write 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 EMFILE on one ChannelManager write led to seven force closes.

Changes

  • The background processor task now returns its Result instead of panicking.
  • If it fails while the node is running, it logs the error and stops all peer traffic: connected peers are disconnected, and new inbound and outbound connections are refused (including reconnects, LSP discovery and Node::connect). Chain syncing and transaction broadcasting keep running, so the node still reacts to on-chain events until it is restarted.
  • To this end, ConnectionManager can now refuse connections at runtime, reusing the existing disable_lightning_connections check.
  • Runtime::wait_on_background_processor_task returns Error::PersistenceFailed for a failed or panicked task instead of panicking.
  • Node::stop marks the node as not running and returns that error. Node::start still just winds down on a failed startup and reports the startup error.

Not in this PR

  • Notifying the application of the failure while the node is running (is_running stays true, only an error is logged). This touches the public API; see the question below.
  • Retrying transient persistence errors before giving up.
  • A shutdown timeout still returns Ok, as before.
  • A connection attempt already in flight when the failure happens can still complete (bounded by the connection timeout).

Testing

Added unit tests in runtime.rs for a clean exit, a failed task and a panicking task. cargo test --lib passes. There is no end-to-end test with a store that starts failing while the node runs.

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.
@ldk-reviews-bot

ldk-reviews-bot commented Nov 24, 2026 •

Copy link
Copy Markdown

I've assigned @tnull as a reviewer!
I'll wait for their review and will help manage the review process.
Once they submit their review, I'll check if a second reviewer would be helpful.

@elsiribot

elsiribot commented Nov 27, 2026 •

Copy link
Copy Markdown

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)
embedded in a long-running daemon, with SqliteStore and the bitcoind RPC
chain source. Posting what happened as a data point for this PR.

Timeline (UTC, one day):

  • 00:23 The host runs out of memory; the process is stalled for minutes.

  • 00:31:29 The process is at its fd limit (soft RLIMIT_NOFILE 1024). The
    ChannelManager persist fails once:

    ERROR Failed to process events: I/O Error: Other. "Failed to write to key //manager: unable to open database file"
    thread 'tokio-rt-worker' panicked at src/lib.rs:604:17:
    Failed to process events
    ERROR Failed to poll for chain data: BlockSourceError { kind: Transient, error: Os { code: 24, kind: Uncategorized, message: "Too many open files" } }
    
  • 00:31 to 20:17 The process is built with unwinding panics, so only the
    background processor task dies. For 20 hours the node:

    • reports is_running == true, answers API calls, keeps peers connected,
      keeps syncing the chain and keeps applying and persisting
      ChannelMonitorUpdates (about 1,500 on the busiest channel);
    • never persists the ChannelManager again and never handles another event.
  • Incoming HTLCs are accepted and then never claimed, failed or forwarded.
    Four counterparties force-close in turn as those HTLCs reach their expiry
    (11:57, 17:07, 19:12, 19:44).

  • 17:07 The first of those closes hits the 0.1.4 force_shutdown update_id
    gap (rust-lightning#3857, fixed in 0.1.5; a blocked monitor update was
    waiting on an event that could never be handled). That panic poisons the
    monitor and manager locks, and every later call panics with PoisonError.

  • 20:17 The operator restarts the process. The ChannelManager on disk is 20
    hours behind every monitor, so LDK force-closes all remaining channels
    ("ChannelManager read from disk was stale compared to ChannelMonitor(s)").

No funds were lost, but one transient EMFILE on one write ended with seven
force closes. The fd limit and the memory exhaustion were our fault. What made
it expensive is that the node kept accepting channel updates for 20 hours
after it had lost the ability to persist the manager, and nothing told the
embedding application.

jbesraa pushed a commit to blocknil/ldk-node that referenced this pull request Nov 28, 2026
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.
@ajaysehwal

Copy link
Copy Markdown
Contributor Author

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) embedded in a long-running daemon, with SqliteStore and the bitcoind RPC chain source. Posting what happened as a data point for this PR.

Timeline (UTC, one day):

  • 00:23 The host runs out of memory; the process is stalled for minutes.

  • 00:31:29 The process is at its fd limit (soft RLIMIT_NOFILE 1024). The
    ChannelManager persist fails once:

    ERROR Failed to process events: I/O Error: Other. "Failed to write to key //manager: unable to open database file"
    thread 'tokio-rt-worker' panicked at src/lib.rs:604:17:
    Failed to process events
    ERROR Failed to poll for chain data: BlockSourceError { kind: Transient, error: Os { code: 24, kind: Uncategorized, message: "Too many open files" } }
    
  • 00:31 to 20:17 The process is built with unwinding panics, so only the
    background processor task dies. For 20 hours the node:

    • reports is_running == true, answers API calls, keeps peers connected,
      keeps syncing the chain and keeps applying and persisting
      ChannelMonitorUpdates (about 1,500 on the busiest channel);
    • never persists the ChannelManager again and never handles another event.
  • Incoming HTLCs are accepted and then never claimed, failed or forwarded.
    Four counterparties force-close in turn as those HTLCs reach their expiry
    (11:57, 17:07, 19:12, 19:44).

  • 17:07 The first of those closes hits the 0.1.4 force_shutdown update_id
    gap (rust-lightning#3857, fixed in 0.1.5; a blocked monitor update was
    waiting on an event that could never be handled). That panic poisons the
    monitor and manager locks, and every later call panics with PoisonError.

  • 20:17 The operator restarts the process. The ChannelManager on disk is 20
    hours behind every monitor, so LDK force-closes all remaining channels
    ("ChannelManager read from disk was stale compared to ChannelMonitor(s)").

No funds were lost, but one transient EMFILE on one write ended with seven force closes. The fd limit and the memory exhaustion were our fault. What made it expensive is that the node kept accepting channel updates for 20 hours after it had lost the ability to persist the manager, and nothing told the embedding application.

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, connect(), LSP discovery), so channel state can't keep changing while the ChannelManager can't be persisted. That was what caused the 20 hours of drift and the stale-manager force closes on restart. Chain syncing and broadcasting keep running, so the monitors still react to on-chain events. stop() now returns PersistenceFailed instead of panicking with the is_running lock held, so the PoisonError cascade from our side is gone too (the one you hit at 17:07 came from rust-lightning#3857, which is fixed in 0.1.5).

@ajaysehwal

Copy link
Copy Markdown
Contributor Author

@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 disable_lightning_connections check in ConnectionManager.

Two questions before I go further:

  1. Notifying the app. After the failure is_running stays true, so the application only learns about it from the logs, and in-flight HTLCs stay stuck until a restart. Options I see:

    • (a) a new NodeStatus field, e.g. background_processing_failed: bool;
    • (b) flipping is_running to false, while still letting stop() clean up;
    • (c) leave it to a follow-up.

    Which would you prefer, or should it live in this PR at all?

  2. Retries. Should transient persistence errors be retried before the background processor gives up (as you suggested in Address remaining panic-on-persistence failures #381 for local stores), or is that better as a separate PR?

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