From 55cc5434fe4d5183fd912932e4cb926f92d03d87 Mon Sep 17 00:00:00 2001 From: Ludovic Chenut Date: Mon, 12 Aug 2024 19:21:16 +0200 Subject: [PATCH] fix(yamux): future leak (#1171) --- libp2p/muxers/yamux/yamux.nim | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/libp2p/muxers/yamux/yamux.nim b/libp2p/muxers/yamux/yamux.nim index 667f60fba..10ad33773 100644 --- a/libp2p/muxers/yamux/yamux.nim +++ b/libp2p/muxers/yamux/yamux.nim @@ -279,10 +279,15 @@ method readOnce*( raise newLPStreamRemoteClosedError() if channel.recvQueue.len == 0: channel.receivedData.clear() - try: # https://github.com/status-im/nim-chronos/issues/516 - discard await race(channel.closedRemotely.wait(), channel.receivedData.wait()) - except ValueError: - raiseAssert("Futures list is not empty") + let + closedRemotelyFut = channel.closedRemotely.wait() + receivedDataFut = channel.receivedData.wait() + defer: + if not closedRemotelyFut.finished(): + await closedRemotelyFut.cancelAndWait() + if not receivedDataFut.finished(): + await receivedDataFut.cancelAndWait() + await closedRemotelyFut or receivedDataFut if channel.closedRemotely.isSet() and channel.recvQueue.len == 0: channel.isEof = true return