Skip to content

Lightpanda: the headless browser designed for AI and automation

tools: close the page when a goto fails to start

Adrià Arrufat02a6f8295b98
diff --git a/src/browser/tools.zig b/src/browser/tools.zig
index f909f8659..937b64853 100644
--- a/src/browser/tools.zig
+++ b/src/browser/tools.zig
@@ -2688,6 +2688,7 @@ pub const StartedGoto = struct {
/// right after createPage but navigate can change/null it, so callers re-fetch.
fn openPage(session: *lp.Session, url: [:0]const u8) ToolError!lp.Session.PageHandle {
const page = session.createPage() catch return ToolError.NavigationFailed;
+ errdefer page.close();
_ = page.frame().?.navigate(url, .{
.reason = .address_bar,
.kind = .{ .push = null },
@@ -3593,6 +3594,21 @@ test "markdown: alternate reads the page's text/markdown link" {
try std.testing.expect(std.mem.find(u8, failed.text, "markdown alternate") == null);
}
+test "startGoto: a navigation that fails to start leaves no page behind" {
+ var registry: NodeRegistry = .init(std.testing.allocator);
+ defer registry.deinit();
+
+ const session = testing.test_session;
+ const pages = session.pages.items.len;
+
+ const aa = testing.arena_allocator;
+ const args = try std.json.parseFromSliceLeaky(std.json.Value, aa,
+ \\{"url":"blob:http://localhost:9582/missing"}
+ , .{});
+ try std.testing.expectError(ToolError.NavigationFailed, startGoto(aa, session, &registry, args, null));
+ try std.testing.expectEqual(pages, session.pages.items.len);
+}
+
test "markdown: a same-url page whose navigation failed is navigated again" {
var registry: NodeRegistry = .init(std.testing.allocator);
defer registry.deinit();
diff --git a/src/lightpanda.zig b/src/lightpanda.zig
index 7509c14c0..b0b3419a1 100644
--- a/src/lightpanda.zig
+++ b/src/lightpanda.zig
@@ -230,6 +230,10 @@ fn resolveWaitUntil(opts: FetchOpts) ?Config.WaitUntil {
/// `wait_script` is still unmet (the `wait_until` phase never raises it: when
/// the budget runs out the page is dumped as-is), or `error.Cancelled` if the
/// embedder's opt-in `Session.cancel_hook` returned true.
+fn remainingMs(timer: std.Io.Timestamp, budget_ms: u32) u32 {
+ return budget_ms -| @as(u32, @intCast(timer.untilNow(io, .boot).toMilliseconds()));
+}
+
pub fn fetch(app: *App, browser: *Browser, urls: []const [:0]const u8, opts: FetchOpts) !void {
const notification = try Notification.init(app.allocator);
defer notification.deinit();
@@ -311,7 +315,7 @@ pub fn fetch(app: *App, browser: *Browser, urls: []const [:0]const u8, opts: Fet
var runner = session.runner(.{});
- var timer: std.Io.Timestamp = .now(io, .boot);
+ const timer: std.Io.Timestamp = .now(io, .boot);
if (resolveWaitUntil(opts)) |wu| {
try runner.waitForAll(opts.wait_ms, .{ .until = wu });
@@ -328,7 +332,7 @@ pub fn fetch(app: *App, browser: *Browser, urls: []const [:0]const u8, opts: Fet
err.* = error.FrameClosed;
continue;
};
- const remaining = opts.wait_ms -| @as(u32, @intCast(timer.untilNow(io, .boot).toMilliseconds()));
+ const remaining = remainingMs(timer, opts.wait_ms);
_ = runner.waitForSelector(frame._frame_id, selector, remaining) catch |e| {
err.* = e;
};
@@ -342,7 +346,7 @@ pub fn fetch(app: *App, browser: *Browser, urls: []const [:0]const u8, opts: Fet
err.* = error.FrameClosed;
continue;
};
- const remaining = opts.wait_ms -| @as(u32, @intCast(timer.untilNow(io, .boot).toMilliseconds()));
+ const remaining = remainingMs(timer, opts.wait_ms);
runner.waitForScript(frame._frame_id, wait_script, remaining) catch |e| {
err.* = e;
};
@@ -381,7 +385,7 @@ pub fn fetch(app: *App, browser: *Browser, urls: []const [:0]const u8, opts: Fet
const alternates = try session.arena.allocator().alloc(bool, pages.items.len);
@memset(alternates, false);
if (opts.markdown_alternate) {
- const remaining = opts.wait_ms -| @as(u32, @intCast(timer.untilNow(io, .boot).toMilliseconds()));
+ const remaining = remainingMs(timer, opts.wait_ms);
try followMarkdownAlternates(session, pages.items, errors, remaining, alternates);
}