Lightpanda: the headless browser designed for AI and automation
crash: only clear call_arena when all call_depths are 0.
Karl Seguin6394e9daa85a
diff --git a/src/browser/js/Caller.zig b/src/browser/js/Caller.zigindex c0057ae68..fd03544b8 100644--- a/src/browser/js/Caller.zig+++ b/src/browser/js/Caller.zig@@ -104,6 +104,7 @@ pub fn initFromHandle(self: *Caller, handle: ?*const v8.FunctionCallbackInfo) bopub fn deinit(self: *Caller) void {const ctx = self.local.ctx;const call_depth = ctx.call_depth - 1;+ ctx.call_depth = call_depth;// Because of callbacks, calls can be nested. Because of this, we// can't clear the call_arena after _every_ call. Imagine we have@@ -116,7 +117,13 @@ pub fn deinit(self: *Caller) void {//// Therefore, we keep a call_depth, and only reset the call_arena// when a top-level (call_depth == 0) function ends.- if (call_depth == 0) {+ //+ // Another context of the same global (an isolated world) can be mid-call+ // underneath us, e.g. a DOM change made by the world delivers main-world+ // MutationObservers. That Zig code allocates from frame.call_arena, which+ // is this (the main) context's call_arena, but only the world's+ // call_depth was raised.+ if (call_depth == 0 and self.prev_context.call_depth == 0) {const arena: *ArenaAllocator = @ptrCast(@alignCast(ctx.call_arena.ptr));_ = arena.reset(.{ .retain_with_limit = CALL_ARENA_RETAIN });}@@ -131,7 +138,6 @@ pub fn deinit(self: *Caller) void {_ = local_arena.reset(if (comptime lp.IS_DEBUG) .free_all else .{ .retain_with_limit = LOCAL_ARENA_RETAIN });}- ctx.call_depth = call_depth;ctx.local = self.prev_local;ctx.global.setJs(self.prev_context);}diff --git a/src/server/cdp/domains/page.zig b/src/server/cdp/domains/page.zigindex 73f56937a..621a39bf8 100644--- a/src/server/cdp/domains/page.zig+++ b/src/server/cdp/domains/page.zig@@ -1468,6 +1468,50 @@ test "cdp.frame: createIsolatedWorld targets the requested frame" {try ctx.expectSentResult(.{ .result = .{ .type = "string", .value = "Parent jobs" } }, .{ .id = 37 });}+// A DOM change made from an isolated world queues the MutationObserver+// delivery while the world's context is the frame's current one. The+// main-world callbacks then call natives, and must not reset the frame's+// call_arena, which holds the delivery's snapshot of observers and records.+test "cdp.frame: isolated world mutation delivers main-world observers" {+ var ctx = try testing.context();+ defer ctx.deinit();++ const bc = try ctx.loadBrowserContext(.{ .id = "BID-IW", .url = "hi.html", .target_id = "FID-000000000X".* });+ const root = bc.mainFrame() orelse unreachable;+ const root_id = id.toFrameId(root._frame_id);++ try ctx.processMessage(.{ .id = 1, .method = "Runtime.evaluate", .params = .{+ .expression =+ \\ window.calls = [];+ \\ for (const name of ['a', 'b', 'c']) {+ \\ new MutationObserver((records) => {+ \\ for (let i = 0; i < 50; i++) {+ \\ document.getElementById(name.repeat(64) + i);+ \\ }+ \\ calls.push(name + records.length);+ \\ }).observe(document.body, {attributes: true});+ \\ }+ ,+ } });++ try ctx.processMessage(.{ .id = 2, .method = "Page.createIsolatedWorld", .params = .{+ .frameId = &root_id,+ .worldName = "utility",+ .grantUniveralAccess = true,+ } });+ const world_ctx = try isolatedWorldContextId(bc, root);++ try ctx.processMessage(.{ .id = 3, .method = "Runtime.evaluate", .params = .{+ .expression = "document.body.setAttribute('data-x', '1')",+ .contextId = world_ctx,+ } });++ try ctx.processMessage(.{ .id = 4, .method = "Runtime.evaluate", .params = .{+ .expression = "calls.join(',')",+ } });+ try ctx.expectSentResult(.{ .result = .{ .type = "string", .value = "a1,b1,c1" } }, .{ .id = 4 });+}+// Chrome only puts a world in a frame on an explicit trigger. A world the// client asked for on the root must not appear in a sibling frame just// because that frame navigated afterwards.