Skip to content

Lightpanda: the headless browser designed for AI and automation

fix: avoid rearming heap termination during teardown

Scott Taylor427960fd6c08
diff --git a/src/browser/Browser.zig b/src/browser/Browser.zig
index 79cad999b..bf5fcd0d5 100644
--- a/src/browser/Browser.zig
+++ b/src/browser/Browser.zig
@@ -203,6 +203,13 @@ pub fn newSession(self: *Browser, notification: *Notification) !*Session {
}
pub fn closeSession(self: *Browser) void {
+ const env = &self.env;
+
+ // Teardown GC must not arm a new termination.
+ const was_tearing_down = env.tearing_down;
+ env.tearing_down = true;
+ defer env.tearing_down = was_tearing_down;
+
if (self.session) |*session| {
session.deinit();
self.session = null;
diff --git a/src/browser/Session.zig b/src/browser/Session.zig
index 05a9439d3..8c312289a 100644
--- a/src/browser/Session.zig
+++ b/src/browser/Session.zig
@@ -243,6 +243,12 @@ pub fn processDestroyQueues(self: *Session) void {
{
const queue = self._page_destruction_queue.items;
if (queue.len > 0) {
+ // Context disposal GC must not arm a new termination.
+ const env = &self.browser.env;
+ const was_tearing_down = env.tearing_down;
+ env.tearing_down = true;
+ defer env.tearing_down = was_tearing_down;
+
for (queue) |page| {
page.deinit();
self.browser.page_pool.destroy(page);
diff --git a/src/browser/js/Env.zig b/src/browser/js/Env.zig
index c42f837d0..1bf52dfb1 100644
--- a/src/browser/js/Env.zig
+++ b/src/browser/js/Env.zig
@@ -104,6 +104,11 @@ terminate_mutex: std.Io.Mutex = .init,
// thread making sure terminate hasn't been canceled.
terminate_requested: std.atomic.Value(bool) = .init(false),
+// Set while V8 contexts are being destroyed.
+tearing_down: bool = false,
+
+heap_limit_protected: bool = false,
+
pub const InitOpts = struct {
with_inspector: bool = false,
};
@@ -209,6 +214,10 @@ pub fn init(app: *App, opts: InitOpts) !Env {
}
pub fn deinit(self: *Env) void {
+ // Prevent callbacks during isolate disposal.
+ self.tearing_down = true;
+ self.releaseHeapLimit();
+
if (comptime lp.IS_DEBUG) {
std.debug.assert(self.contexts.items.len == 0);
}
@@ -576,6 +585,7 @@ pub fn terminate(self: *Env) void {
// We need a stable pointer for *Env, so can't be setup in init.
pub fn protectHeapLimit(self: *Env) void {
+ self.heap_limit_protected = true;
v8.v8__Isolate__AddNearHeapLimitCallback(self.isolate.handle, nearHeapLimit, self);
// TODO: uncomment this when https://github.com/lightpanda-io/zig-v8-fork/pull/187 lands
// if our nearHeapLimit extends the memory, we want to restore the original
@@ -599,12 +609,29 @@ pub fn protectHeapLimit(self: *Env) void {
// V8 expects.
fn nearHeapLimit(data: ?*anyopaque, current_limit: usize, initial_limit: usize) callconv(.c) usize {
const self: *Env = @ptrCast(@alignCast(data.?));
+ return self.onNearHeapLimit(current_limit, initial_limit);
+}
+
+fn releaseHeapLimit(self: *Env) void {
+ if (self.heap_limit_protected == false) {
+ return;
+ }
+ self.heap_limit_protected = false;
+ v8.v8__Isolate__RemoveNearHeapLimitCallback(self.isolate.handle, nearHeapLimit, 0);
+}
+
+fn onNearHeapLimit(self: *Env, current_limit: usize, initial_limit: usize) usize {
lp.metrics.js_heap_limits.incr();
log.err(.app, "JS heap limit reached", .{
.initial_limit = initial_limit,
.current_limit = current_limit,
+ .tearing_down = self.tearing_down,
});
- self.requestTerminate();
+
+ // Context disposal can trigger this after execution has ended.
+ if (self.tearing_down == false) {
+ self.requestTerminate();
+ }
const cap = initial_limit + 256 * 1024 * 1024;
if (current_limit >= cap) {
@@ -718,6 +745,24 @@ const PrivateSymbols = struct {
};
const testing = @import("../../testing.zig");
+
+test "Env: a heap limit reached during teardown does not arm a termination" {
+ testing.expectLog(&.{.app});
+
+ const frame = try testing.createFrame();
+ defer testing.test_session.closeAllPages();
+
+ const env = frame.js.env;
+ const limit: usize = 64 * 1024 * 1024;
+
+ env.tearing_down = true;
+ defer env.tearing_down = false;
+
+ const granted = env.onNearHeapLimit(limit, limit);
+ try testing.expectEqual(false, env.terminatePending());
+ try testing.expect(granted > limit);
+}
+
test "Env: Worker context " {
const frame = try testing.createFrame();
defer testing.test_session.closeAllPages();
diff --git a/src/browser/webapi/MutationObserver.zig b/src/browser/webapi/MutationObserver.zig
index 43e0ebb0a..4bbab0f4a 100644
--- a/src/browser/webapi/MutationObserver.zig
+++ b/src/browser/webapi/MutationObserver.zig
@@ -571,3 +571,44 @@ test "WebApi: MutationObserver requested termination unwinds delivery" {
env.runMicrotasks();
try testing.expectEqual(2, try (try local.exec("window.__delivered", null)).toI32());
}
+
+test "WebApi: MutationObserver terminated page tears down" {
+ testing.expectLog(&.{ .frame, .frame });
+
+ const frame = try testing.createFrame();
+ const env = frame.js.env;
+ defer env.cancelTerminate();
+ {
+ var ls: js.Local.Scope = undefined;
+ frame.js.localScope(&ls);
+ defer ls.deinit();
+ const local = &ls.local;
+
+ const State = struct {
+ env: *js.Env,
+
+ fn kill(self: *@This()) void {
+ self.env.requestTerminate();
+ }
+ };
+ var state = State{ .env = env };
+ const kill_cb = local.newCallback(State.kill, &state);
+ const setup = try local.exec(
+ \\(function(kill) {
+ \\ const target = document.createElement('div');
+ \\ new MutationObserver(() => { kill(); for(;;){} })
+ \\ .observe(target, { attributes: true });
+ \\ target.setAttribute('x', '1');
+ \\})
+ , null);
+ const setup_fn = js.Function{ .local = local, .handle = @ptrCast(setup.handle) };
+ try setup_fn.call(void, .{kill_cb});
+ }
+
+ env.runMicrotasks();
+ try testing.expectEqual(true, env.terminatePending());
+ try testing.expectEqual(false, js.v8.v8__Isolate__IsExecutionTerminating(env.isolate.handle));
+
+ testing.test_session.closeAllPages();
+ try testing.expectEqual(@as(usize, 0), env.contexts.items.len);
+}