Skip to content

Commit

Permalink
better async timeout wait (#659)
Browse files Browse the repository at this point in the history
* don't call timeout code if future finishes before getting cancelled
* avoid extra raises effect resulting from `read` (vs `await`)
  • Loading branch information
arnetheduck authored Jan 16, 2024
1 parent e52f5fa commit e5e695c
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions eth/async_utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)

0 comments on commit e5e695c

Please sign in to comment.