Skip to content

Lightpanda: the headless browser designed for AI and automation

guard against post-close navigate

Karl Seguin36fec926ccd8
diff --git a/src/browser/Session.zig b/src/browser/Session.zig
index 99d3ce406..2f0b08cae 100644
--- a/src/browser/Session.zig
+++ b/src/browser/Session.zig
@@ -487,6 +487,16 @@ pub fn processQueuedNavigation(self: *Session) !void {
fn processFrameNavigation(self: *Session, frame: *Frame, qn: *QueuedNavigation) !void {
frame._queued_navigation = null;
defer self.releaseArena(qn.arena);
+
+ // A popup whose window was close()'d is parked in page.closed_frames and
+ // torn down at Page.deinit. It must never be navigated. This navigation
+ // can happen _after_ window.close() has been called (because JS can do
+ // anything with that window), so the safest place to prevent navigation
+ // from happening is here.
+ if (frame.window._closed) {
+ return;
+ }
+
self._processFrameNavigation(frame, qn) catch |err| {
log.warn(.frame, "frame navigation", .{ .url = qn.url, .err = err });
return err;
diff --git a/src/browser/webapi/Window.zig b/src/browser/webapi/Window.zig
index ed7b695a6..a02458509 100644
--- a/src/browser/webapi/Window.zig
+++ b/src/browser/webapi/Window.zig
@@ -603,8 +603,7 @@ pub fn close(self: *Window) void {
_ = page.popups.swapRemove(popup_index);
- // Drop any pending queued navigation for this frame, otherwise
- // processQueuedNavigation would try to re-navigate the closed popup.
+ // Drop any pending queued navigation for this frame.
if (frame._queued_navigation != null) {
for (page.queued_navigation.items, 0..) |f, i| {
if (f == frame) {