From 7d6a2fc0dff770e523b08eab315faa175ad95492 Mon Sep 17 00:00:00 2001 From: Jorropo Date: Thu, 22 Aug 2024 11:21:48 +0200 Subject: [PATCH] fix: deadlock on panic in transport testsuite I had a panic in `list.Multiaddr()` which would deadlock the test instead of properly showing up the stack trace. This happens because in the defer block it waits for <-done however because the test never proceed the other goroutine is stuck on `.Accept()`, never sending `close(done)`. --- p2p/transport/testsuite/transport_suite.go | 24 ++++++++-------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/p2p/transport/testsuite/transport_suite.go b/p2p/transport/testsuite/transport_suite.go index bd8892e807..7c366a9d17 100644 --- a/p2p/transport/testsuite/transport_suite.go +++ b/p2p/transport/testsuite/transport_suite.go @@ -57,28 +57,17 @@ func SubtestBasic(t *testing.T, ta, tb transport.Transport, maddr ma.Multiaddr, } defer list.Close() - var ( - connA, connB transport.CapableConn - done = make(chan struct{}) - ) - defer func() { - <-done - if connA != nil { - connA.Close() - } - if connB != nil { - connB.Close() - } - }() + done := make(chan struct{}) go func() { defer close(done) - var err error - connB, err = list.Accept() + connB, err := list.Accept() if err != nil { t.Error(err) return } + defer connB.Close() + s, err := connB.AcceptStream() if err != nil { t.Error(err) @@ -115,10 +104,11 @@ func SubtestBasic(t *testing.T, ta, tb transport.Transport, maddr ma.Multiaddr, t.Error("CanDial should have returned true") } - connA, err = tb.Dial(ctx, list.Multiaddr(), peerA) + connA, err := tb.Dial(ctx, list.Multiaddr(), peerA) if err != nil { t.Fatal(err) } + defer connA.Close() s, err := connA.OpenStream(context.Background()) if err != nil { @@ -154,6 +144,8 @@ func SubtestBasic(t *testing.T, ta, tb transport.Transport, maddr ma.Multiaddr, t.Fatal(err) return } + + <-done } func SubtestPingPong(t *testing.T, ta, tb transport.Transport, maddr ma.Multiaddr, peerA peer.ID) {