From d0dca336df74473f12d0239b61f09bb683aae771 Mon Sep 17 00:00:00 2001 From: cheatfate Date: Wed, 9 Aug 2023 02:38:17 +0300 Subject: [PATCH] Fix cancellation behavior. --- chronos/apps/http/httpserver.nim | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/chronos/apps/http/httpserver.nim b/chronos/apps/http/httpserver.nim index 816684cff..10ad6bb16 100644 --- a/chronos/apps/http/httpserver.nim +++ b/chronos/apps/http/httpserver.nim @@ -1010,8 +1010,6 @@ proc getConnectionFence*(server: HttpServerRef, proc processRequest(server: HttpServerRef, connection: HttpConnectionRef, connId: string): Future[HttpProcessExitType] {.async.} = - # This procedure is cancellation-safe all the ancestors transforming - # CancelledError into appropriate error code. let requestFence = await getRequestFence(server, connection) if requestFence.isErr(): case requestFence.error.kind @@ -1060,12 +1058,17 @@ proc processLoop(holder: HttpConnectionHolderRef) {.async.} = var runLoop = HttpProcessExitType.KeepAlive while runLoop == HttpProcessExitType.KeepAlive: - runLoop = await server.processRequest(connection, connectionId) + runLoop = + try: + await server.processRequest(connection, connectionId) + except CancelledError: + HttpProcessExitType.Immediate + except CatchableError as exc: + raiseAssert "Unexpected error [" & exc.name & "] happens: " & exc.msg server.connections.del(connectionId) case runLoop of HttpProcessExitType.KeepAlive: - # This could happened only on CancelledError. await connection.closeWait() of HttpProcessExitType.Immediate: await connection.closeWait()