Skip to content

Compile service worker scripts in their first Worker's context - #8513

Merged
csjh merged 2 commits into
mainfrom
cormick/first-worker-context
Nov 23, 2026
Merged

csjh merged 2 commits into
mainfrom
cormick/first-worker-context

Conversation

@csjh

@csjh csjh commented Nov 17, 2026

Copy link
Copy Markdown
Contributor

A service worker script is compiled independent of any context, but compiling it, reporting its errors and converting its globals need an entered one, so Worker::Script created a throwaway context to compile in. That cost every service worker script an extra context, created before the one its first Worker runs in.

This PR has two commits:

  1. Add Worker::Script::newContext(): all of a script's contexts are created with its module registry and schema loader, and each caller spelled both out. They now go through one method.
  2. Compile service worker scripts in their first Worker's context: the Script creates the context its first Worker will run in, compiles in it, and hands it to that Worker. Later Workers of the same Script still create their own contexts. If no Worker takes the context, the Script disposes of it, as it already does with a modules script's shared context.

🤖 Generated with Claude Code

csjh and others added 2 commits November 17, 2026 01:08
A script's contexts are all created with its module registry and
schema loader, and each caller spelled both out. Create them through
one method instead.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A service worker script is compiled independent of any context, but
compiling it, reporting its errors and converting its globals need an
entered one, so the Script created a throwaway context to compile in.
That cost every service worker script an extra context, created
before the one its first Worker runs in.

Create the context the first Worker will run in instead, compile in
it, and hand it to that Worker. Later Workers of the same Script still
create their own contexts. A Script whose context no Worker took
disposes of it, like the shared context of a modules script.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@csjh
csjh requested review from a team as code owners November 17, 2026 01:09

@clanky-the-clanker clanky-the-clanker Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💬 Approved with comments — The context hand-off and disposal paths look correct. The only finding is an INFO about missing tests for the second-Worker and no-Worker paths.

Findings: 1 info (1 is an inline comment on the diff)

The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.


📋 Code Quality: 💬 1 info

Ownership hand-off looks correct; the new lifecycle paths have no tests.

Recommendation: 💬 Optional Nits

  • [INFO] No tests cover the second-Worker and no-Worker paths of the new firstWorkerContext hand-off (src/workerd/io/worker.c++:2427)
View full review

The new firstWorkerContext hand-off looks correct:

  • First Worker: it takes the context and clears it.
  • Later Workers: they create their own context through newContext().
  • No Worker takes the context: ~Script and the constructor's failure path dispose of it.
  • Script fails to compile: the context still goes to the first Worker, which stores permanentException and returns early.

The usual service worker tests only exercise the first-Worker path, so the other paths have no test coverage.


✅ All other checks passed

📝 Documentation: ✅ LGTM

The new comments describe what the code does now, and the header comment on newContext() is accurate.


Reviewed commit: 75e2b3f3

Comment thread src/workerd/io/worker.c++ Resolved
@csjh
csjh merged commit 77c8507 into main Nov 23, 2026
23 checks passed
@csjh
csjh deleted the cormick/first-worker-context branch November 23, 2026 15:25
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.

2 participants