Skip to content

Route peer store mutations through a single persist-first path - #1234

Open
tnull wants to merge 4 commits into
lightningdevkit:mainfrom
tnull:2026-11-peer-store-single-mutation-path
Open

tnull wants to merge 4 commits into
lightningdevkit:mainfrom
tnull:2026-11-peer-store-single-mutation-path

Conversation

@tnull

@tnull tnull commented Nov 12, 2026

Copy link
Copy Markdown
Collaborator

Several recent PRs each fixed one symptom of the peer store's persistence
handling. This addresses them at the store level instead:

  • add_peer and remove_peer now share one mutation path that persists
    the updated peer set before publishing it to readers, so the in-memory
    set can no longer get ahead of the persisted one and a retry after a
    failed write persists again.
  • Removing a peer that isn't stored no longer writes.
  • The persisted peer count uses CollectionLength, as Use CollectionLength for the persisted event queue length #1230 did for the
    event queue, and we no longer preallocate based on it.
  • After a write whose outcome is unknown (failed or cancelled, but possibly
    applied by the store), the next mutation rewrites the peer set even if it
    changes nothing.

Supersedes #1150 and the peer-store half of #1215. It also replaces the PeerStore rewrite in #1175, which can be rebased to only add upsert_peer and its call-site changes on top. #1149 needed the #1150 fix and can now be rebased onto this.

Closes #1142.

Adding a peer inserted it into the in-memory set before the write was
awaited. If the write failed, a retry found the peer already present
and returned success without writing again, so the peer was reported
as stored but was gone after a restart. Removing a peer already
persisted first, but did so through separate hand-rolled logic.

Route both mutations through a single helper that applies the change
to a copy of the peer set, persists it, and only then publishes it to
readers. The in-memory set can thereby no longer get ahead of the
persisted one, and a retry after a failed write persists again.

Co-Authored-By: HAL 9000
Removing a peer rewrote the persisted peer set even if the peer was
not stored. With a failing store, callers were thereby handed a
persistence error for a removal that had nothing to remove, e.g., when
a channel to a peer we never stored was closed.

Report whether the removal changed the peer set, so that nothing is
written, and nothing can fail, if the peer is unknown.

Co-Authored-By: HAL 9000
The number of persisted peers was written as a plain `u16`, so a peer
set of more than 65535 entries had its count truncated and most of its
peers silently dropped on the next restart. The event queue had the
same issue, which was fixed by switching to `CollectionLength`.

Do the same for the peer store. As `CollectionLength` encodes counts
below `u16::MAX` exactly as before, previously persisted peer sets
are still read and smaller ones are still written unchanged. As the
count read from disk is thereby no longer bounded to `u16::MAX`, we
also stop preallocating for it, so that a corrupted count fails to
read rather than exhausting memory.

Co-Authored-By: HAL 9000
A peer store write that fails or is cancelled may still have reached
the store, e.g., if a remote store timed out after applying it. The
persisted peer set could then differ from the in-memory one, and as
mutations that don't change the in-memory set skip the write, the
difference was only repaired by the next unrelated change. A peer the
user removed could thereby be reconnected to after a restart.

Remember that the outcome of a write is unknown until one succeeds,
and write the peer set on the next mutation regardless of whether it
changes anything.

Co-Authored-By: HAL 9000
@tnull tnull added this to the 0.8 milestone Nov 12, 2026
@tnull
tnull requested a review from jkczyz November 12, 2026 12:51
@ldk-reviews-bot

ldk-reviews-bot commented Nov 12, 2026 •

Copy link
Copy Markdown

👋 I see @jkczyz was un-assigned.
If you'd like another reviewer assignment, please click here.

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

LGTM!

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.

Do not report unpersisted peers as successfully stored

3 participants