From 72abe822c067ddf3a1315f508c102d995d7625f4 Mon Sep 17 00:00:00 2001 From: Tanguy Date: Thu, 15 Sep 2022 09:06:32 +0200 Subject: [PATCH] Fix switch failed start (#770) --- libp2p/switch.nim | 10 ++++------ tests/testswitch.nim | 9 +++++++++ 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/libp2p/switch.nim b/libp2p/switch.nim index 3bbdaa6..93f2b5f 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 3d47b1a..608266c 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