Flush pending microtasks on service worker startup#6134
Open
Conversation
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.
Collaborator
Author
|
A key question is whether this could potentially be a breaking change. It shouldn't be but.... |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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()afterNonModuleScript::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.