summary refs log tree commit diff stats
path: root/lib
diff options
context:
space:
mode:
authorRay Imber <rayimber@gmail.com>2020-06-30 06:23:53 -0700
committerGitHub <noreply@github.com>2020-06-30 15:23:53 +0200
commit36fa79a5242d2e39a83083cd66cf3af9f8931f3b (patch)
treedfb8b8fc2836f7667db2fa4e34c25b9464f2387f /lib
parent8be54b8fa7f24fe2886de95c4a2bd356dbae5aa9 (diff)
downloadNim-36fa79a5242d2e39a83083cd66cf3af9f8931f3b.tar.gz
Fix asyncdispatch drain behavior (#14820) (#14838)
* Fix asyncdispatch drain behavior (#14820)

* Changed test to use asyncCheck instead of discard after code review (#14820)

* Added some debug statements to help understand what is happening in Azure.

* Removed debug statements and increased timeouts by 1 order of magnitude to account for slow Azure VMs

Co-authored-by: Ray Imber <ray@crankuptheamps.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/pure/asyncdispatch.nim13
1 files changed, 9 insertions, 4 deletions
diff --git a/lib/pure/asyncdispatch.nim b/lib/pure/asyncdispatch.nim
index a15442109..a0bc96e72 100644
--- a/lib/pure/asyncdispatch.nim
+++ b/lib/pure/asyncdispatch.nim
@@ -1601,11 +1601,16 @@ else:
     p.selector.registerEvent(SelectEvent(ev), data)
 
 proc drain*(timeout = 500) =
-  ## Waits for completion events and processes them. Raises ``ValueError``
+  ## 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.
-  if runOnce(timeout) or hasPendingOperations():
-    while hasPendingOperations() and runOnce(timeout): discard
+  ## processes as many events as are available until the timeout has elapsed.
+  var curTimeout = timeout
+  let start = now()
+  while hasPendingOperations():
+    discard runOnce(curTimeout) 
+    curTimeout -= (now() - start).inMilliseconds.int
+    if curTimeout < 0:
+      break
 
 proc poll*(timeout = 500) =
   ## Waits for completion events and processes them. Raises ``ValueError``