diff --git a/src/browser/Page.zig b/src/browser/Page.zig index 86374179..8e4e6806 100644 --- a/src/browser/Page.zig +++ b/src/browser/Page.zig @@ -204,10 +204,11 @@ pub fn deinit(self: *Page) void { // stats.print(&stream) catch unreachable; } - // removeContext() will execute the destructor of any type that - // registered a destructor (e.g. XMLHttpRequest). - // Should be called before we deinit the page, because these objects - // could be referencing it. + + // some MicroTasks might be referencing the page, we need to drain it while + // the page still exists + self.js.runMicrotasks(); + const session = self._session; session.executor.removeContext(); diff --git a/src/cdp/cdp.zig b/src/cdp/cdp.zig index 3aef758a..b58ff9b2 100644 --- a/src/cdp/cdp.zig +++ b/src/cdp/cdp.zig @@ -411,15 +411,16 @@ pub fn BrowserContext(comptime CDP_T: type) type { transfer.abort(error.ClientDisconnect); } + for (self.isolated_worlds.items) |*world| { + world.deinit(); + } + self.isolated_worlds.clearRetainingCapacity(); + // If the session has a page, we need to clear it first. The page // context is always nested inside of the isolated world context, // so we need to shutdown the page one first. self.cdp.browser.closeSession(); - for (self.isolated_worlds.items) |*world| { - world.deinit(); - } - self.isolated_worlds.clearRetainingCapacity(); self.node_registry.deinit(); self.node_search_list.deinit(); self.cdp.browser.notification.unregisterAll(self);