Handle cancellation in close (#143)

* Handle cancellation in close

* Using a future instead of sleepAsync
This commit is contained in:
diegomrsantos 2023-06-06 22:57:40 +02:00 committed by GitHub
parent 062fd132a5
commit 3696e3f3a5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 0 deletions

View File

@ -170,6 +170,37 @@ suite "Test transmission":
check string.fromBytes(clientRes) == testString
await waitForClose(session)
asyncTest "Close handle cancellation":
let testString = "Hello!"
let cancelSignal = newFuture[void]()
proc handle(request: HttpRequest) {.async.} =
check request.uri.path == WSPath
let server = WSServer.new(protos = ["proto"])
let ws = await server.handleRequest(request)
let servRes = await ws.recvMsg()
check string.fromBytes(servRes) == testString
await ws.waitForClose()
server = createServer(
address = address,
handler = handle,
flags = {ReuseAddr})
proc client() {.async, gcsafe.} =
let session = await connectClient()
await session.send(testString)
cancelSignal.complete()
expect CancelledError:
await session.close()
let task = client()
await cancelSignal
await task.cancelAndWait()
suite "Test ping-pong":
setup:
var

View File

@ -518,11 +518,16 @@ proc close*(
try:
while ws.readyState != ReadyState.Closed:
discard await ws.readFrame()
except CancelledError as exc:
raise exc
except CatchableError as exc:
discard # most likely EOF
try:
ws.readyState = ReadyState.Closing
await gentleCloser(ws, prepareCloseBody(code, reason)).wait(10.seconds)
except CancelledError as exc:
trace "Cancellation when closing!", exc = exc.msg
raise exc
except CatchableError as exc:
trace "Exception closing", exc = exc.msg
finally: