Skip to content

Lightpanda: the headless browser designed for AI and automation

Clear V8 terminating state after a terminated microtask checkpoint

Scott Taylord0c145b0c966
diff --git a/src/browser/js/Env.zig b/src/browser/js/Env.zig
index 251b7bc7b..af2cfa925 100644
--- a/src/browser/js/Env.zig
+++ b/src/browser/js/Env.zig
@@ -427,10 +427,25 @@ pub fn runMicrotasks(self: *Env) void {
while (i < self.contexts.items.len) : (i += 1) {
const ctx = self.contexts.items[i];
v8.v8__MicrotaskQueue__PerformCheckpoint(ctx.microtask_queue, v8_isolate);
+ if (self.terminatePending()) {
+ // A terminate landed inside this checkpoint. Unlike a JS entry,
+ // the checkpoint exit does not clear V8's terminating state, and
+ // leaving it set makes later V8 calls (teardown, inspector) fail
+ // in ways their callers don't expect. Clear the V8 state; the
+ // sticky terminate_requested keeps blocking new JS entries.
+ clearTerminationState(v8_isolate);
+ return;
+ }
}
}
}
+fn clearTerminationState(v8_isolate: *v8.Isolate) void {
+ if (v8.v8__Isolate__IsExecutionTerminating(v8_isolate)) {
+ v8.v8__Isolate__CancelTerminateExecution(v8_isolate);
+ }
+}
+
pub fn runMacrotasks(self: *Env) !void {
if (self.terminatePending()) {
return;
@@ -633,6 +648,11 @@ pub fn performIsolateMicrotasks(self: *Env) void {
defer self.terminate_mutex.unlock(lp.io);
if (self.terminatePending()) return;
v8.v8__Isolate__PerformMicrotaskCheckpoint(self.isolate.handle);
+ if (self.terminatePending()) {
+ // See runMicrotasks: a checkpoint exit doesn't clear V8's terminating
+ // state the way a JS entry unwind does.
+ clearTerminationState(self.isolate.handle);
+ }
}
fn promiseRejectCallback(message_handle: v8.PromiseRejectMessage) callconv(.c) void {
diff --git a/src/browser/webapi/MutationObserver.zig b/src/browser/webapi/MutationObserver.zig
index 6ed87ba8f..6c193eece 100644
--- a/src/browser/webapi/MutationObserver.zig
+++ b/src/browser/webapi/MutationObserver.zig
@@ -469,3 +469,69 @@ const testing = @import("../../testing.zig");
test "WebApi: MutationObserver" {
try testing.htmlRunner("mutation_observer", .{});
}
+
+// Production watchdog path (lightpanda-io/browser#3130 follow-up): the
+// terminate lands inside a MutationObserver callback, so ExecutionTerminated
+// must unwind out of deliverRecords, stay sticky against further V8 entries,
+// and leave the observer machinery usable after a top-level cancel.
+test "WebApi: MutationObserver requested termination unwinds delivery" {
+ const v8 = js.v8;
+ testing.expectLog(&.{ .frame, .frame });
+
+ const frame = try testing.createFrame();
+ defer testing.test_session.closeAllPages();
+
+ var ls: js.Local.Scope = undefined;
+ frame.js.localScope(&ls);
+ defer ls.deinit();
+ const local = &ls.local;
+
+ const env = frame.js.env;
+ defer env.cancelTerminate();
+
+ 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);
+
+ // An observer whose callback wedges until the requested termination
+ // lands, like a storefront spinning inside its MutationObserver callback
+ // when the watchdog fires.
+ const setup = try local.exec(
+ \\(function(kill) {
+ \\ const target = document.createElement('div');
+ \\ window.__delivered = 0;
+ \\ window.__wedge = true;
+ \\ window.__target = target;
+ \\ new MutationObserver(() => {
+ \\ window.__delivered++;
+ \\ if (window.__wedge) { 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});
+
+ // The queued delivery microtask wedges; the terminate unwinds it and the
+ // unwind must stop at deliverRecords without another V8 entry.
+ env.runMicrotasks();
+
+ try testing.expectEqual(true, env.terminatePending());
+ try testing.expectEqual(false, v8.v8__Isolate__IsExecutionTerminating(env.isolate.handle));
+
+ // Sticky: no fresh eval may enter V8 while the termination is pending.
+ try testing.expectError(error.ExecutionTerminated, local.exec("1 + 1", null));
+
+ // A top-level cancel restores execution AND mutation delivery.
+ env.cancelTerminate();
+ try testing.expectEqual(1, try (try local.exec("window.__delivered", null)).toI32());
+ try local.eval("window.__wedge = false; window.__target.setAttribute('x', '2');", null);
+ env.runMicrotasks();
+ try testing.expectEqual(2, try (try local.exec("window.__delivered", null)).toI32());
+}