diff --git a/src/bun.js/event_loop.zig b/src/bun.js/event_loop.zig index bca57c7f3e9e9..2434502ac6487 100644 --- a/src/bun.js/event_loop.zig +++ b/src/bun.js/event_loop.zig @@ -1356,8 +1356,9 @@ pub const EventLoop = struct { var ctx = this.virtual_machine; var loop = this.usocketsLoop(); - this.flushImmediateQueue(); - this.tickImmediateTasks(ctx); + while (this.flushImmediateQueue()) { + this.tickImmediateTasks(ctx); + } if (comptime Environment.isPosix) { // Some tasks need to keep the event loop alive for one more tick. @@ -1394,22 +1395,28 @@ pub const EventLoop = struct { ctx.timer.drainTimers(ctx); } - this.flushImmediateQueue(); + while (this.flushImmediateQueue()) { + this.tickImmediateTasks(ctx); + } + ctx.onAfterEventLoop(); } - pub fn flushImmediateQueue(this: *EventLoop) void { + pub fn flushImmediateQueue(this: *EventLoop) bool { // If we can get away with swapping the queues, do that rather than copying the data if (this.immediate_tasks.count > 0) { this.immediate_tasks.write(this.next_immediate_tasks.readableSlice(0)) catch unreachable; this.next_immediate_tasks.head = 0; this.next_immediate_tasks.count = 0; + return true; } else if (this.next_immediate_tasks.count > 0) { const prev_immediate = this.immediate_tasks; const next_immediate = this.next_immediate_tasks; this.immediate_tasks = next_immediate; this.next_immediate_tasks = prev_immediate; + return true; } + return false; } pub fn tickPossiblyForever(this: *EventLoop) void { @@ -1447,8 +1454,9 @@ pub const EventLoop = struct { pub fn autoTickActive(this: *EventLoop) void { var loop = this.usocketsLoop(); var ctx = this.virtual_machine; - this.flushImmediateQueue(); - this.tickImmediateTasks(ctx); + while (this.flushImmediateQueue()) { + this.tickImmediateTasks(ctx); + } if (comptime Environment.isPosix) { const pending_unref = ctx.pending_unref_counter; @@ -1471,7 +1479,9 @@ pub const EventLoop = struct { ctx.timer.drainTimers(ctx); } - this.flushImmediateQueue(); + while (this.flushImmediateQueue()) { + this.tickImmediateTasks(ctx); + } ctx.onAfterEventLoop(); } diff --git a/src/cli/test_command.zig b/src/cli/test_command.zig index b0f1000b5bd45..837e7fd5230af 100644 --- a/src/cli/test_command.zig +++ b/src/cli/test_command.zig @@ -1244,7 +1244,7 @@ pub const TestCommand = struct { vm.eventLoop().tick(); var prev_unhandled_count = vm.unhandled_error_counter; - while (vm.active_tasks > 0) : (vm.eventLoop().flushImmediateQueue()) { + while (vm.active_tasks > 0) : (_ = vm.eventLoop().flushImmediateQueue()) { if (!jest.Jest.runner.?.has_pending_tests) { jest.Jest.runner.?.drain(); } @@ -1264,7 +1264,9 @@ pub const TestCommand = struct { } } - vm.eventLoop().flushImmediateQueue(); + while (vm.eventLoop().flushImmediateQueue()) { + vm.eventLoop().tickImmediateTasks(vm); + } switch (vm.aggressive_garbage_collection) { .none => {},