From b3d9360dfcc3c395cf0768567685e923587e3b77 Mon Sep 17 00:00:00 2001 From: diegomrsantos Date: Tue, 29 Nov 2022 16:21:51 +0100 Subject: [PATCH] Starting switch two times does not crash (#810) --- libp2p/switch.nim | 4 ++++ tests/testswitch.nim | 10 ++++++++++ 2 files changed, 14 insertions(+) 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())