diff --git a/libp2p/switch.nim b/libp2p/switch.nim index 3bbdaa613..93f2b5f27 100644 --- a/libp2p/switch.nim +++ b/libp2p/switch.nim @@ -312,12 +312,10 @@ proc start*(s: Switch) {.async, gcsafe, public.} = await allFutures(startFuts) - for s in startFuts: - if s.failed: - # TODO: replace this exception with a `listenError` callback. See - # https://github.com/status-im/nim-libp2p/pull/662 for more info. - raise newException(transport.TransportError, - "Failed to start one transport", s.error) + for fut in startFuts: + if fut.failed: + await s.stop() + raise fut.error for t in s.transports: # for each transport if t.addrs.len > 0 or t.running: diff --git a/tests/testswitch.nim b/tests/testswitch.nim index 3d47b1a5b..608266c09 100644 --- a/tests/testswitch.nim +++ b/tests/testswitch.nim @@ -1035,3 +1035,12 @@ suite "Switch": await conn.close() await src.stop() await dst.stop() + + asyncTest "switch failing to start stops properly": + let switch = newStandardSwitch( + addrs = @[MultiAddress.init("/ip4/0.0.0.0/tcp/0").tryGet(), MultiAddress.init("/ip4/1.1.1.1/tcp/0").tryGet()] + ) + + expect LPError: + await switch.start() + # test is that this doesn't leak