AI-generated analysisPublished automatically and not human-verified. Validated context appears in community notes below.
← Watch feed
Low 32 Bitcoin

server: gate handleDonePeerMsg on peerAdded, not VerAckReceived

Public commit record

What the developer wrote

Authored by Olaoluwa Osuntokun

73/100 · Adequate
server: gate handleDonePeerMsg on peerAdded, not VerAckReceived

peer.processRemoteVerAckMsg sets verAckReceived=true *before* it
invokes the OnVerAck listener that closes verAckCh. If the peer
disconnects in that window, the peerLifecycleHandler select may
pick Peer.Done() over verAckCh and skip the peerAdd send -- yet
VerAckReceived() still returns true. handleDonePeerMsg would then
call syncManager.DonePeer for a peer that was never NewPeer'd
(sync manager logs a warning and discards).

Add an explicit peerAdded atomic.Bool on serverPeer. Set it in
peerLifecycleHandler after peerAdd has been enqueued. Gate the
syncManager.DonePeer + orphan eviction in handleDonePeerMsg on
peerAdded.Load() so those side effects only fire for peers that
were actually registered.

This tightens the "peerAdd precedes peerDone, or peerAdd is
skipped entirely" invariant from this PR into something explicit
rather than something derived from a flag that races OnVerAck.
✓ Specific, descriptive subject✓ Names a concrete action or component✓ Provides detailed explanatory context
The short version

What changed, and why it matters

This commit fixes a subtle internal race condition in btcd's peer-handling logic. Previously, a peer could disconnect at exactly the wrong moment, causing the server to tell its sync manager that a peer had finished even though it had never been properly registered. That produced only a warning and skipped cleanup, but it weakened the invariant that every 'peer done' event matches a prior 'peer add' event. The patch adds an explicit flag so the server only runs cleanup side effects for peers that were actually registered.

Recommended action

Treat as a low-severity hardening fix. Reviewers should verify that peerAdded is set in exactly one place and that no other code paths rely on VerAckReceived() for the same side effects. No immediate incident response is warranted unless further analysis shows the mismatched DonePeer can be exploited for denial of service or state corruption.

Security signals we found

01

Race condition between verack flag and disconnect channel

02

Mismatched sync-manager peer lifecycle events (DonePeer without NewPeer)

03

Atomic flag added to enforce explicit peer-registration invariant

04

Cleanup side effects (orphan eviction) gated on actual registration

Risk score

Why this scored 32/100

Our methodology →
Potential impact 8/30
Exploitability 3/25
Stealth signal 5/15
Affected reach 5/15
Confidence 7/10
Evidence quality 4/5
Human-validated context

Community notes

Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.

No validated notes yet.

The AI analysis stands alone for now. Submit a note if you can add evidence or important context.