Skip to content

Async channel manager persistence in background processor#4419

Draft
joostjager wants to merge 4 commits intolightningdevkit:mainfrom
joostjager:async-cm-persist
Draft

Async channel manager persistence in background processor#4419
joostjager wants to merge 4 commits intolightningdevkit:mainfrom
joostjager:async-cm-persist

Conversation

@joostjager
Copy link
Contributor

@joostjager joostjager commented Feb 13, 2026

Based on #4351, this moves ChannelManager persistence into the Selector loop so it runs concurrently with event processing and network I/O, rather than being joined with scorer/graph/sweeper writes. CM persistence is tracked as a CmPersistState polled via a new Selector variant, letting it span across loop iterations. On completion, deferred monitor writes are flushed before resuming event processing.

Benchmark results (simple A->B payments, 150 ms write latency):

  • Immediate monitor writing: 25% speed up
  • Deferred monitor writing: 0%

The deferred result is expected because channel manager writing still blocks monitor writing, which holds off event processing. The speed up for immediate mode comes from CM persistence no longer blocking event processing.

Open question: the 25% improvement only applies to immediate (non-deferred) monitor writing. For the time being, we use deferred writing (for persistence ordering guarantees), and that mode shows no improvement yet. Should we pursue this PR?

joostjager and others added 4 commits February 11, 2026 10:31
Pure refactor: move the bodies of Watch::watch_channel and
Watch::update_channel into methods on ChainMonitor, and have
the Watch trait methods delegate to them. This prepares for adding
deferred mode where the Watch methods will conditionally queue
operations instead of executing them immediately.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Add a `deferred` parameter to `ChainMonitor::new` and
`ChainMonitor::new_async_beta`. When set to true, the Watch trait
methods (watch_channel and update_channel) will unimplemented!() for
now. All existing callers pass false to preserve current behavior.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Replace the unimplemented!() stubs with a full deferred write
implementation. When ChainMonitor has deferred=true, Watch trait
operations queue PendingMonitorOp entries instead of executing
immediately. A new flush() method drains the queue and forwards
operations to the internal watch/update methods, calling
channel_monitor_updated on Completed status.

The BackgroundProcessor is updated to capture pending_operation_count
before persisting the ChannelManager, then flush that many writes
afterward - ensuring monitor writes happen in the correct order
relative to manager persistence.

Key changes:
- Add PendingMonitorOp enum and pending_ops queue to ChainMonitor
- Implement flush() and pending_operation_count() public methods
- Integrate flush calls in BackgroundProcessor (both sync and async)
- Add TestChainMonitor::new_deferred, flush helpers, and auto-flush
  in release_pending_monitor_events for test compatibility
- Add create_node_cfgs_deferred for deferred-mode test networks
- Add unit tests for queue/flush mechanics and full payment flow

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Instead of joining the ChannelManager persistence future with
scorer/graph/sweeper writes, track it as a CmPersistState and poll it
via a new Selector variant (G). This lets CM persistence run across
loop iterations concurrently with event processing and network I/O.

Key changes:
- Add generic G future to Selector and SelectorOutput::G variant
- Generalize OptionalSelector to propagate the inner future's Output
- Remove the A slot from Joiner (now only used for scorer/graph/sweeper/
  liquidity persistence)
- Introduce CmPersistState to track in-flight CM persistence alongside
  the number of deferred monitor writes captured at persist start
- On async CM persist completion, flush deferred monitor writes and
  continue back to event processing
- On shutdown, await any in-flight CM persist before the final persist

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@ldk-reviews-bot
Copy link

👋 Hi! I see this is a draft PR.
I'll wait to assign reviewers until you mark it as ready for review.
Just convert it out of draft status when you're ready for review!

@codecov
Copy link

codecov bot commented Feb 13, 2026

Codecov Report

❌ Patch coverage is 89.89247% with 47 lines in your changes missing coverage. Please review.
✅ Project coverage is 86.08%. Comparing base (4e32d10) to head (3ff3a37).
⚠️ Report is 42 commits behind head on main.

Files with missing lines Patch % Lines
lightning/src/chain/chainmonitor.rs 91.77% 22 Missing and 4 partials ⚠️
lightning-background-processor/src/lib.rs 72.58% 15 Missing and 2 partials ⚠️
lightning/src/util/test_utils.rs 92.85% 3 Missing ⚠️
lightning/src/ln/functional_test_utils.rs 97.72% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4419      +/-   ##
==========================================
+ Coverage   86.06%   86.08%   +0.01%     
==========================================
  Files         156      156              
  Lines      103188   103755     +567     
  Branches   103188   103755     +567     
==========================================
+ Hits        88808    89313     +505     
- Misses      11868    11920      +52     
- Partials     2512     2522      +10     
Flag Coverage Δ
tests 86.08% <89.89%> (+0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

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