diff --git a/libp2p/switch.nim b/libp2p/switch.nim index b6fc22fec..b1a7d489e 100644 --- a/libp2p/switch.nim +++ b/libp2p/switch.nim @@ -296,6 +296,10 @@ proc stop*(s: Switch) {.async, public.} = proc start*(s: Switch) {.async, gcsafe, public.} = ## Start listening on every transport + if s.started: + warn "Switch has already been started" + return + trace "starting switch for peer", peerInfo = s.peerInfo var startFuts: seq[Future[void]] for t in s.transports: diff --git a/tests/testswitch.nim b/tests/testswitch.nim index 1e0db7e15..3cbb66d7e 100644 --- a/tests/testswitch.nim +++ b/tests/testswitch.nim @@ -1016,3 +1016,13 @@ suite "Switch": expect LPError: await switch.start() # test is that this doesn't leak + + asyncTest "starting two times does not crash": + let switch = newStandardSwitch( + addrs = @[MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet()] + ) + + await switch.start() + await switch.start() + + await allFuturesThrowing(switch.stop())