Compare commits

...

2 Commits

Author SHA1 Message Date
Diego
1342b45b29 handle CancelledError 2024-03-20 11:04:50 +01:00
Diego
a65ce6a47a fix: sendMsg must not raise errors 2024-03-19 22:36:17 +01:00

View File

@@ -288,22 +288,28 @@ proc sendMsgSlow(p: PubSubPeer, msg: seq[byte]) {.async.} =
if p.sendConn == nil:
# Wait for a send conn to be setup. `connectOnce` will
# complete this even if the sendConn setup failed
await p.connectedFut
try:
await p.connectedFut
except CatchableError as exc:
debug "Error when waiting for a send conn to be setup", msg = exc.msg, p
var conn = p.sendConn
if conn == nil or conn.closed():
debug "No send connection", p, msg = shortLog(msg)
debug "No send connection", msg = shortLog(msg), p
return
trace "sending encoded msg to peer", conn, encoded = shortLog(msg)
await sendMsgContinue(conn, conn.writeLp(msg))
trace "sending encoded msg to peer", conn, encoded = shortLog(msg), p
try:
await sendMsgContinue(conn, conn.writeLp(msg))
except CancelledError as exc:
trace "Continuation for pending `sendMsg` future has been unexpectedly cancelled"
proc sendMsg(p: PubSubPeer, msg: seq[byte]): Future[void] =
if p.sendConn != nil and not p.sendConn.closed():
# Fast path that avoids copying msg (which happens for {.async.})
let conn = p.sendConn
trace "sending encoded msg to peer", conn, encoded = shortLog(msg)
trace "sending encoded msg to peer", conn, encoded = shortLog(msg), p
let f = conn.writeLp(msg)
if not f.completed():
sendMsgContinue(conn, f)