Skip to content

Commit

Permalink
use race instead of or to avoid lockup (#1042)
Browse files Browse the repository at this point in the history
  • Loading branch information
etan-status committed Mar 3, 2024
1 parent 0b753e7 commit 9059a8a
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 4 deletions.
4 changes: 3 additions & 1 deletion libp2p/muxers/yamux/yamux.nim
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,9 @@ method readOnce*(
raise newLPStreamRemoteClosedError()
if channel.recvQueue.len == 0:
channel.receivedData.clear()
await channel.closedRemotely or channel.receivedData.wait()
try: # https://github.com/status-im/nim-chronos/issues/516
discard await race(channel.closedRemotely, channel.receivedData.wait())
except ValueError: raiseAssert("Futures list is not empty")
if channel.closedRemotely.done() and channel.recvQueue.len == 0:
channel.returnedEof = true
channel.isEof = true
Expand Down
4 changes: 3 additions & 1 deletion libp2p/protocols/connectivity/relay/utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ proc bridge*(connSrc: Connection, connDst: Connection) {.async.} =

try:
while not connSrc.closed() and not connDst.closed():
await futSrc or futDst
try: # https://github.com/status-im/nim-chronos/issues/516
discard await race(futSrc, futDst)
except ValueError: raiseAssert("Futures list is not empty")
if futSrc.finished():
bufRead = await futSrc
if bufRead > 0:
Expand Down
5 changes: 4 additions & 1 deletion libp2p/protocols/secure/secure.nim
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ proc handleConn(s: Secure,
let
fut1 = conn.join()
fut2 = sconn.join()
await fut1 or fut2 # one join() completes, cancel outstanding join()
try: # https://github.com/status-im/nim-chronos/issues/516
discard await race(fut1, fut2)
except ValueError: raiseAssert("Futures list is not empty")
# at least one join() completed, cancel pending one, if any
if not fut1.finished: await fut1.cancelAndWait()
if not fut2.finished: await fut2.cancelAndWait()
block:
Expand Down
5 changes: 4 additions & 1 deletion libp2p/transports/tcptransport.nim
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ proc connHandler*(self: TcpTransport,
let
fut1 = client.join()
fut2 = conn.join()
await fut1 or fut2 # one join() completes, cancel outstanding join()
try: # https://github.com/status-im/nim-chronos/issues/516
discard await race(fut1, fut2)
except ValueError: raiseAssert("Futures list is not empty")
# at least one join() completed, cancel pending one, if any
if not fut1.finished: await fut1.cancelAndWait()
if not fut2.finished: await fut2.cancelAndWait()

Expand Down

0 comments on commit 9059a8a

Please sign in to comment.