Skip to content

Commit 3ca49a2

Browse files
fix(transport): various tcp transport races (#1095)
Co-authored-by: diegomrsantos <diego@status.im>
1 parent 1b91b97 commit 3ca49a2

File tree

7 files changed

+280
-226
lines changed

7 files changed

+280
-226
lines changed

libp2p/dialer.nim

+7-5
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,18 @@ proc dialAndUpgrade(
8181
if dialed.dir != dir:
8282
dialed.dir = dir
8383
await transport.upgrade(dialed, peerId)
84+
except CancelledError as exc:
85+
await dialed.close()
86+
raise exc
8487
except CatchableError as exc:
8588
# If we failed to establish the connection through one transport,
8689
# we won't succeeded through another - no use in trying again
8790
await dialed.close()
8891
debug "Connection upgrade failed", err = exc.msg, peerId = peerId.get(default(PeerId))
89-
if exc isnot CancelledError:
90-
if dialed.dir == Direction.Out:
91-
libp2p_failed_upgrades_outgoing.inc()
92-
else:
93-
libp2p_failed_upgrades_incoming.inc()
92+
if dialed.dir == Direction.Out:
93+
libp2p_failed_upgrades_outgoing.inc()
94+
else:
95+
libp2p_failed_upgrades_incoming.inc()
9496

9597
# Try other address
9698
return nil

libp2p/errors.nim

-9
Original file line numberDiff line numberDiff line change
@@ -44,12 +44,3 @@ macro checkFutures*[F](futs: seq[F], exclude: untyped = []): untyped =
4444
# We still don't abort but warn
4545
debug "A future has failed, enable trace logging for details", error=exc.name
4646
trace "Exception details", msg=exc.msg
47-
48-
template tryAndWarn*(message: static[string]; body: untyped): untyped =
49-
try:
50-
body
51-
except CancelledError as exc:
52-
raise exc
53-
except CatchableError as exc:
54-
debug "An exception has ocurred, enable trace logging for details", name = exc.name, msg = message
55-
trace "Exception details", exc = exc.msg

libp2p/switch.nim

+7-12
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,7 @@ proc accept(s: Switch, transport: Transport) {.async.} = # noraises
273273
except CancelledError as exc:
274274
trace "releasing semaphore on cancellation"
275275
upgrades.release() # always release the slot
276+
return
276277
except CatchableError as exc:
277278
error "Exception in accept loop, exiting", exc = exc.msg
278279
upgrades.release() # always release the slot
@@ -288,6 +289,12 @@ proc stop*(s: Switch) {.async, public.} =
288289

289290
s.started = false
290291

292+
try:
293+
# Stop accepting incoming connections
294+
await allFutures(s.acceptFuts.mapIt(it.cancelAndWait())).wait(1.seconds)
295+
except CatchableError as exc:
296+
debug "Cannot cancel accepts", error = exc.msg
297+
291298
for service in s.services:
292299
discard await service.stop(s)
293300

@@ -302,18 +309,6 @@ proc stop*(s: Switch) {.async, public.} =
302309
except CatchableError as exc:
303310
warn "error cleaning up transports", msg = exc.msg
304311

305-
try:
306-
await allFutures(s.acceptFuts)
307-
.wait(1.seconds)
308-
except CatchableError as exc:
309-
trace "Exception while stopping accept loops", exc = exc.msg
310-
311-
# check that all futures were properly
312-
# stopped and otherwise cancel them
313-
for a in s.acceptFuts:
314-
if not a.finished:
315-
a.cancel()
316-
317312
for service in s.services:
318313
discard await service.stop(s)
319314

0 commit comments

Comments
 (0)