fix(p2p): synchronize access to the inbound listener - #568
Open
0xrlawrence wants to merge 1 commit into
Open
0xrlawrence wants to merge 1 commit into
0xrlawrence wants to merge 1 commit into
Conversation
p.listener was written by ListenForInboundPeers from the goroutine Start() spawns, and read unsynchronized by Stop() and by several test helpers. The race detector reported it as the single largest source of races in this package. The practical consequence is at shutdown: Stop() can observe a nil listener that has in fact already been created, skip the Close(), and leak the socket; the accept loop then keeps running against a listener nobody closed. Changes: - guard the field with a dedicated listenerMu - publish the listener under the lock, then run the accept loop against a local reference so the hot path stays lock-free - read it under the lock in Stop() - add ListenerAddress(), a synchronized accessor, and use it in the tests that previously reached into n.listener directly Verified with 'go test -race ./p2p/': data races in this package drop from 65 to 8, and no remaining race involves the listener. The 8 that remain are pre-existing and unrelated (Stream/MultiConn state in conn.go and encrypt.go); they are left for separate changes. Note for reviewers: this touches ListenForInboundPeers, which PR canopy-network#521 also edits. The two changes are independent but will need a trivial rebase depending on merge order.
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.
Description
p.listeneris written byListenForInboundPeersfrom the goroutineStart()spawns, and read unsynchronized byStop()and by several test helpers. The race detector reports it as the single largest source of races in this package.The practical consequence is at shutdown:
Stop()can observe a nil listener that has in fact already been created, skip theClose(), and leak the socket — while the accept loop keeps running against a listener nobody closed.Changes Made
listenerMu.Stop().ListenerAddress(), a synchronized accessor, and use it in the tests that previously reached inton.listenerdirectly.Testing
go test -race ./p2p/: data races in this package drop from 65 to 8, and no remaining race involves the listener.The 8 that remain are pre-existing and unrelated —
Stream/MultiConnstate inconn.goandencrypt.go. Left for separate changes rather than widening this one.go test ./p2p/passes.Note for reviewers
This touches
ListenForInboundPeers, which #521 also edits. The two changes are independent but will need a trivial rebase depending on merge order.🤖 Generated with Claude Code