Repository navigation
scripts: stop local engines with SIGTERM so wrangler cleans its tmp dir - #256
ruifigueira wants to merge 1 commit into
Conversation
Each `wrangler dev` start writes a ~40 MB bundle to packages/engine/.wrangler/tmp/dev-* and removes it only in an exit hook. The WPT, Playwright and Puppeteer runners stopped engines with SIGKILL to the process group, which skips that hook. A WPT run starts one engine per lane per batch, so one worktree collected 1,338 dirs (26 GB) and filled the disk. Miniflare's $TMPDIR/miniflare-* dirs leaked the same way, also with the Miniflare launcher. Wrangler only sweeps dirs older than a day. The new scripts/process-group.mjs sends SIGTERM to the group, waits up to 5 s, then sends SIGKILL. Wrangler exits in about 100 ms and removes its dirs. The runners now wait for this in their SIGINT and SIGTERM handlers. A plain exit handler cannot wait, but it still sends SIGTERM first. wpt-batched still stops a finished batch without waiting, so the next batch is not slowed. At the end of a run it removes its state-wpt-<port> persist dirs. wpt-bucket sends SIGTERM to the batched runner first, so the runner can stop its own engines, which run in their own groups. A sweep of new dirs could also delete those of a concurrent `pnpm run dev`: nothing in a dev-* dir names its owner. beep-boop-🤖 Signed-off-by: Rui Figueira <rfigueira@cloudflare.com>
BernardoPe
left a comment
There was a problem hiding this comment.
beep-boop-bernardo-agent-🤖
One blocking issue in the new shutdown path in scripts/wpt-batched.mjs: the final batch's stop promise is dropped, so the last engines are not awaited before removePersistDirs() and process.exit(). That loses the SIGKILL escalation for the final set and races the persist-dir removal. One non-blocking note on error handling in the same cleanup. Details inline.
| if (shuttingDown) return; | ||
| const execMs = Date.now() - execT0; | ||
| killSet(set); | ||
| void killSet(set); |
There was a problem hiding this comment.
beep-boop-bernardo-agent-🤖
Blocking (P2 - Reliability): the final batch's stop is dropped, so its SIGKILL never runs
void discards the stop promise, and killSet clears currentSet as it runs, so the await killWorkerd() before removePersistDirs() has nothing left to wait for. The run then calls process.exit(), which drops the pending grace timer. An engine in the last batch that ignores SIGTERM is never killed, keeps its port, and removePersistDirs() deletes state-wpt-<port> while that engine is still shutting down, which is the window the comment on removePersistDirs warns about.
The next run then exits 2 in refuseHeldPorts(), and the ignored-SIGTERM warning never prints. The .wrangler/tmp cleanup still happens because each engine gets SIGTERM itself, but the port lockout is new: before this change killSet sent SIGKILL synchronously.
Keep the promise and join it before removing directories:
const pendingStops = [];
// line 880
pendingStops.push(killSet(set));
// before removePersistDirs(), and in the signal handler
await killWorkerd();
await Promise.all(pendingStops);| killSet(spareSet); | ||
| // Call only once every engine has stopped: a later set on the same port recreates its directory. | ||
| function removePersistDirs() { | ||
| for (const port of runPorts()) rmSync(persistDirFor(port), { recursive: true, force: true }); |
There was a problem hiding this comment.
beep-boop-bernardo-agent-🤖
Non-blocking (P3 - Reliability): a failed removal skips the rest of shutdown
rmSync with force ignores only a missing path. If a writer creates a file between the directory read and the removal, it throws ENOTEMPTY, and nothing catches it. On the normal path the rejection escapes the async IIFE and replaces the intended exit code with 1; in the signal handler it skips stopWptServe() and process.exit(130), so an --start-server run leaves wpt serve on 8000/8443. This can wait, but the guard is two lines:
function removePersistDirs() {
for (const port of runPorts()) {
try {
rmSync(persistDirFor(port), { recursive: true, force: true });
} catch {}
}
}
Summary
Local WPT, Playwright and Puppeteer runs leak a temporary bundle for each engine they stop. One worktree collected 1,338 directories (26 GB) in
packages/engine/.wrangler/tmpafter a few WPT runs, and the disk filled up. This pull request makes the runners stop engines so that wrangler and Miniflare can clean up.Behavior and API impact
None for the engine. Only the local test runners change.
Implementation
Each
wrangler devstart writes a bundle of about 40 MB topackages/engine/.wrangler/tmp/dev-*. Wrangler removes it only in an exit hook. The runners stopped engines with SIGKILL to the process group, so the hook never ran. Miniflare's$TMPDIR/miniflare-*directories leaked the same way, also with the Miniflare launcher.scripts/process-group.mjssends SIGTERM to the group, waits up to 5 seconds, then sends SIGKILL. Wrangler exits in about 100 ms and removes its directories. The helper has a unit test.wpt-batched,local-engine(used by the Playwright and Puppeteer runners) andwpt-bucketuse the helper.wpt-batchedstill stops a finished batch without waiting, so the next batch does not start later.wpt-batchedremoves itsstate-wpt-<port>directories (--persist-to).wpt-bucketsends SIGTERM to the batched runner first, so the runner can stop its own engines. The engines run in their own process groups.The runners do not sweep new directories after a run. A sweep could delete the directories of a concurrent
pnpm run dev, because nothing in adev-*directory names its owner.Validation
New entries left after a run of 34 WPT tests (
-j 2 --batch-size 8, 5 batches):.wrangler/tmpstate-wpt-*$TMPDIR/miniflare-*local-enginestop(), and exit withoutstop()No
wrangler,workerdorwpt serveprocess was left after any run.pnpm run check: passes. Locally,//:cargo_lock_testpasses only with--test_env=PATH=$HOME/.cargo/bin:..., because a localrustc-wrappersetting is not in the sandbox. This change touches no Rust.pnpm run lint:js: passes.node --test scripts/process-group.test.mjs: passes.I did not run the Playwright or Puppeteer suites. Only
local-engine's stop paths were tested directly.Risks and follow-ups
stopWptServerunslsof -ti:8000 | xargs kill -9, which also matches the runner's own connections to port 8000.packages/engine/.wpt-dist/, the default Miniflare dist path, is not in.gitignore, so a local Miniflare run makeslint:jsfail.Checklist
Signed-off-byline (git commit -s); see CONTRIBUTING.md.beep-boop-🤖