Skip to content

fix(p2p): make P2P gossip mode flag atomic to eliminate a data race#457

Open
amathxbt wants to merge 1 commit into
canopy-network:mainfrom
amathxbt:fix/gossip-mode-race
Open

fix(p2p): make P2P gossip mode flag atomic to eliminate a data race#457
amathxbt wants to merge 1 commit into
canopy-network:mainfrom
amathxbt:fix/gossip-mode-race

Conversation

@amathxbt

@amathxbt amathxbt commented Jul 4, 2026

Copy link
Copy Markdown

Bug

P2P.gossip (in p2p/p2p.go) is a plain bool field read and written from different goroutines without synchronization:

type P2P struct {
	...
	gossip bool // whether gossip mode is active
	...
}

func (p *P2P) SetGossipMode(gossip bool) {
	p.gossip = gossip
}

func (p *P2P) GossipMode() bool {
	return p.gossip
}

SetGossipMode() is called from Controller in controller/consensus.go (during must-connect/peer-count updates), while GossipMode() is read concurrently from multiple other places in controller/consensus.go (ShouldGossip, and the self-send/replica-broadcast decision in the block-proposal path) as part of normal BFT/consensus operation. These calls happen on different goroutines (consensus loop vs. peer-management callbacks) with no shared lock, which is a genuine unsynchronized concurrent read/write on a plain bool — undefined behavior per the Go memory model, and flagged by the race detector.

Fix

Changed gossip from bool to atomic.Bool and updated SetGossipMode/GossipMode to use Store/Load. This is a minimal, drop-in fix: atomic.Bool's zero value is false, matching the previous default, and both accessor methods keep their existing signatures so no call sites outside this file need to change.

Testing

Confirmed p.gossip was only referenced within p2p/p2p.go via SetGossipMode/GossipMode, and that all external callers (in controller/consensus.go) only go through these two methods, so the internal type change is fully encapsulated.

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.

1 participant