From e45c6ede93eb8a86ce2045739bfdcc5bce948c45 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lex=20Cabeza=20Romero?= Date: Mon, 11 Mar 2024 17:10:40 +0100 Subject: [PATCH 1/2] Handle graceful shutdown on TransportUseClosedError. --- libp2p/switch.nim | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/libp2p/switch.nim b/libp2p/switch.nim index d6ea195cb9..61c099bd98 100644 --- a/libp2p/switch.nim +++ b/libp2p/switch.nim @@ -223,6 +223,11 @@ proc upgradeMonitor( finally: upgrades.release() +proc accept_cleanup(upgrades: AsyncSemaphore, connection: Connection) {.async.} = + upgrades.release() # always release the slot + if not isNil(connection): + await connection.close() + proc accept(s: Switch, transport: Transport) {.async.} = # noraises ## switch accept loop, ran for every transport ## @@ -263,11 +268,12 @@ proc accept(s: Switch, transport: Transport) {.async.} = # noraises trace "releasing semaphore on cancellation" upgrades.release() # always release the slot return + except TransportUseClosedError: + await accept_cleanup(upgrades, conn) + return except CatchableError as exc: error "Exception in accept loop, exiting", exc = exc.msg - upgrades.release() # always release the slot - if not isNil(conn): - await conn.close() + await accept_cleanup(upgrades, conn) return proc stop*(s: Switch) {.async, public.} = From 453bcc6f92712fae7b73f810c431d4ad63c5ce88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lex=20Cabeza=20Romero?= Date: Fri, 15 Mar 2024 16:39:35 +0100 Subject: [PATCH 2/2] Update with PR suggestions. --- libp2p/switch.nim | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/libp2p/switch.nim b/libp2p/switch.nim index 61c099bd98..bc225e6966 100644 --- a/libp2p/switch.nim +++ b/libp2p/switch.nim @@ -223,7 +223,7 @@ proc upgradeMonitor( finally: upgrades.release() -proc accept_cleanup(upgrades: AsyncSemaphore, connection: Connection) {.async.} = +proc acceptCleanup(upgrades: AsyncSemaphore, connection: Connection) {.async.} = upgrades.release() # always release the slot if not isNil(connection): await connection.close() @@ -269,11 +269,12 @@ proc accept(s: Switch, transport: Transport) {.async.} = # noraises upgrades.release() # always release the slot return except TransportUseClosedError: - await accept_cleanup(upgrades, conn) + trace "Graceful shutdown in accept loop, exiting" + await acceptCleanup(upgrades, conn) return except CatchableError as exc: error "Exception in accept loop, exiting", exc = exc.msg - await accept_cleanup(upgrades, conn) + await acceptCleanup(upgrades, conn) return proc stop*(s: Switch) {.async, public.} =