From 114680453f789f947a3c057bbeb5bdf593f5364b Mon Sep 17 00:00:00 2001 From: Jacek Sieka Date: Tue, 17 Nov 2020 15:36:41 +0100 Subject: [PATCH] wait for cancellation to be processed on timeout (#311) --- eth/async_utils.nim | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/eth/async_utils.nim b/eth/async_utils.nim index 8882de0..3f418af 100644 --- a/eth/async_utils.nim +++ b/eth/async_utils.nim @@ -1,5 +1,5 @@ import - chronos, chronicles, chronicles/chronos_tools + chronos, chronicles/chronos_tools export chronos_tools @@ -10,7 +10,10 @@ template awaitWithTimeout*[T](operation: Future[T], let f = operation await f or deadline if not f.finished: - cancel f + # If we don't wait for for the cancellation here, it's possible that + # the "next" operation will run concurrently to this one, messing up + # the order of operations (since await/async is not fair) + await cancelAndWait(f) onTimeout else: f.read @@ -19,4 +22,3 @@ template awaitWithTimeout*[T](operation: Future[T], timeout: Duration, onTimeout: untyped): T = awaitWithTimeout(operation, sleepAsync(timeout), onTimeout) -