From bbf6a62c90ce326529cf675398bc5498cfd10f79 Mon Sep 17 00:00:00 2001 From: Tomohiro Date: Wed, 4 Dec 2024 23:11:32 +0900 Subject: [PATCH] fixes #24506; calculate timeout correctly (#24507) `curTimeout` is calculated incorrectly. So this PR fixes it. This PR also replaces `now()` with `getMonoTime()`. --- lib/pure/asyncdispatch.nim | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/pure/asyncdispatch.nim b/lib/pure/asyncdispatch.nim index b7ace0b3e3..b3a26f303c 100644 --- a/lib/pure/asyncdispatch.nim +++ b/lib/pure/asyncdispatch.nim @@ -1696,12 +1696,12 @@ proc drain*(timeout = 500) = ## Waits for completion of **all** events and processes them. Raises `ValueError` ## if there are no pending operations. In contrast to `poll` this ## processes as many events as are available until the timeout has elapsed. - var curTimeout = timeout - let start = now() + var elapsed = 0 + let start = getMonoTime() while hasPendingOperations(): - discard runOnce(curTimeout) - curTimeout -= (now() - start).inMilliseconds.int - if curTimeout < 0: + discard runOnce(timeout - elapsed) + elapsed = (getMonoTime() - start).inMilliseconds + if elapsed >= timeout: break proc poll*(timeout = 500) =