Repository navigation
Update stored peer address on persist connect - #1115
Closed
vincenzopalazzo wants to merge 1 commit into
Closed
vincenzopalazzo wants to merge 1 commit into
vincenzopalazzo wants to merge 1 commit into
Conversation
PeerStore::add_peer returned early when the pubkey was already stored, so Node::connect(..., persist=true) left a stale address for reconnect. Replace the address and persist it when it changes, and leave memory untouched if that write fails. Assisted-by: goose (AI)
|
I've assigned @tnull as a reviewer! |
Contributor
|
I think you might want to look at #1002. It addresses the same issue. |
Member
Author
vincenzopalazzo
added a commit
to vincenzopalazzo/ldk-node
that referenced
this pull request
Oct 16, 2026
PeerStore::add_peer returned early when the pubkey was already stored, so Node::connect(..., persist=true) with a new address silently kept the stale address used for reconnection (lightningdevkit#700). Split add_peer into two methods sharing a persist-then-apply helper, following the maintainer review on lightningdevkit#1002: - upsert_peer updates the stored address on re-add. Used where the address is explicitly supplied: Node::connect with persist set, open_channel, and the LSPS2 payment path. - add_peer_if_missing only stores unknown peers. Used for the gossip-derived address of inbound channel counterparties, which must not override an explicitly stored address. In-memory state is only mutated after a successful write, matching remove_peer. Following the review suggestion on lightningdevkit#1002, avoid cloning the entire peer map: apply the update temporarily under the write lock for serialization, restore it, persist, then apply it permanently. Readers cannot observe the temporary value as they need the same lock. Re-adding an unchanged peer is a no-op. The liquidity-side counterpart (NodeBuilder/Liquidity add_liquidity_source) is deliberately left out; it is tracked in lightningdevkit#1147. Fixes lightningdevkit#700. Supersedes lightningdevkit#1002 and lightningdevkit#1115. Co-authored-by: Bartok <259807879+Bartok9@users.noreply.github.com> Assisted-by: goose (AI)
vincenzopalazzo
added a commit
to vincenzopalazzo/ldk-node
that referenced
this pull request
Oct 16, 2026
PeerStore::add_peer returned early when the pubkey was already stored, so Node::connect(..., persist=true) with a new address silently kept the stale address used for reconnection (see lightningdevkit#700). Split add_peer into two methods sharing a persist-then-apply helper, following the maintainer review on lightningdevkit#1002: - upsert_peer updates the stored address on re-add. Used where the address is explicitly supplied: Node::connect with persist set, open_channel, and the LSPS2 payment path. - add_peer_if_missing only stores unknown peers. Used for the gossip-derived address of inbound channel counterparties, which must not override an explicitly stored address. In-memory state is only mutated after a successful write, matching remove_peer. Following the review suggestion on lightningdevkit#1002, avoid cloning the entire peer map: apply the update temporarily under the write lock for serialization, restore it, persist, then apply it permanently. Readers cannot observe the temporary value as they need the same lock. Re-adding an unchanged peer is a no-op. Addresses the peer-store side of lightningdevkit#700. The liquidity-source side (NodeBuilder/Liquidity add_liquidity_source) is deliberately left out here; it is tracked in lightningdevkit#1147. Supersedes lightningdevkit#1002 and lightningdevkit#1115. Co-authored-by: Bartok <259807879+Bartok9@users.noreply.github.com> Assisted-by: goose (AI)
vincenzopalazzo
added a commit
to vincenzopalazzo/ldk-node
that referenced
this pull request
Oct 16, 2026
PeerStore::add_peer returned early when the pubkey was already stored, so Node::connect(..., persist=true) with a new address silently kept the stale address used for reconnection (see lightningdevkit#700). Split add_peer into two methods sharing a persist-then-apply helper, following the maintainer review on lightningdevkit#1002: - upsert_peer updates the stored address on re-add. Used where the address is explicitly supplied: Node::connect with persist set, open_channel, and the LSPS2 payment path. - add_peer_if_missing only stores unknown peers. Used for the gossip-derived address of inbound channel counterparties, which must not override an explicitly stored address. In-memory state is only mutated after a successful write, matching remove_peer. Following the review suggestion on lightningdevkit#1002, avoid cloning the entire peer map: apply the update temporarily under the write lock for serialization, restore it, persist, then apply it permanently. Readers cannot observe the temporary value as they need the same lock. Re-adding an unchanged peer is a no-op. remove_peer is switched to the same pattern for consistency. Addresses the peer-store side of lightningdevkit#700. The liquidity-source side (NodeBuilder/Liquidity add_liquidity_source) is deliberately left out here; it is tracked in lightningdevkit#1147. Supersedes lightningdevkit#1002 and lightningdevkit#1115. Co-authored-by: Bartok <259807879+Bartok9@users.noreply.github.com> Assisted-by: goose (AI)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
connect(pubkey, new_addr, persist=true)succeeded and logged the new socket, but the peer store kept the old address. The reconnect task only readspeer_store.list_peers(), so the next dial (and every dial after restart) used the stale address.028ea2162c8cc5d9bd938c5b218a3432b1eae23f9d36f491d6d8a811b95578d494: stored98.207.69.189:54324, connected at98.234.61.157:54324on 2026-09-21T17:43:24Z, then dialed the old address again from 2026-09-22T13:00:12Z about once a minute. A process restart only reloaded the stale entry.PeerStore::add_peerreturnedOk(())when the pubkey was already present and never wrote the new address. It now replaces the address and persists it. Memory is updated only after the write succeeds, same rule asremove_peer. An unchanged address still skips the write.Review Notes
Decision Log
Hardest decision: Persist the updated map before mutating
self.peers, instead of inserting under the write lock and then writing.list_peers()is what the reconnect task dials, so memory has to move, but a failed KV write must not leave this process ahead of disk. Encoding a clone and inserting only afterpersist_peersmatchesremove_peer.Alternatives rejected:
disconnectthenconnect: disconnect removes the persisted peer and drops the live session. That is a workaround, not the persist contract.Least confident about: HashMap iteration order in
PeerStoreSerWrapperis not stable, so the unchanged-address test compares raw KV bytes. A future change that re-encodes an equal map could fail that assertion even though the logical address is unchanged. The reload assertions on127.0.0.1:9739are the ones that catch the ldk-server bug.Test plan
add_peer_updates_stored_addressfails onupstream/main(cd4fe79): reloaded address stays127.0.0.1:9738127.0.0.1:9739peer_info_persistenceandremove_peer_does_not_mutate_memory_if_persist_failspassadd_peer_does_not_mutate_memory_if_persist_failspassesAssisted-by: goose (AI)