Do not reset transfer_arena if page navigation results in delayed navigation

We normally expect a navigation event to happen at some point after the page
loads, like a puppeteer script clicking on a link. But, it's also possible for
the main navigation event to result in a delayed navigation. For example, an
html page with this JS:

<script>top.location = '/';</script>

Would result in a delayed navigation being called from the main navigate
function. In these cases, we cannot clear the transfer_arena when navigate is
completed, as its memory is needed by the new "sub" delayed navigation.
This commit is contained in:
Karl Seguin
2025-06-02 14:16:36 +08:00
parent 747a8ad09c
commit 4644e55883
2 changed files with 12 additions and 1 deletions

View File

@@ -92,6 +92,9 @@ pub const Page = struct {
// current_script could by fetch module to resolve module's url to fetch.
current_script: ?*const Script = null,
// indicates intention to navigate to another page on the next loop execution.
delayed_navigation: bool = false,
pub fn init(self: *Page, arena: Allocator, session: *Session) !void {
const browser = session.browser;
self.* = .{
@@ -580,6 +583,7 @@ pub const Page = struct {
// The page.arena is safe to use here, but the transfer_arena exists
// specifically for this type of lifetime.
pub fn navigateFromWebAPI(self: *Page, url: []const u8, opts: NavigateOpts) !void {
self.delayed_navigation = true;
const arena = self.session.transfer_arena;
const navi = try arena.create(DelayedNavigation);
navi.* = .{

View File

@@ -128,7 +128,14 @@ pub const Session = struct {
// it isn't null!
std.debug.assert(self.page != null);
defer _ = self.browser.transfer_arena.reset(.{ .retain_with_limit = 1 * 1024 * 1024 });
defer if (self.page) |*p| {
if (!p.delayed_navigation) {
// If, while loading the page, we intend to navigate to another
// page, then we need to keep the transfer_arena around, as this
// sub-navigation is probably using it.
_ = self.browser.transfer_arena.reset(.{ .retain_with_limit = 1 * 1024 * 1024 });
}
};
// it's safe to use the transfer arena here, because the page will
// eventually clone the URL using its own page_arena (after it gets