Handle graceful shutdown on TransportUseClosedError.

This commit is contained in:
Álex Cabeza Romero 2024-03-11 17:10:40 +01:00 committed by Diego
parent 100f3188ed
commit e45c6ede93
No known key found for this signature in database
GPG Key ID: C9DAC9BF68D1F806
1 changed files with 9 additions and 3 deletions

View File

@ -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.} =