From 92cb8b7c927b9809e5a91e95be0d6a1774032802 Mon Sep 17 00:00:00 2001 From: Mihail Stoykov Date: Thu, 11 Jan 2024 19:32:01 +0200 Subject: [PATCH] Don't block the event loop on context end Previously setTimeout (and co) might block the event loop in cases where timeout hasn't triggered but the context has been done. --- timers/timers.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/timers/timers.go b/timers/timers.go index 971024a..6ff7621 100644 --- a/timers/timers.go +++ b/timers/timers.go @@ -212,10 +212,13 @@ func (e *Timers) closeTaskQueue() { // so that we do not execute it twice e.taskQueueCh = nil - // wait for this to happen so we don't need to hit the event loop again - // instead this just closes the queue - ch <- struct{}{} - <-ch + select { + case ch <- struct{}{}: + // wait for this to happen so we don't need to hit the event loop again + // instead this just closes the queue + <-ch + case <-e.vu.Context().Done(): // still shortcircuit if the context is done as we might block otherwise + } } func (e *Timers) setupTaskQueueCloserOnIterationEnd() {