Skip to content

Comments

Flush pending microtasks on service worker startup#6134

Open
jasnell wants to merge 1 commit intomainfrom
jasnell/flush-microtask-on-service-worker-startup
Open

Flush pending microtasks on service worker startup#6134
jasnell wants to merge 1 commit intomainfrom
jasnell/flush-microtask-on-service-worker-startup

Conversation

@jasnell
Copy link
Collaborator

@jasnell jasnell commented Feb 22, 2026

Service-worker format scripts (non-module) did not flush the V8 microtask queue after top-level evaluation in the Worker constructor.

When two Workers sharing an isolate (same script, different zones) are constructed sequentially, and leave pending microtasks on the shared per-isolate queue, the first request's microtask checkpoint will then drain microtasks from both contexts, executing the other Worker's callbacks under the wrong IoContext — causing things like `TimeoutId::Generator`` mismatch assertions and potential cross-context state corruption.

The fix is to call lock.runMicrotasks() after NonModuleScript::run(), matching the module path.

A test in workerd (either C++ or wd-test) is currently not possible so the regression test will be added to the internal project.

Service-worker format scripts (non-module) did not
flush the V8 microtask queue after top-level
evaluation in the Worker constructor.

When two Workers sharing an isolate (same script,
different zones) are constructed sequentially,
and leave pending microtasks on the shared
per-isolate queue, the first request's microtask
checkpoint will then drain microtasks from both
contexts, executing the other Worker's callbacks
under the wrong IoContext — causing things like
`TimeoutId::Generator`` mismatch assertions and
potential cross-context state corruption.

The fix is to call `lock.runMicrotasks()` after
`NonModuleScript::run()`, matching the module path.

A test in workerd (either C++ or wd-test) is currently
not possible so the regression test will be added
to the internal project.
@jasnell jasnell requested review from a team as code owners February 22, 2026 19:14
@jasnell
Copy link
Collaborator Author

jasnell commented Feb 22, 2026

A key question is whether this could potentially be a breaking change. It shouldn't be but....

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant