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

@@ -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