Lightpanda: the headless browser designed for AI and automation
Log the stalled page URL and JS stack when the watchdog terminates
Scott Taylor0f9cbb8f570b
diff --git a/src/Watchdog.zig b/src/Watchdog.zigindex bd080ce60..a95b50628 100644--- a/src/Watchdog.zig+++ b/src/Watchdog.zig@@ -133,7 +133,9 @@ fn run(self: *Watchdog) void {if (entry.fired == false) {entry.fired = true;log.warn(.app, "watchdog stall", .{ .stalled_ms = stalled_ms });- entry.env.requestTerminate();+ // Also logs "watchdog stall script" (URL and JS stack) from+ // the worker thread when the termination interrupt lands.+ entry.env.requestTerminateForStall();}}}diff --git a/src/browser/js/Context.zig b/src/browser/js/Context.zigindex 7469f240e..678389e7c 100644--- a/src/browser/js/Context.zig+++ b/src/browser/js/Context.zig@@ -153,6 +153,13 @@ fn fromC(c_context: *const v8.Context) ?*Context {return @ptrCast(@alignCast(v8.v8__Context__GetAlignedPointerFromEmbedderData(c_context, 1)));}+/// The entered Context, or null when none is entered (e.g. a V8 interrupt+/// serviced outside script execution) or its Context struct is gone.+pub fn current(isolate: *v8.Isolate) ?*Context {+ const v8_context = v8.v8__Isolate__GetCurrentContext(isolate) orelse return null;+ return fromC(v8_context);+}+/// Returns the Context and v8::Context for the given isolate./// If the current context is from a destroyed Context (e.g., navigated-away iframe),/// falls back to the incumbent context (the calling context).diff --git a/src/browser/js/Env.zig b/src/browser/js/Env.zigindex eea42d88f..34629f646 100644--- a/src/browser/js/Env.zig+++ b/src/browser/js/Env.zig@@ -110,6 +110,11 @@ terminate_mutex: std.Io.Mutex = .init,// thread making sure terminate hasn't been canceled.terminate_requested: std.atomic.Value(bool) = .init(false),+// Boot-clock ms at which the watchdog asked for a stall report; 0 = none.+// Set by the watchdog thread, consumed on the worker by terminateInterrupt so+// the stalled script can be identified before it is killed.+stall_report_requested_at: std.atomic.Value(u64) = .init(0),+// Set while a V8 context (or the isolate) is being disposed.tearing_down: bool = false,@@ -695,14 +700,105 @@ pub fn requestTerminate(self: *Env) void {v8.v8__Isolate__RequestInterrupt(self.isolate.handle, terminateInterrupt, self);}+// Called from the watchdog thread. Like requestTerminate, but the worker also+// logs the running script's frame URL and stack before termination.+pub fn requestTerminateForStall(self: *Env) void {+ self.stall_report_requested_at.store(@max(lp.datetime.milliTimestamp(.boot), 1), .release);+ self.requestTerminate();+}+// Runs on the worker threadfn 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);+ }v8.v8__Isolate__TerminateExecution(self.isolate.handle);}}+const STALL_STACK_FRAMES = 12;+const STALL_URL_MAX = 512;++const StallReport = struct {+ url: []const u8 = "",+ page_url: []const u8 = "",+ stack: []const u8 = "",+};++// Runs on the worker thread, inside a V8 interrupt: reads the current stack+// but never runs JavaScript. Everything returned is either in `stack_buf` or+// owned by the frame, so it's only valid until the caller returns.+fn stallReport(self: *Env, stack_buf: []u8) StallReport {+ const isolate = self.isolate.handle;+ var hs: v8.HandleScope = undefined;+ v8.v8__HandleScope__CONSTRUCT(&hs, isolate);+ defer v8.v8__HandleScope__DESTRUCT(&hs);++ var report: StallReport = .{};+ if (Context.current(isolate)) |ctx| {+ report.url = truncate(ctx.global.url(), STALL_URL_MAX);+ report.page_url = truncate(switch (ctx.global) {+ .frame => |frame| frame.page.frame.url,+ .worker => |worker| worker.page.frame.url,+ }, STALL_URL_MAX);+ }++ var writer: std.Io.Writer = .fixed(stack_buf);+ if (v8.v8__StackTrace__CurrentStackTrace__STATIC(isolate, STALL_STACK_FRAMES)) |stack| {+ // A full buffer truncates the stack; the frames written are kept.+ writeStallStack(isolate, stack, &writer) catch {};+ }+ report.stack = writer.buffered();+ return report;+}++fn logStall(self: *Env, requested_at: u64) void {+ var stack_buf: [1536]u8 = undefined;+ const report = self.stallReport(&stack_buf);+ log.warn(.app, "watchdog stall script", .{+ .url = report.url,+ .page_url = report.page_url,+ // Large when the stall was in native code: the interrupt only lands+ // once control is back in JavaScript.+ .interrupt_delay_ms = lp.datetime.milliTimestamp(.boot) -| requested_at,+ .stack = report.stack,+ });+}++fn writeStallStack(isolate: *v8.Isolate, stack: *const v8.StackTrace, writer: *std.Io.Writer) !void {+ const separator = log.separator();+ const frame_count: usize = @intCast(v8.v8__StackTrace__GetFrameCount(stack));+ for (0..frame_count) |i| {+ const frame = v8.v8__StackTrace__GetFrame(stack, isolate, @intCast(i)).?;++ var name_buf: [128]u8 = undefined;+ const name = utf8(isolate, v8.v8__StackFrame__GetFunctionName(frame), &name_buf);+ var script_buf: [256]u8 = undefined;+ const script = utf8(isolate, v8.v8__StackFrame__GetScriptNameOrSourceURL(frame), &script_buf);++ try writer.print("{s}{s} ({s}:{d}:{d})", .{+ separator,+ if (name.len == 0) "<anonymous>" else name,+ if (script.len == 0) "<unknown>" else script,+ v8.v8__StackFrame__GetLineNumber(frame),+ v8.v8__StackFrame__GetColumn(frame),+ });+ }+}++fn utf8(isolate: *v8.Isolate, str: ?*const v8.String, buf: []u8) []const u8 {+ const s = str orelse return "";+ const n = v8.v8__String__WriteUtf8(s, isolate, buf.ptr, buf.len, v8.WRITE_REPLACE_INVALID_UTF8, null);+ return buf[0..n];+}++fn truncate(s: []const u8, max: usize) []const u8 {+ return s[0..@min(s.len, max)];+}+/// Clears a pending termination so V8 calls (e.g. those made during cleanup)/// don't keep tripping over the terminating-state asserts. Safe to call/// unconditionally; a no-op if termination wasn't pending. Also clears the@@ -711,6 +807,7 @@ pub fn cancelTerminate(self: *Env) void {self.terminate_mutex.lockUncancelable(lp.io);defer self.terminate_mutex.unlock(lp.io);self.terminate_requested.store(false, .release);+ self.stall_report_requested_at.store(0, .release);v8.v8__Isolate__CancelTerminateExecution(self.isolate.handle);}@@ -842,6 +939,99 @@ test "Env: a heap limit reached during teardown does not arm a termination" {try testing.expect(granted > limit);}+test "Env: stall report names the running frame and script stack" {+ 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 State = struct {+ env: *Env,+ url: [64]u8 = undefined,+ url_len: usize = 0,+ page_url_matches: bool = false,+ stack: [1536]u8 = undefined,+ stack_len: usize = 0,++ fn capture(self: *@This()) void {+ var buf: [1536]u8 = undefined;+ const report = self.env.stallReport(&buf);+ self.url_len = @min(report.url.len, self.url.len);+ @memcpy(self.url[0..self.url_len], report.url[0..self.url_len]);+ self.page_url_matches = std.mem.eql(u8, report.url, report.page_url);+ self.stack_len = report.stack.len;+ @memcpy(self.stack[0..self.stack_len], report.stack);+ }+ };+ var state = State{ .env = frame.js.env };++ const driver = try local.exec(+ \\(function(capture) {+ \\ function stallOuter() { stallInner(); }+ \\ function stallInner() { capture(); }+ \\ stallOuter();+ \\})+ , "https://example.com/stall.js");+ const driver_fn = js.Function{ .local = local, .handle = @ptrCast(driver.handle) };+ try driver_fn.call(void, .{local.newCallback(State.capture, &state)});++ try testing.expectEqual(frame.url, state.url[0..state.url_len]);+ try testing.expectEqual(true, state.page_url_matches);+ const stack = state.stack[0..state.stack_len];+ const inner = std.mem.indexOf(u8, stack, "stallInner (https://example.com/stall.js:3:") orelse return error.MissingInnerFrame;+ const outer = std.mem.indexOf(u8, stack, "stallOuter (https://example.com/stall.js:2:") orelse return error.MissingOuterFrame;+ try testing.expect(inner < outer);+}++test "Env: watchdog termination logs the stalled script once" {+ testing.expectLog(&.{.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;+ defer env.cancelTerminate();++ const State = struct {+ env: *Env,+ fn stall(self: *@This()) void {+ // Two in-flight interrupts must still produce one report.+ self.env.requestTerminateForStall();+ self.env.requestTerminateForStall();+ }+ };+ var state = State{ .env = env };++ const driver = try local.exec("(function(stall) { stall(); for(;;){} })", null);+ const driver_fn = js.Function{ .local = local, .handle = @ptrCast(driver.handle) };+ var caught: js.TryCatch.Caught = .{};+ try testing.expectError(error.ExecutionTerminated, driver_fn.tryCall(void, .{local.newCallback(State.stall, &state)}, &caught));+ try testing.expectEqual(0, env.stall_report_requested_at.load(.acquire));+}++test "Env: canceling a termination drops its pending stall report" {+ const frame = try testing.createFrame();+ defer testing.test_session.closeAllPages();++ const env = frame.js.env;+ env.requestTerminateForStall();+ env.cancelTerminate();+ try testing.expectEqual(0, env.stall_report_requested_at.load(.acquire));++ var ls: js.Local.Scope = undefined;+ frame.js.localScope(&ls);+ defer ls.deinit();+ try testing.expectEqual(3, try (try ls.local.exec("1 + 2", null)).toI32());+}+test "Env: Worker context " {const frame = try testing.createFrame();defer testing.test_session.closeAllPages();