Fix startup session races and active-pool re-admission - #39
Merged
pk910 merged 2 commits intoJul 29, 2026
Conversation
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.
Three related bugs around service startup and active-pool admission:
1. Sessions we initiate never carry a node reference (
discv5/protocol/handler.go)When we initiate a handshake (we ping, peer responds WHOAREYOU),
handleWHOAREYOUPacketcreated the session and only inherited the node reference from a previous session —pending.ToNodewas never attached, even though it's the node we were sending to and was just used to verify the handshake.After a restart the session cache is empty, so every session created during the startup burst (aliveness pings, random walks, bootnode connects) had
Node == nil. Consequences for the whole session lifetime (12h by default):OnHandshakeCompletenever fired (guarded bysess.GetNode() != nil), so the peer was never admitted to the active pool via the handshake path.handleMessagewith a nil node, soOnNodeSeennever fired either — no lastSeen updates, no ENR-seq refresh.Fix: fall back to
pending.ToNodewhen no previous session provides a node.2.
LoadInitialNodesFromDBmutates the pool without locking (nodes/flattable.go)The discv5 packet handler is registered and receiving live traffic during
bootnode.New(), beforeStart()runsLoadInitialNodesFromDB. A peer handshaking in that window callsFlatTable.Add(which lockst.mu) concurrently withLoadInitialNodesFromDBwritingt.activeNodesandt.ipLimiterwith no lock — a data race that can fatal withconcurrent map writes.Fix: hold
t.mufor the load, and skip nodes already in the pool so a freshly handshaked node isn't replaced by its stale DB copy or double-counted by the IP limiter.3. Re-admit known nodes to the active pool on traffic (
bootnode/service.go)Previously the only path into the active pool for a peer that contacts us was
OnHandshakeComplete. A node demoted by a sweep — or missed at handshake time — could ping us for the entire session lifetime without ever rejoining the pool, sinceonNodeSeenonly updatedlastSeenfor nodes already present.onNodeSeennow attemptstable.Addfor nodes that pass the layer's fork filter.FlatTable.Getfalls back to the DB, so demoted-but-alive nodes are re-admitted as soon as they talk to us;Addis a no-op for already-active nodes and still enforces IP limits and the pool's hard cap.Verified with
go build,go vet, andgo test -race ./bootnode/... ./nodes/....