Handle cancellation in timeoutMonitor (#283)

* Handle cancellation in timeoutMonitor

* refactor lpchannel timeout as suggested by cheatfate
This commit is contained in:
Giovanni Petrantoni 2020-07-22 00:03:41 +09:00 committed by GitHub
parent 3b088f8980
commit c3404f6eea
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 6 additions and 12 deletions

View File

@ -57,7 +57,6 @@ type
msgCode*: MessageType # cached in/out message code msgCode*: MessageType # cached in/out message code
closeCode*: MessageType # cached in/out close code closeCode*: MessageType # cached in/out close code
resetCode*: MessageType # cached in/out reset code resetCode*: MessageType # cached in/out reset code
timerFut: Future[void] # the current timer instanse
timerTaskFut: Future[void] # the current timer instanse timerTaskFut: Future[void] # the current timer instanse
proc open*(s: LPChannel) {.async, gcsafe.} proc open*(s: LPChannel) {.async, gcsafe.}
@ -82,11 +81,8 @@ template withEOFExceptions(body: untyped): untyped =
proc cleanupTimer(s: LPChannel) {.async.} = proc cleanupTimer(s: LPChannel) {.async.} =
## cleanup timers ## cleanup timers
## if not s.timerTaskFut.finished:
if not(isNil(s.timerFut)) and await s.timerTaskFut.cancelAndWait()
not(s.timerFut.finished):
s.timerFut.cancel()
await s.timerTaskFut
proc closeMessage(s: LPChannel) {.async.} = proc closeMessage(s: LPChannel) {.async.} =
logScope: logScope:
@ -248,13 +244,10 @@ proc timeoutMonitor(s: LPChannel) {.async.} =
## be reset ## be reset
## ##
if not(isNil(s.timerFut)):
return
try: try:
while true: while true:
s.timerFut = sleepAsync(s.timeout) await sleepAsync(s.timeout)
await s.timerFut
if s.closed or s.atEof: if s.closed or s.atEof:
return return
@ -267,6 +260,8 @@ proc timeoutMonitor(s: LPChannel) {.async.} =
# reset channel on innactivity timeout # reset channel on innactivity timeout
trace "channel timed out, resetting" trace "channel timed out, resetting"
await s.reset() await s.reset()
except CancelledError as exc:
raise exc
except CatchableError as exc: except CatchableError as exc:
trace "exception in timeout", exc = exc.msg trace "exception in timeout", exc = exc.msg
@ -328,7 +323,6 @@ proc init*(
await conn.writeMsg(chann.id, await conn.writeMsg(chann.id,
chann.msgCode, chann.msgCode,
data) data)
except CatchableError as exc: except CatchableError as exc:
trace "exception in lpchannel write handler", exc = exc.msg trace "exception in lpchannel write handler", exc = exc.msg
await chann.reset() await chann.reset()