fix(codemode): dispose dynamic worker and entrypoint stub after execute#1814
Merged
Conversation
DynamicWorkerExecutor.execute() spins up a child Worker via loader.load()
and an RPC Fetcher stub via getEntrypoint(), but left both for GC. When a
native handle is finalized late (e.g. during isolate shutdown under
@cloudflare/vitest-pool-workers) workerd raises a fatal assertion ("tried
to defer destruction during isolate shutdown") that kills the worker,
surfacing as a flaky "Worker exited unexpectedly" with no failing
assertion. The milder manifestation is workerd's "An RPC result was not
disposed properly" warning.
Dispose the entrypoint stub and the loaded worker in finally blocks
(best-effort via Symbol.dispose) so the handles are released while the
isolate is still alive. No behavior or API change for callers.
Co-authored-by: Cursor <cursoragent@cursor.com>
🦋 Changeset detectedLatest commit: e956b69 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
agents
@cloudflare/ai-chat
@cloudflare/codemode
create-think
hono-agents
@cloudflare/shell
@cloudflare/think
@cloudflare/voice
@cloudflare/worker-bundler
commit: |
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.
Summary
Fixes a flaky
@cloudflare/thinkworkers-test failure that surfaces in CI as an uncatchable "Worker exited unexpectedly" (example release run). The underlying crash is a fatal workerd assertion during isolate teardown:40 passed (41)= every assertion passed, but one file's worker process died before reporting, so vitest reds the whole run. Because it's a process crash (not an assertion failure or timeout), the suite's existingretry: 3/teardownTimeout: 60_000can't catch it.Root cause
DynamicWorkerExecutor.execute()spins up a child Worker vialoader.load()and gets an RPCFetcherstub viagetEntrypoint(), but left both for the garbage collector. These own native handles; when one is finalized late (e.g. during isolate shutdown under@cloudflare/vitest-pool-workers) workerd trips the deferred-destruction assertion and kills the worker. The milder, nondeterministic manifestation of the same leak is workerd'sAn RPC result was not disposed properlywarning, which I also observed in the@cloudflare/codemodesuite.This is the only production
loader.load()/getEntrypoint()call site in the repo;@cloudflare/shellreuses this same executor, so both are covered.Fix
Dispose the entrypoint stub and the loaded worker in
finallyblocks (best-effort viaSymbol.dispose), releasing the handles while the isolate is still alive. No behavior or API change for callers.Test plan
pnpm run typecheck(all 113 projects)executor.ts@cloudflare/codemodesuite passes; runtime tests run clean (no RPC-not-disposed warning)@cloudflare/thinktest:workerscrashed ~1-in-2 full runstest:workerssuite with the rebuilt codemode (iter 1 =41 passed (41), no crash)Note: dependency bumps (
workerd/@cloudflare/vitest-pool-workers) will be handled separately and should further harden this teardown path.Made with Cursor