Skip to content

Commit

Permalink
flush entire immediate queue
Browse files Browse the repository at this point in the history
  • Loading branch information
dylan-conway committed Oct 17, 2024
1 parent 2d0b557 commit b94ea3a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 9 deletions.
24 changes: 17 additions & 7 deletions src/bun.js/event_loop.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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;
Expand All @@ -1471,7 +1479,9 @@ pub const EventLoop = struct {
ctx.timer.drainTimers(ctx);
}

this.flushImmediateQueue();
while (this.flushImmediateQueue()) {
this.tickImmediateTasks(ctx);
}
ctx.onAfterEventLoop();
}

Expand Down
6 changes: 4 additions & 2 deletions src/cli/test_command.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand All @@ -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 => {},
Expand Down

0 comments on commit b94ea3a

Please sign in to comment.