Handle trying to write empty byte seq (#780)

This commit is contained in:
diegomrsantos 2022-09-29 20:02:10 +02:00 committed by GitHub
parent 0cd3554ce4
commit 4f18dd30e9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 11 additions and 6 deletions

View File

@ -64,12 +64,14 @@ proc bridge*(connSrc: Connection, connDst: Connection) {.async.} =
await futSrc or futDst await futSrc or futDst
if futSrc.finished(): if futSrc.finished():
bufRead = await futSrc bufRead = await futSrc
if bufRead > 0:
bytesSendFromSrcToDst.inc(bufRead) bytesSendFromSrcToDst.inc(bufRead)
await connDst.write(@bufSrcToDst[0..<bufRead]) await connDst.write(@bufSrcToDst[0..<bufRead])
zeroMem(addr(bufSrcToDst), bufSrcToDst.high + 1) zeroMem(addr(bufSrcToDst), bufSrcToDst.high + 1)
futSrc = connSrc.readOnce(addr bufSrcToDst[0], bufSrcToDst.high + 1) futSrc = connSrc.readOnce(addr bufSrcToDst[0], bufSrcToDst.high + 1)
if futDst.finished(): if futDst.finished():
bufRead = await futDst bufRead = await futDst
if bufRead > 0:
bytesSendFromDstToSrc += bufRead bytesSendFromDstToSrc += bufRead
await connSrc.write(bufDstToSrc[0..<bufRead]) await connSrc.write(bufDstToSrc[0..<bufRead])
zeroMem(addr(bufDstToSrc), bufDstToSrc.high + 1) zeroMem(addr(bufDstToSrc), bufDstToSrc.high + 1)

View File

@ -130,6 +130,9 @@ proc completeWrite(
method write*(s: ChronosStream, msg: seq[byte]): Future[void] = method write*(s: ChronosStream, msg: seq[byte]): Future[void] =
# Avoid a copy of msg being kept in the closure created by `{.async.}` as this # Avoid a copy of msg being kept in the closure created by `{.async.}` as this
# drives up memory usage # drives up memory usage
if msg.len == 0:
trace "Empty byte seq, nothing to write"
return
if s.closed: if s.closed:
let fut = newFuture[void]("chronosstream.write.closed") let fut = newFuture[void]("chronosstream.write.closed")
fut.fail(newLPStreamClosedError()) fut.fail(newLPStreamClosedError())