properly exit readLoop on error/stream end

This commit is contained in:
Dmitriy Ryajov 2019-09-28 13:55:48 -06:00
parent 34d1a641de
commit 968fd85d49

View File

@ -402,15 +402,21 @@ proc handshake*(s: Secio, conn: Connection): Future[SecureConnection] {.async.}
proc readLoop(sconn: SecureConnection, stream: BufferStream) {.async.} = proc readLoop(sconn: SecureConnection, stream: BufferStream) {.async.} =
while not sconn.conn.closed: while not sconn.conn.closed:
let msg = await sconn.readMessage() try:
await stream.pushTo(msg) let msg = await sconn.readMessage()
await stream.pushTo(msg)
except:
trace "exception in secio", exc = getCurrentExceptionMsg()
return
finally:
trace "ending secio readLoop"
proc handleConn(s: Secio, conn: Connection): Future[Connection] {.async.} = proc handleConn(s: Secio, conn: Connection): Future[Connection] {.async.} =
var sconn = await s.handshake(conn) var sconn = await s.handshake(conn)
proc writeHandler(data: seq[byte]) {.async, gcsafe.} = proc writeHandler(data: seq[byte]) {.async, gcsafe.} =
debug "sending encrypted bytes", bytes = data.toHex() debug "sending encrypted bytes", bytes = data.toHex()
await sconn.writeMessage(data) await sconn.writeMessage(data)
var stream = newBufferStream(writeHandler) var stream = newBufferStream(writeHandler)
asyncCheck readLoop(sconn, stream) asyncCheck readLoop(sconn, stream)
var secured = newConnection(stream) var secured = newConnection(stream)
@ -419,9 +425,9 @@ proc handleConn(s: Secio, conn: Connection): Future[Connection] {.async.} =
method init(s: Secio) {.gcsafe.} = method init(s: Secio) {.gcsafe.} =
proc handle(conn: Connection, proto: string) {.async, gcsafe.} = proc handle(conn: Connection, proto: string) {.async, gcsafe.} =
debug "handling connection" trace "handling connection"
discard await s.handleConn(conn) discard await s.handleConn(conn)
debug "connection secured" trace "connection secured"
s.codec = SecioCodec s.codec = SecioCodec
s.handler = handle s.handler = handle