Skip to content

Commit

Permalink
Fix cancellation behavior.
Browse files Browse the repository at this point in the history
  • Loading branch information
cheatfate committed Aug 8, 2023
1 parent 9f5c1c5 commit d0dca33
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions chronos/apps/http/httpserver.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit d0dca33

Please sign in to comment.