Skip to content

scripts: stop local engines with SIGTERM so wrangler cleans its tmp dir - #256

Open
ruifigueira wants to merge 1 commit into
mainfrom
rfigueira/wpt-runner-wrangler-tmp
Open

ruifigueira wants to merge 1 commit into
mainfrom
rfigueira/wpt-runner-wrangler-tmp

Conversation

@ruifigueira

Copy link
Copy Markdown
Collaborator

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/tmp after 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 dev start writes a bundle of about 40 MB to packages/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.

  • The new scripts/process-group.mjs sends 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) and wpt-bucket use the helper.
  • The SIGINT and SIGTERM handlers wait for the engines to stop. A plain exit handler cannot wait, but it still sends SIGTERM.
  • wpt-batched still stops a finished batch without waiting, so the next batch does not start later.
  • At the end of a run, wpt-batched removes its state-wpt-<port> directories (--persist-to).
  • wpt-bucket sends 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 a dev-* directory names its owner.

Validation

New entries left after a run of 34 WPT tests (-j 2 --batch-size 8, 5 batches):

Run .wrangler/tmp state-wpt-* $TMPDIR/miniflare-*
Before, normal run +20 (39 MB → 425 MB) +2 +20
Before, SIGINT during the run +8 +2 +8
Before, Miniflare launcher 0 0 +12
After, normal run (34/34 as expected, 30 s) 0 0 0
After, SIGINT during the run 0 0 0
After, SIGTERM during the run 0 0 0
After, Miniflare launcher 0 0 0
After, local-engine stop(), and exit without stop() 0 0 0

No wrangler, workerd or wpt serve process was left after any run.

  • pnpm run check: passes. Locally, //:cargo_lock_test passes only with --test_env=PATH=$HOME/.cargo/bin:..., because a local rustc-wrapper setting 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

  • An engine that ignores SIGTERM gets SIGKILL after 5 seconds, and the runner prints a warning that its bundle can remain.
  • Not changed here: an interrupted run exits with 137 instead of 130, before and after this change. stopWptServe runs lsof -ti:8000 | xargs kill -9, which also matches the runner's own connections to port 8000.
  • Not changed here: packages/engine/.wpt-dist/, the default Miniflare dist path, is not in .gitignore, so a local Miniflare run makes lint:js fail.

Checklist

  • This pull request solves one problem; see CONTRIBUTING.md.
  • Every commit has a Signed-off-by line (git commit -s); see CONTRIBUTING.md.
  • Added or updated focused tests for behavior changes.
  • Updated documentation and generated files, or confirmed they do not apply.

beep-boop-🤖

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>
@github-actions github-actions Bot added the vouch:trusted Author is vouched, a collaborator, or a bot. label Nov 27, 2026
@BernardoPe BernardoPe assigned BernardoPe and unassigned BernardoPe Nov 27, 2026

@BernardoPe BernardoPe left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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.

Comment thread scripts/wpt-batched.mjs
if (shuttingDown) return;
const execMs = Date.now() - execT0;
killSet(set);
void killSet(set);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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);

Comment thread scripts/wpt-batched.mjs
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 });

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

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 {}
  }
}

@BernardoPe BernardoPe removed their assignment Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

vouch:trusted Author is vouched, a collaborator, or a bot.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants