Skip to content

bug: data race and leaked dialing counter in DialForOutboundPeers causes node to stop connecting new peers #491

Description

@Sertug17

Summary

DialForOutboundPeers() in p2p/p2p.go has two related bugs in the initial config peer dial loop:

  1. Data race the dialing variable is written from spawned goroutines and read from the main loop without any synchronization.
  2. Counter never decremented goroutines increment dialing but never decrement it after DialWithBackoff returns, so the counter stays permanently inflated.

Affected Code

File: p2p/p2p.goDialForOutboundPeers()

for _, peerString := range p.config.DialPeers {
    peerAddress, err := getPeerFromString(peerString)
    if err != nil {
        continue
    }
    go func() {
        dialing++ // ← unsynchronized write from goroutine
        p.DialWithBackoff(peerAddress, true)
        // ← dialing is NEVER decremented after DialWithBackoff returns
    }()
}

// main loop reads dialing without synchronization:
if outbound > 0 && outbound+dialing >= p.config.MaxOutbound {
    return // ← falsely triggered once initial goroutines finish
}

Impact

  • After the initial DialPeers goroutines complete, dialing stays permanently at len(p.config.DialPeers).
  • The main dial loop evaluates outbound + dialing >= MaxOutbound as true and stops dialing new peers, even when the node is well under its outbound peer limit.
  • Running with the -race flag will surface the data race on dialing.

Steps to Reproduce

  1. Set one or more entries in DialPeers in node config
  2. Start node with go run -race ./cmd/...
  3. Observe data race warning on dialing in DialForOutboundPeers
  4. After initial dial goroutines finish, observe node stops making new outbound connections despite being under MaxOutbound

Suggested Fix

Use sync/atomic and add a deferred decrement:

var dialing atomic.Int64

for _, peerString := range p.config.DialPeers {
    peerAddress, err := getPeerFromString(peerString)
    if err != nil {
        continue
    }
    go func() {
        dialing.Add(1)
        defer dialing.Add(-1)
        p.DialWithBackoff(peerAddress, true)
    }()
}

// main loop:
if outbound > 0 && int64(outbound)+dialing.Load() >= int64(p.config.MaxOutbound) {
    return
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions