Lightpanda: the headless browser designed for AI and automation
crash: refuse native calls when terminating
Karl Seguin74bbbb69163e
diff --git a/src/browser/js/Caller.zig b/src/browser/js/Caller.zigindex 82c937d6f..aae31a36a 100644--- a/src/browser/js/Caller.zig+++ b/src/browser/js/Caller.zig@@ -50,12 +50,31 @@ pub fn init(self: *Caller, v8_isolate: *v8.Isolate) bool {throwDetachedError(v8_isolate);return false;};+ if (refuseIfTerminating(ctx, v8_isolate)) {+ return false;+ }initWithContext(self, ctx, v8_context);return true;}fn throwDetachedError(isolate: *v8.Isolate) void {- const message = "Cannot execute in detached context (e.g., navigated-away iframe)";+ throwError(isolate, "Cannot execute in detached context (e.g., navigated-away iframe)");+}+// Terminate only triggers on a v8 stack check. A script that spends it's time+// in a few huge native calls (e.g. el.innerHTML += el.innerHTML (yes, we've+// seen that)) never triggers it. So we refuse new native work if we have a+// pending terminate.+fn refuseIfTerminating(ctx: *const Context, isolate: *v8.Isolate) bool {+ const env = ctx.env;+ if (env.terminatePending() == false) {+ return false;+ }+ env.logPendingReports();+ throwError(isolate, "Execution is terminating");+ return true;+}++fn throwError(isolate: *v8.Isolate, message: []const u8) void {const v8_message = v8.v8__String__NewFromUtf8(isolate, message.ptr, v8.kNormal, @intCast(message.len));const js_exception = v8.v8__Exception__Error(v8_message);_ = v8.v8__Isolate__ThrowException(isolate, js_exception);@@ -837,6 +856,9 @@ pub const Function = struct {throwDetachedError(v8_isolate);return;};+ if (refuseIfTerminating(ctx, v8_isolate)) {+ return;+ }const info = FunctionCallbackInfo{ .handle = info_handle };var hs: js.HandleScope = undefined;diff --git a/src/browser/js/Env.zig b/src/browser/js/Env.zigindex 0264ffc74..dad9382f2 100644--- a/src/browser/js/Env.zig+++ b/src/browser/js/Env.zig@@ -698,11 +698,8 @@ fn onNearHeapLimit(self: *Env, current_limit: usize, initial_limit: usize) usizeself.requestTerminate();}- const cap = initial_limit + 256 * 1024 * 1024;- if (current_limit >= cap) {- return current_limit;- }- return @min(cap, current_limit + 64 * 1024 * 1024);+ // give it 256MB more to, hopefully, shtudown cleanly+ return @max(current_limit, initial_limit + 256 * 1024 * 1024);}// Called from the network thread, caused v8 to eventually call terminateInterrupt@@ -730,17 +727,22 @@ pub fn pendingStallReport(self: *const Env) ?u64 {fn terminateInterrupt(_: ?*v8.Isolate, data: ?*anyopaque) callconv(.c) void {const self: *Env = @ptrCast(@alignCast(data.?));if (self.terminate_requested.load(.acquire)) {- const requested_at = self.stall_report_requested_at.swap(0, .acq_rel);- if (requested_at != 0) {- self.logStall(requested_at);- }- if (self.heap_report_requested.swap(false, .acq_rel)) {- self.logHeapLimit();- }+ self.logPendingReports();v8.v8__Isolate__TerminateExecution(self.isolate.handle);}}+// Runs on the worker thread, while the script being stopped is on the stack.+pub fn logPendingReports(self: *Env) void {+ const requested_at = self.stall_report_requested_at.swap(0, .acq_rel);+ if (requested_at != 0) {+ self.logStall(requested_at);+ }+ if (self.heap_report_requested.swap(false, .acq_rel)) {+ self.logHeapLimit();+ }+}+const SCRIPT_STACK_FRAMES = 12;const SCRIPT_URL_MAX = 512;@@ -1057,6 +1059,42 @@ test "Env: heap limit termination logs the running script once" {try testing.expectEqual(false, env.heap_report_requested.load(.acquire));}+test "Env: a pending termination refuses native calls" {+ // "JS heap limit reached", then the refusal logs "JS heap limit script".+ testing.expectLog(&.{ .app, .app });++ 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;++ const State = struct {+ env: *Env,+ fn exhaust(self: *@This()) void {+ _ = self.env.onNearHeapLimit(1024, 1024);+ }+ };+ var state = State{ .env = env };++ // No loop and no JS call after exhaust(): nothing reaches a V8 stack+ // check, so only the setter can stop the script.+ const driver = try local.exec("(function(exhaust) { exhaust(); document.title = 'ran'; })", null);+ const driver_fn = js.Function{ .local = local, .handle = @ptrCast(driver.handle) };+ var caught: js.TryCatch.Caught = .{};+ if (driver_fn.tryCall(void, .{local.newCallback(State.exhaust, &state)}, &caught)) {+ return error.NativeCallNotRefused;+ } else |_| {}+ try testing.expectEqual(false, env.heap_report_requested.load(.acquire));++ env.cancelTerminate();+ try testing.expectEqual(false, std.mem.eql(u8, "ran", try (try local.exec("document.title", null)).toStringSlice()));+}+test "Env: canceling a termination drops its pending stall report" {const frame = try testing.createFrame();defer testing.test_session.closeAllPages();