From e5e695c39602e85fcbba7288ca97a69a06f992e0 Mon Sep 17 00:00:00 2001 From: Jacek Sieka Date: Tue, 16 Jan 2024 18:12:47 +0100 Subject: [PATCH] better async timeout wait (#659) * don't call timeout code if future finishes before getting cancelled * avoid extra raises effect resulting from `read` (vs `await`) --- eth/async_utils.nim | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/eth/async_utils.nim b/eth/async_utils.nim index 8d5c248..e06d418 100644 --- a/eth/async_utils.nim +++ b/eth/async_utils.nim @@ -14,9 +14,11 @@ template awaitWithTimeout*[T](operation: Future[T], # the "next" operation will run concurrently to this one, messing up # the order of operations (since await/async is not fair) await cancelAndWait(f) + + if f.cancelled: # It could have finished instead of getting cancelled onTimeout else: - f.read + await f # Await avoids extraneous exception effects template awaitWithTimeout*[T](operation: Future[T], timeout: Duration, @@ -33,10 +35,11 @@ template awaitWithTimeout*(operation: Future[void], # the "next" operation will run concurrently to this one, messing up # the order of operations (since await/async is not fair) await cancelAndWait(f) + + if f.cancelled: # It could have finished instead of getting cancelled onTimeout template awaitWithTimeout*(operation: Future[void], timeout: Duration, onTimeout: untyped) = awaitWithTimeout(operation, sleepAsync(timeout), onTimeout) -