From 080b1d9a7cbe4c1ad592003748cea19607970769 Mon Sep 17 00:00:00 2001 From: Pierre Tachoire Date: Wed, 8 Oct 2025 13:55:17 +0200 Subject: [PATCH] drain micro task queue before reset ExecutionWorld and page --- src/browser/js/ExecutionWorld.zig | 6 ++++++ src/browser/page.zig | 3 +++ 2 files changed, 9 insertions(+) diff --git a/src/browser/js/ExecutionWorld.zig b/src/browser/js/ExecutionWorld.zig index 04522a04..73603227 100644 --- a/src/browser/js/ExecutionWorld.zig +++ b/src/browser/js/ExecutionWorld.zig @@ -231,6 +231,12 @@ pub fn createContext(self: *ExecutionWorld, page: *Page, enter: bool, global_cal } pub fn removeContext(self: *ExecutionWorld) void { + // Force running the micro task to drain the queue before reseting the + // context arena. + // Tasks in the queue are relying to the arena memory could be present in + // the queue. Running them later could lead to invalid memory accesses. + self.env.runMicrotasks(); + self.context.?.deinit(); self.context = null; _ = self.context_arena.reset(.{ .retain_with_limit = CONTEXT_ARENA_RETAIN }); diff --git a/src/browser/page.zig b/src/browser/page.zig index ca36cd09..6cdc4f96 100644 --- a/src/browser/page.zig +++ b/src/browser/page.zig @@ -161,6 +161,9 @@ pub const Page = struct { } fn reset(self: *Page) void { + // Force running the micro task to drain the queue. + self.session.browser.env.runMicrotasks(); + self.scheduler.reset(); self.http_client.abort(); self.script_manager.reset();