Skip to content

Commit 15f46cc

Browse files
rootulpclaude
andauthored
fix: prevent GC finalizer from closing TCP conn in p2p switch tests (#2805)
## Summary - Fix flaky `TestPeerRemovedFromReactorOnStopWithNilReason` and `TestSwitchInitPeerIsNotCalledBeforeRemovePeer` tests by retaining the `net.Conn` returned by `rp.Dial()` instead of discarding it - When the connection was discarded (`_, err = rp.Dial(...)`), Go's GC finalizer on `netFD` could close the TCP socket, causing the switch's MConnection to receive EOF and immediately remove the peer before the test could observe it - Reproduced locally: ~2-4% failure rate with `-race`, 0% without `-race`; 0% failure rate after fix across 100+ race-enabled runs Closes #2804 ## Test plan - [x] `go test -v -race -run "TestPeerRemovedFromReactorOnStopWithNilReason|TestSwitchInitPeerIsNotCalledBeforeRemovePeer" -count=100 -timeout=5m ./p2p/` passes (0 failures out of 200 test runs) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent b91224a commit 15f46cc

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

p2p/switch_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -819,8 +819,9 @@ func TestSwitchInitPeerIsNotCalledBeforeRemovePeer(t *testing.T) {
819819
rp := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
820820
rp.Start()
821821
defer rp.Stop()
822-
_, err = rp.Dial(sw.NetAddress())
822+
rpConn, err := rp.Dial(sw.NetAddress())
823823
require.NoError(t, err)
824+
defer rpConn.Close()
824825

825826
// wait till the switch adds rp to the peer set, then stop the peer asynchronously
826827
require.Eventually(t, func() bool {
@@ -832,8 +833,9 @@ func TestSwitchInitPeerIsNotCalledBeforeRemovePeer(t *testing.T) {
832833
}, 5*time.Second, 20*time.Millisecond, "peer was never added to switch")
833834

834835
// simulate peer reconnecting to us
835-
_, err = rp.Dial(sw.NetAddress())
836+
rpConn2, err := rp.Dial(sw.NetAddress())
836837
require.NoError(t, err)
838+
defer rpConn2.Close()
837839
// wait till the switch adds rp to the peer set
838840
time.Sleep(50 * time.Millisecond)
839841

@@ -867,8 +869,9 @@ func TestPeerRemovedFromReactorOnStopWithNilReason(t *testing.T) {
867869
rp := &remotePeer{PrivKey: ed25519.GenPrivKey(), Config: cfg}
868870
rp.Start()
869871
defer rp.Stop()
870-
_, err = rp.Dial(sw.NetAddress())
872+
rpConn, err := rp.Dial(sw.NetAddress())
871873
require.NoError(t, err)
874+
defer rpConn.Close()
872875

873876
// wait till the switch adds rp to the peer set
874877
var peer Peer

0 commit comments

Comments
 (0)