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: finally:
upgrades.release() 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 proc accept(s: Switch, transport: Transport) {.async.} = # noraises
## switch accept loop, ran for every transport ## switch accept loop, ran for every transport
## ##
@ -263,11 +268,12 @@ proc accept(s: Switch, transport: Transport) {.async.} = # noraises
trace "releasing semaphore on cancellation" trace "releasing semaphore on cancellation"
upgrades.release() # always release the slot upgrades.release() # always release the slot
return return
except TransportUseClosedError:
await accept_cleanup(upgrades, conn)
return
except CatchableError as exc: except CatchableError as exc:
error "Exception in accept loop, exiting", exc = exc.msg error "Exception in accept loop, exiting", exc = exc.msg
upgrades.release() # always release the slot await accept_cleanup(upgrades, conn)
if not isNil(conn):
await conn.close()
return return
proc stop*(s: Switch) {.async, public.} = proc stop*(s: Switch) {.async, public.} =