Skip to content

Commit 9059a8a

Browse files
authored
use race instead of or to avoid lockup (#1042)
1 parent 0b753e7 commit 9059a8a

File tree

4 files changed

+14
-4
lines changed

4 files changed

+14
-4
lines changed

libp2p/muxers/yamux/yamux.nim

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,9 @@ method readOnce*(
266266
raise newLPStreamRemoteClosedError()
267267
if channel.recvQueue.len == 0:
268268
channel.receivedData.clear()
269-
await channel.closedRemotely or channel.receivedData.wait()
269+
try: # https://github.com/status-im/nim-chronos/issues/516
270+
discard await race(channel.closedRemotely, channel.receivedData.wait())
271+
except ValueError: raiseAssert("Futures list is not empty")
270272
if channel.closedRemotely.done() and channel.recvQueue.len == 0:
271273
channel.returnedEof = true
272274
channel.isEof = true

libp2p/protocols/connectivity/relay/utils.nim

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ proc bridge*(connSrc: Connection, connDst: Connection) {.async.} =
5555

5656
try:
5757
while not connSrc.closed() and not connDst.closed():
58-
await futSrc or futDst
58+
try: # https://github.com/status-im/nim-chronos/issues/516
59+
discard await race(futSrc, futDst)
60+
except ValueError: raiseAssert("Futures list is not empty")
5961
if futSrc.finished():
6062
bufRead = await futSrc
6163
if bufRead > 0:

libp2p/protocols/secure/secure.nim

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,10 @@ proc handleConn(s: Secure,
9797
let
9898
fut1 = conn.join()
9999
fut2 = sconn.join()
100-
await fut1 or fut2 # one join() completes, cancel outstanding join()
100+
try: # https://github.com/status-im/nim-chronos/issues/516
101+
discard await race(fut1, fut2)
102+
except ValueError: raiseAssert("Futures list is not empty")
103+
# at least one join() completed, cancel pending one, if any
101104
if not fut1.finished: await fut1.cancelAndWait()
102105
if not fut2.finished: await fut2.cancelAndWait()
103106
block:

libp2p/transports/tcptransport.nim

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ proc connHandler*(self: TcpTransport,
6868
let
6969
fut1 = client.join()
7070
fut2 = conn.join()
71-
await fut1 or fut2 # one join() completes, cancel outstanding join()
71+
try: # https://github.com/status-im/nim-chronos/issues/516
72+
discard await race(fut1, fut2)
73+
except ValueError: raiseAssert("Futures list is not empty")
74+
# at least one join() completed, cancel pending one, if any
7275
if not fut1.finished: await fut1.cancelAndWait()
7376
if not fut2.finished: await fut2.cancelAndWait()
7477

0 commit comments

Comments
 (0)