Run the MessageLoop [a lot] more.

Depends on: https://github.com/lightpanda-io/zig-v8-fork/pull/152

We previously ran the message loop every 250ms. This commit changes it to run on
every tick (much more frequently). It also runs microtasks after draining the
message loop (since it can generate microtasks).

Also, we use to run microtasks after each script execution. Now we drain the
message Loop + microtasks.

We still only drain the microtasks when executing v8 callbacks.

As part of this change, we also adjust our wait time based on whether or not
there are pending background tasks in v8 in order to try to execute them (in
general) and in a timely manner.

The goal is to ensure that tasks v8 enqueued on the foreground thread are
executed promptly.

This change is particularly useful for calls to webassembly as compilation
happens in the background and eventually requires the message loop to be drained
to continue.

Previously, if a script did `await WebAssembly.instantiate(....)`, there was
a good chance we'd never finish the code - we'd wait too long to run the
message loop AND, after running it, we wouldn't necessarily resolve the promise.
This commit is contained in:
Karl Seguin
2026-02-25 13:55:35 +08:00
parent a041162b32
commit af7498d283
9 changed files with 71 additions and 23 deletions

View File

@@ -436,17 +436,18 @@ pub fn BrowserContext(comptime CDP_T: type) type {
pub fn deinit(self: *Self) void {
const browser = &self.cdp.browser;
const env = &browser.env;
// Drain microtasks makes sure we don't have inspector's callback
// in progress before deinit.
browser.env.runMicrotasks();
env.runMicrotasks();
// resetContextGroup detach the inspector from all contexts.
// It append async tasks, so we make sure we run the message loop
// before deinit it.
browser.env.inspector.?.resetContextGroup();
browser.runMessageLoop();
browser.env.inspector.?.stopSession();
env.inspector.?.resetContextGroup();
_ = env.pumpMessageLoop();
env.inspector.?.stopSession();
// abort all intercepted requests before closing the sesion/page
// since some of these might callback into the page/scriptmanager