mirror of https://github.com/vacp2p/nim-libp2p.git
Handle trying to write empty byte seq (#780)
This commit is contained in:
parent
0cd3554ce4
commit
4f18dd30e9
|
@ -64,15 +64,17 @@ 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
|
||||||
bytesSendFromSrcToDst.inc(bufRead)
|
if bufRead > 0:
|
||||||
await connDst.write(@bufSrcToDst[0..<bufRead])
|
bytesSendFromSrcToDst.inc(bufRead)
|
||||||
zeroMem(addr(bufSrcToDst), bufSrcToDst.high + 1)
|
await connDst.write(@bufSrcToDst[0..<bufRead])
|
||||||
|
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
|
||||||
bytesSendFromDstToSrc += bufRead
|
if bufRead > 0:
|
||||||
await connSrc.write(bufDstToSrc[0..<bufRead])
|
bytesSendFromDstToSrc += bufRead
|
||||||
zeroMem(addr(bufDstToSrc), bufDstToSrc.high + 1)
|
await connSrc.write(bufDstToSrc[0..<bufRead])
|
||||||
|
zeroMem(addr(bufDstToSrc), bufDstToSrc.high + 1)
|
||||||
futDst = connDst.readOnce(addr bufDstToSrc[0], bufDstToSrc.high + 1)
|
futDst = connDst.readOnce(addr bufDstToSrc[0], bufDstToSrc.high + 1)
|
||||||
except CancelledError as exc:
|
except CancelledError as exc:
|
||||||
raise exc
|
raise exc
|
||||||
|
|
|
@ -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())
|
||||||
|
|
Loading…
Reference in New Issue