mirror of
https://github.com/codex-storage/nim-websock.git
synced 2025-02-13 02:46:31 +00:00
Fix closing hangs (#131)
A peer can make us wait forever when closing a connection. This PR instead tries to be nice and wait for him, but kills the connection after a while
This commit is contained in:
parent
cf8b8ce235
commit
691f069b20
@ -249,7 +249,10 @@ proc handleClose*(
|
|||||||
if ws.readyState != ReadyState.Closing:
|
if ws.readyState != ReadyState.Closing:
|
||||||
ws.readyState = ReadyState.Closing
|
ws.readyState = ReadyState.Closing
|
||||||
trace "Sending close", code = ord(code), reason
|
trace "Sending close", code = ord(code), reason
|
||||||
await ws.send(prepareCloseBody(code, reason), Opcode.Close)
|
try:
|
||||||
|
await ws.send(prepareCloseBody(code, reason), Opcode.Close).wait(5.seconds)
|
||||||
|
except CatchableError as exc:
|
||||||
|
trace "Failed to send Close opcode", err=exc.msg
|
||||||
|
|
||||||
ws.readyState = ReadyState.Closed
|
ws.readyState = ReadyState.Closed
|
||||||
|
|
||||||
@ -502,10 +505,9 @@ proc close*(
|
|||||||
if ws.readyState != ReadyState.Open:
|
if ws.readyState != ReadyState.Open:
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
proc gentleCloser(ws: WSSession, closeBody: seq[byte]) {.async.} =
|
||||||
ws.readyState = ReadyState.Closing
|
|
||||||
await ws.send(
|
await ws.send(
|
||||||
prepareCloseBody(code, reason),
|
closeBody,
|
||||||
opcode = Opcode.Close)
|
opcode = Opcode.Close)
|
||||||
|
|
||||||
# read frames until closed
|
# read frames until closed
|
||||||
@ -513,7 +515,12 @@ proc close*(
|
|||||||
while ws.readyState != ReadyState.Closed:
|
while ws.readyState != ReadyState.Closed:
|
||||||
discard await ws.readFrame()
|
discard await ws.readFrame()
|
||||||
except CatchableError as exc:
|
except CatchableError as exc:
|
||||||
ws.readyState = ReadyState.Closed
|
discard # most likely EOF
|
||||||
await ws.stream.closeWait()
|
try:
|
||||||
|
ws.readyState = ReadyState.Closing
|
||||||
|
await gentleCloser(ws, prepareCloseBody(code, reason)).wait(10.seconds)
|
||||||
except CatchableError as exc:
|
except CatchableError as exc:
|
||||||
trace "Exception closing", exc = exc.msg
|
trace "Exception closing", exc = exc.msg
|
||||||
|
finally:
|
||||||
|
await ws.stream.closeWait()
|
||||||
|
ws.readyState = ReadyState.Closed
|
||||||
|
Loading…
x
Reference in New Issue
Block a user