Skip to content

Commit 75e0637

Browse files
committed
Ensure page background tasks are re-registered on reset
1 parent 852c30b commit 75e0637

File tree

3 files changed

+32
-23
lines changed

3 files changed

+32
-23
lines changed

src/browser/Scheduler.zig

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ pub fn init(allocator: Allocator) Scheduler {
3737
}
3838

3939
pub fn reset(self: *Scheduler) void {
40-
self.high_priority.clearRetainingCapacity();
41-
self.low_priority.clearRetainingCapacity();
40+
// Our allocator is the page arena, it's been reset. We cannot use
41+
// clearAndRetainCapacity, since that space is no longer ours
42+
self.high_priority.clearAndFree();
43+
self.low_priority.clearAndFree();
4244
}
4345

4446
const AddOpts = struct {

src/browser/js/Function.zig

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,10 +138,7 @@ fn getThis(self: *const Function) v8.Object {
138138
return self.this orelse self.context.v8_context.getGlobal();
139139
}
140140

141-
// debug/helper to print the source of the JS callback
142-
pub fn printFunc(self: Function) !void {
143-
const context = self.context;
141+
pub fn src(self: *const Function) ![]const u8 {
144142
const value = self.func.castToFunction().toValue();
145-
const src = try js.valueToString(context.call_arena, value, context.isolate, context.v8_context);
146-
std.debug.print("{s}\n", .{src});
143+
return self.context.valueToString(value, .{});
147144
}

src/browser/page.zig

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const Allocator = std.mem.Allocator;
2424
const Dump = @import("dump.zig");
2525
const State = @import("State.zig");
2626
const Mime = @import("mime.zig").Mime;
27+
const Browser = @import("browser.zig").Browser;
2728
const Session = @import("session.zig").Session;
2829
const Renderer = @import("renderer.zig").Renderer;
2930
const Window = @import("html/window.zig").Window;
@@ -146,11 +147,7 @@ pub const Page = struct {
146147
self.js = try session.executor.createContext(self, true, js.GlobalMissingCallback.init(&self.polyfill_loader));
147148
try polyfill.preload(self.arena, self.js);
148149

149-
try self.scheduler.add(self, runMicrotasks, 5, .{ .name = "page.microtasks" });
150-
// message loop must run only non-test env
151-
if (comptime !builtin.is_test) {
152-
try self.scheduler.add(self, runMessageLoop, 5, .{ .name = "page.messageLoop" });
153-
}
150+
try self.registerBackgroundTasks();
154151
}
155152

156153
pub fn deinit(self: *Page) void {
@@ -160,7 +157,7 @@ pub const Page = struct {
160157
self.script_manager.deinit();
161158
}
162159

163-
fn reset(self: *Page) void {
160+
fn reset(self: *Page) !void {
164161
// Force running the micro task to drain the queue.
165162
self.session.browser.env.runMicrotasks();
166163

@@ -171,18 +168,31 @@ pub const Page = struct {
171168
self.load_state = .parsing;
172169
self.mode = .{ .pre = {} };
173170
_ = self.session.browser.page_arena.reset(.{ .retain_with_limit = 1 * 1024 * 1024 });
174-
}
175171

176-
fn runMicrotasks(ctx: *anyopaque) ?u32 {
177-
const self: *Page = @ptrCast(@alignCast(ctx));
178-
self.session.browser.runMicrotasks();
179-
return 5;
172+
try self.registerBackgroundTasks();
180173
}
181174

182-
fn runMessageLoop(ctx: *anyopaque) ?u32 {
183-
const self: *Page = @ptrCast(@alignCast(ctx));
184-
self.session.browser.runMessageLoop();
185-
return 100;
175+
fn registerBackgroundTasks(self: *Page) !void {
176+
if (comptime builtin.is_test) {
177+
// HTML test runner manually calls these as necessary
178+
return;
179+
}
180+
181+
try self.scheduler.add(self.session.browser, struct {
182+
fn runMicrotasks(ctx: *anyopaque) ?u32 {
183+
const b: *Browser = @ptrCast(@alignCast(ctx));
184+
b.runMicrotasks();
185+
return 5;
186+
}
187+
}.runMicrotasks, 5, .{ .name = "page.microtasks" });
188+
189+
try self.scheduler.add(self.session.browser, struct {
190+
fn runMessageLoop(ctx: *anyopaque) ?u32 {
191+
const b: *Browser = @ptrCast(@alignCast(ctx));
192+
b.runMessageLoop();
193+
return 100;
194+
}
195+
}.runMessageLoop, 5, .{ .name = "page.messageLoop" });
186196
}
187197

188198
pub const DumpOpts = struct {
@@ -529,7 +539,7 @@ pub const Page = struct {
529539
if (self.mode != .pre) {
530540
// it's possible for navigate to be called multiple times on the
531541
// same page (via CDP). We want to reset the page between each call.
532-
self.reset();
542+
try self.reset();
533543
}
534544

535545
log.info(.http, "navigate", .{

0 commit comments

Comments
 (0)