Skip to content

Persist the peer before initiating a channel - #1252

Open
cryptokvc wants to merge 5 commits into
lightningdevkit:mainfrom
cryptokvc:persist-peer-before-channel
Open

cryptokvc wants to merge 5 commits into
lightningdevkit:mainfrom
cryptokvc:persist-peer-before-channel

Conversation

@cryptokvc

@cryptokvc cryptokvc commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #1143. Supersedes #1149, rebased onto current main as two additive commits, as requested there.

open_channel_inner created the channel and only then wrote the peer to the peer store. If that write failed, the API returned PersistenceFailed for a channel that was already live, and a caller retrying would open a second channel (and push the amount again).

This takes the first option from the issue: the peer is persisted before create_channel, so a persistence failure is returned before anything is initiated, and before the fee-rate override is registered. If channel creation then fails, the peer stays stored: removing it again raced with concurrent open_channel calls to the same peer, so that cleanup was dropped on review and is left to a later PeerStore refactor.

The PeerStore::add_peer caching issue the issue mentions is left to #1150, as agreed on #1149.

Change

After review, the diff is +1/−1 in src/lib.rs: add_peer moves up before create_channel. The regression test, the cleanup and the explanatory comment were dropped, as asked on review. The branch history still shows them; it can be squashed on merge.

Testing: cargo check passes on the final branch. The earlier integration test is no longer part of this PR.

🤖 Generated with Claude Code

https://claude.ai/code/session_014cRzwuyJe8BGwXowsQ3kpw

cryptokvc and others added 2 commits November 20, 2026 04:09
open_channel_inner created the channel and only then wrote the peer to
the peer store. If that write failed, the API returned PersistenceFailed
for a channel that was already live, and a caller retrying would open a
second channel.

Persist the peer first, so a persistence failure is returned before
anything is initiated. If channel creation then fails and the peer was
only stored for this attempt, remove it again so we don't keep
reconnecting to it, unless we have a channel with it (a concurrent
open_channel to the same peer may have succeeded in the meantime), as
remove_peer_after_reconnect does.

Co-authored-by: Claude <noreply@anthropic.com>
PeerWriteFailingStore wraps an InMemoryStore and fails writes of the
persisted peer list while a flag is set. With the flag on, open_channel
returns PersistenceFailed and no channel exists. Once the store recovers,
a retry opens exactly one channel and the peer is persisted.

Co-authored-by: Claude <noreply@anthropic.com>
@ldk-reviews-bot

ldk-reviews-bot commented Nov 20, 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.

@tnull tnull 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.

Thanks! Let's drop the cleanup part please.

Comment thread src/lib.rs Outdated
.is_empty();
if !peer_was_stored && !has_channels {
if let Err(e) =
self.runtime.block_on(self.peer_store.remove_peer(&peer_info.node_id))

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.

Hmm, I think this cleanup is race-y for concurrent calls of open_channel. Let's simplify things further and just move the add_peer line up in this PR.

Eventually we'll want to do a larger PeerStore refactor that is more robust.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Thanks, dropped in aa9424a. The add_peer call is the only change left in open_channel_inner: it now runs before create_channel, and nothing removes the peer if creation fails, so a failed attempt leaves the peer stored. (The commit message says "as before this PR" about that, which is wrong: before this PR a failed creation never stored the peer.) cargo check --tests and cargo fmt --check pass. I could not rerun channel_open_fails_cleanly_when_peer_persistence_fails here because my environment cannot download bitcoind; that test only covers the failed peer write, which this commit does not touch.

The removal raced with concurrent open_channel calls to the same peer. A failed creation now keeps the peer it stored, as before this PR; a later PeerStore refactor can revisit this.

Co-authored-by: Claude <noreply@anthropic.com>

@tnull tnull 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.

I don't think we need the big regression test, and the comment should also not tell the 'history' of the code. I think we can just reduce this to a +1/-1 diff really, just moving the one line up.

cryptokvc and others added 2 commits November 24, 2026 07:15
Co-authored-by: Claude <noreply@anthropic.com>
Co-authored-by: Claude <noreply@anthropic.com>
@cryptokvc

Copy link
Copy Markdown
Contributor Author

Thanks @tnull, done. The PR is now a +1/-1 diff in src/lib.rs: add_peer moved up before create_channel, without the comment, and the regression test is gone. cargo check passes. I can squash the commits if you prefer.

@tnull tnull 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.

Thanks, but please clean up the commit history. And please don't open another PR for this.

@Camillarhi Camillarhi left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Small race to keep in mind, a pending remove_peer_after_reconnect can now pass its check before create_channel and remove the peer, since nothing re-adds it after.

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.

Avoid returning failure after initiating a channel

4 participants