From aa04bcc984f848b527f50ab83c48735deb7b4292 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Tue, 28 Jan 2025 17:28:38 -0800 Subject: [PATCH] Drain microtasks again after deferred tasks run --- src/bun.js/event_loop.zig | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/bun.js/event_loop.zig b/src/bun.js/event_loop.zig index 242103874ec0ca..8883490be6ebac 100644 --- a/src/bun.js/event_loop.zig +++ b/src/bun.js/event_loop.zig @@ -743,9 +743,10 @@ pub const DeferredTaskQueue = struct { return this.map.swapRemove(ctx); } - pub fn run(this: *DeferredTaskQueue) void { + pub fn run(this: *DeferredTaskQueue) bool { var i: usize = 0; var last = this.map.count(); + const has_any = last > 0; while (i < last) { const key = this.map.keys()[i] orelse { this.map.swapRemoveAt(i); @@ -760,6 +761,8 @@ pub const DeferredTaskQueue = struct { i += 1; } } + + return has_any; } pub fn deinit(this: *DeferredTaskQueue) void { @@ -877,7 +880,10 @@ pub const EventLoop = struct { jsc_vm.releaseWeakRefs(); JSC__JSGlobalObject__drainMicrotasks(globalObject); - this.deferred_tasks.run(); + this.drainMicrotasksWithGlobal(this.global, this.virtual_machine.jsc); + if (this.deferred_tasks.run()) { + this.drainMicrotasksWithGlobal(this.global, this.virtual_machine.jsc); + } if (comptime bun.Environment.isDebug) { this.debug.drain_microtasks_count_outside_tick_queue += @as(usize, @intFromBool(!this.debug.is_inside_tick_queue));