vm: support shared microtask queues - #65582
Conversation
Add `vm.MicrotaskQueue` and `options.microtaskQueue` to allow multiple contexts to share an explicit V8 microtask queue and let users control synchronous checkpoint draining. Fixes: nodejs#65555 Signed-off-by: Alessio Attilio <alessio.attilio@protonmail.com> Assisted-by: Antigravity
| * `microtaskQueue` {vm.MicrotaskQueue} A microtask queue created with | ||
| [`new vm.MicrotaskQueue()`][] or [`vm.createMicrotaskQueue()`][]. If | ||
| specified, microtasks scheduled inside this context will be placed on this | ||
| queue, allowing multiple contexts to share the same microtask queue. |
There was a problem hiding this comment.
You could make this an alternative value for microtaskMode, but if not, you should definitely document how these options interact
There was a problem hiding this comment.
options.microtaskQueue and options.microtaskMode remain separate options because they are orthogonal: the former selects the target microtask queue (isolate default vs. shared vm.MicrotaskQueue), while the latter defines the draining behavior (automatic post-evaluation checkpointing via 'afterEvaluate' vs. manual draining via undefined). Keeping them distinct allows contexts to share a queue while independently configuring evaluation draining behavior. Updated documentation in doc/api/vm.md across vm.createContext(), vm.runInNewContext(), script.runInNewContext(), and vm.MicrotaskQueue covers all combinations.
Document how options.microtaskQueue and options.microtaskMode interact across createContext, runInNewContext, and script.runInNewContext. Update receiver type error expectation for runMicrotasks and add test coverage for runInNewContext with microtask queues. Signed-off-by: Alessio Attilio <alessio.attilio@protonmail.com> Assisted-by: Antigravity
This PR introduces vm.MicrotaskQueue and the options.microtaskQueue setting in node:vm, allowing multiple contexts to share an explicit V8 microtask queue and giving authors synchronous control over draining via queue.runMicrotasks(). This enables standards-compliant event loop integration such as HTML window and same-agent iframe microtask checkpointing without premature queue draining.
Fixes: #65555