Skip to content

fix: resolve data race and leaked dialing counter in DialForOutboundPeers - #492

Open
Sertug17 wants to merge 1 commit into
canopy-network:mainfrom
Sertug17:fix/dial-outbound-data-race
Open

fix: resolve data race and leaked dialing counter in DialForOutboundPeers#492
Sertug17 wants to merge 1 commit into
canopy-network:mainfrom
Sertug17:fix/dial-outbound-data-race

Conversation

@Sertug17

@Sertug17 Sertug17 commented Aug 4, 2026

Copy link
Copy Markdown

Summary

Fixes the data race and permanent counter inflation reported in #491.

Changes in p2p/p2p.go

  • Replaced plain int dialing variable with sync/atomic.Int64 to eliminate the data race between the initial DialPeers goroutines and the main loop
  • Added defer dialing.Add(-1) in the initial goroutines so the counter correctly decrements when each DialWithBackoff call completes
  • Updated all read/write sites to use .Add() and .Load()

Before

dialing := 0
go func() {
    dialing++ // unsynchronized, never decremented
    p.DialWithBackoff(peerAddress, true)
}()

After

var dialing atomic.Int64
go func() {
    dialing.Add(1)
    defer dialing.Add(-1) // correctly decrements on completion
    p.DialWithBackoff(peerAddress, true)
}()

Closes #491

- Replace plain int dialing counter with sync/atomic.Int64 to
  eliminate data race between spawned goroutines and the main loop
- Add deferred dialing.Add(-1) in initial DialPeers goroutines so
  the counter correctly drops back when each dial completes

Fixes canopy-network#491
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.

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

1 participant