Skip to content

Lightpanda: the headless browser designed for AI and automation

Interrupt nested script waits during teardown

Scott Taylora12032cd0e00
diff --git a/src/browser/ScriptManager.zig b/src/browser/ScriptManager.zig
index 9dc324690..2cca19e12 100644
--- a/src/browser/ScriptManager.zig
+++ b/src/browser/ScriptManager.zig
@@ -171,6 +171,7 @@ fn waitForPreload(self: *ScriptManager, url: [:0]const u8) ?*Script {
const entry = self.preloaded_scripts.getPtr(url) orelse return null;
switch (entry.state) {
.loading => {
+ if (client.hasPendingTeardown()) return null;
_ = client.tickSync(200) catch return null;
continue;
},
@@ -596,3 +597,38 @@ test "ScriptManager: PreloadedScript.shutdownCallback drops a .loading preload"
try testing.expect(sm.preloaded_scripts.getPtr(url) == null);
}
+
+test "ScriptManager: waitForPreload stops when teardown is pending" {
+ defer testing.reset();
+ const page = try testing.pageTest("mcp_nav.html", .{});
+ defer page.close();
+
+ const frame = page.frame().?;
+ const sm = &frame._script_manager;
+ const client = sm.base.client;
+ const url: [:0]const u8 = "http://127.0.0.1:9582/pending-preload.js";
+
+ const arena = try frame.getArena(.large, "test.pending_teardown");
+ const script = try arena.create(Script);
+ script.* = .{
+ .arena = arena,
+ .url = url,
+ .node = .{},
+ .manager = &sm.base,
+ .complete = false,
+ .source = .{ .remote = .{} },
+ .extra = .preload,
+ .hint_element = null,
+ };
+ try sm.preloaded_scripts.put(sm.base.allocator, url, .{ .state = .{ .loading = script } });
+ defer sm.takePreload(url).?.deinit();
+
+ const message_arena = try client.arena_pool.acquire(.tiny, "test teardown message");
+ client.inbox.push(message_arena, .{ .cdp = .{
+ .raw = try message_arena.dupe(u8, "{}"),
+ .input = .{ .method = "Target.closeTarget" },
+ } });
+ defer client.inbox.pop().?.deinit(client.arena_pool);
+
+ try testing.expect(sm.waitForPreload(url) == null);
+}
diff --git a/src/browser/ScriptManagerBase.zig b/src/browser/ScriptManagerBase.zig
index bcd5c1178..57039bbc3 100644
--- a/src/browser/ScriptManagerBase.zig
+++ b/src/browser/ScriptManagerBase.zig
@@ -354,6 +354,7 @@ pub fn waitForImport(self: *ScriptManagerBase, url: [:0]const u8) !ModuleSource
};
switch (entry.value_ptr.state) {
.loading => {
+ if (client.hasPendingTeardown()) return error.SyncWaitInterrupted;
_ = try client.tickSync(200);
continue;
},
@@ -1104,3 +1105,38 @@ test "ScriptManagerBase: shutdownCallback fails a .loading module" {
try testing.expect(sm.imported_modules.getPtr(url).?.state == .err);
try testing.expectError(error.Failed, sm.waitForImport(url));
}
+
+test "ScriptManagerBase: waitForImport stops when teardown is pending" {
+ defer testing.reset();
+ const page = try testing.pageTest("mcp_nav.html", .{});
+ defer page.close();
+ const frame = page.frame().?;
+
+ const sm = &frame._script_manager.base;
+ const client = sm.client;
+ const url: [:0]const u8 = "http://127.0.0.1:9582/pending-module.js";
+
+ const arena = try sm.acquireArena(.large, "test.pending_teardown");
+ const script = try arena.create(Script);
+ script.* = .{
+ .arena = arena,
+ .url = url,
+ .node = .{},
+ .manager = sm,
+ .complete = false,
+ .source = .{ .remote = .{} },
+ .extra = .import,
+ .hint_element = null,
+ };
+ try sm.imported_modules.put(sm.allocator, url, .{ .state = .{ .loading = script } });
+ sm.async_scripts.append(&script.node);
+
+ const message_arena = try client.arena_pool.acquire(.tiny, "test teardown message");
+ client.inbox.push(message_arena, .{ .cdp = .{
+ .raw = try message_arena.dupe(u8, "{}"),
+ .input = .{ .method = "Target.disposeBrowserContext" },
+ } });
+ defer client.inbox.pop().?.deinit(client.arena_pool);
+
+ try testing.expectError(error.SyncWaitInterrupted, sm.waitForImport(url));
+}
diff --git a/src/network/HttpClient.zig b/src/network/HttpClient.zig
index 657fa6cb6..0a3814860 100644
--- a/src/network/HttpClient.zig
+++ b/src/network/HttpClient.zig
@@ -571,6 +571,10 @@ pub fn tickSync(self: *Client, timeout_ms: u32) !void {
return self._tick(timeout_ms, .sync_wait);
}
+pub fn hasPendingTeardown(self: *Client) bool {
+ return self.inbox.contains(isSyncWaitInterrupt);
+}
+
pub fn _tick(self: *Client, timeout_ms: u32, mode: DrainMode) !void {
if (self.inbox.terminated) {
return error.ClientDisconnected;
@@ -1030,6 +1034,13 @@ pub fn syncRequest(self: *Client, allocator: Allocator, req: Request, owner: *Ow
req.deinit();
return error.ClientDisconnected;
}
+ // A parser can start another blocking script/style fetch while unwinding
+ // the previous interrupted fetch. Don't pay one sync-poll interval per
+ // resource when teardown is already queued.
+ if (self.hasPendingTeardown()) {
+ req.deinit();
+ return error.SyncWaitInterrupted;
+ }
var sync_ctx = SyncContext{ .allocator = allocator, .body = .empty };
errdefer sync_ctx.body.deinit(allocator);
@@ -1063,7 +1074,7 @@ pub fn syncRequest(self: *Client, allocator: Allocator, req: Request, owner: *Ow
}
return err;
};
- if (sync_ctx.completion == .in_progress and self.inbox.contains(isSyncWaitInterrupt)) {
+ if (sync_ctx.completion == .in_progress and self.hasPendingTeardown()) {
// A teardown/close command is queued but sync_wait can't dispatch
// it mid-parse (it would free the Page/Frame this stack holds).
// Abort the blocking fetch so the parser unwinds to the next safe