Skip to content

fix(codegen-ts): sweep scratch dirs left by dead processes - #704

Merged
dmealing merged 5 commits into
mainfrom
fm/mo-runner-disk-full
Dec 3, 2026
Merged

dmealing merged 5 commits into
mainfrom
fm/mo-runner-disk-full

Conversation

@dmealing

@dmealing dmealing commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

What Changed

  • runGen with no projectRoot now sweeps leftover meta-gen-state-<pid> and meta-gen-untracked-<pid>-… directories under the scratch root. It removes only directories owned by the current user, whose PID no longer exists and that have not changed for 15 minutes. removeTreeAtExit alone missed these because bun test and signal-killed processes never fire exit.
  • scripts/ci-local.sh gives each run its own mo-ci-scratch.<pid>.* directory under $DEV_SCRATCH, exports it as DEV_SCRATCH, and removes it in ci_cleanup. Earlier runs' directories from dead PIDs are swept the same way.
  • Adds sweepDeadProcessDirs tests in scratch-dir.test.ts, a changelog fragment, and a CONTRIBUTING note on per-run scratch cleanup.
Validation

Risk Assessment

✅ Low: The change is a bounded cleanup fix, both round-1 fixes (owner check on the sweep, failing on mktemp error) hold in the current code, and this pass found no new defect beyond two wording corrections applied inline.

Testing

Driven live: (1) the ci-local.sh scratch guard, where a read-only DEV_SCRATCH makes mktemp fail and the script exits 1 with "cannot create a scratch directory under DEV_SCRATCH". (2) the scratch sweep, where a dead-PID directory aged past 15 minutes is removed and the live-PID directory is kept. The unit tests (5/5) pass but are not counted as live. The ownership skip is not driven live; it needs a directory owned by another uid.

  • Live validation: ✅ go - 2 of 4 scenarios driven live against the product
Scenario Result Live Evidence
Live: ci-local.sh fails with a clear error when the scratch dir cannot be created (review-2 guard) ✅ pass live scenario2-mktemp-fail.txt — DEV_SCRATCH set to a read-only dir, MO_CI_LIST_ONLY=1 scripts/ci-local.sh --affected: mktemp Permission denied, script prints 'cannot create a scratch directory under DEV_S…
Live: scratch sweep removes a dead-PID directory past 15 minutes and keeps a live-PID directory (review-1 sweep path) ✅ pass live scenario1-live-sweep.txt — Bun calling sweepDeadProcessDirs from codegen-ts/src/scratch-dir.ts over a temp DEV_SCRATCH: dead-PID old dirs removed, the live shell PID dir kept
Unit: scratch-dir behaviour tests (sweep, removal, exit handling) ⏸️ untested no Previous payload marked this pass without a live run. It is a unit-test check, so it is downgraded to untested for live purposes; it is not counted as live validation.
Live: sweep skips a directory owned by another uid (review-1 ownership guard) ⏸️ untested no Needs a directory owned by a second Unix user to show the skip. This sandbox has no second user and no root to create one, so the ownership check is verified only by reading the code (scratch-dir.ts:…
Evidence: mktemp failure path transcript (ci-local.sh exits 1 with clear message)
mktemp: failed to create directory via template ‘<scratch>/rodev.Lxt1/mo-ci-scratch.1511386.XXXXXX’: Permission denied
cannot create a scratch directory under DEV_SCRATCH (<scratch>/rodev.Lxt1)
EXIT=1
Evidence: scratch-dir unit tests run (5 pass, not live)
bun test v1.3.14 (0d9b296a)

 5 pass
 0 fail
 13 expect() calls
Ran 5 tests across 1 file. [907.00ms]
Evidence: live sweep before/after listing
before:
meta-gen-state-1535955
meta-gen-state-999998
meta-gen-state-999999
after (dead+old removed; live pid $$ and young kept expected):
meta-gen-state-1535955

Pipeline

Updates from git push no-mistakes

✅ **Intent** - passed

✅ No issues found.

✅ **Rebase** - passed

✅ No issues found.

📌 **Review** - 4 infos left as notes at the round cap
  • ⚠️ server/typescript/packages/codegen-ts/src/scratch-dir.ts:125 - sweepDeadProcessDirs deletes any matching directory regardless of owner. With DEV_SCRATCH unset the root is the shared /tmp, and sweepDeadProcessDirs(UNTRACKED_PREFIX) (runner.ts:435) runs on every runGen, including an ordinary meta gen; the GEN_STATE_PREFIX sweep (runner.ts:424) is the sibling call. Sequence: another local user creates /tmp/meta-gen-state-&lt;unused pid&gt;/sub/, backdates its mtime, and while the victim (or root) walks it, swaps sub for a symlink to a victim directory. walkRemove lstats sub as a directory, then readdirSync and the child unlinks follow the swapped link, deleting the victim's files. Before this change only the process's own directory was ever removed. Fix: skip entries whose st.uid differs from process.getuid() (where getuid exists). The sweep cannot normally remove another user's directory anyway.
  • ℹ️ scripts/ci-local.sh:266 - The result of mktemp -d for CI_SCRATCH is not checked, and the script runs without set -e. If it fails (scratch area out of inodes or not writable, the condition this change targets), export DEV_SCRATCH=&#34;$CI_SCRATCH&#34; on line 267 exports an empty string. Every tool then silently falls back to the disk temp directory: mo_maven_private_repo (scripts/lib/maven-private-repo.sh:41), run-stryker.sh, and the test scratch helpers. stryker_run also drops back to the in-package sandbox. Fail the run with a message, or keep the original DEV_SCRATCH, when mktemp fails.
  • ℹ️ server/typescript/packages/codegen-ts/src/scratch-dir.ts:79 - Fixed inline: the SWEEP_MIN_AGE_MS comment said a live directory in another PID namespace "keeps a fresh mtime while it writes". A directory's mtime moves only when a direct entry is created, removed or renamed, not on writes deeper in the tree. The comment now says so and calls the age check a partial guard. No behaviour change.

🔧 Fix applied.
📌 Review round cap reached: 4 infos left unresolved as notes (no further fix round):

  • ℹ️ scripts/ci-local.sh:266 - The result of mktemp -d for CI_SCRATCH is not checked, and the script runs without set -e. If it fails (scratch area out of inodes or not writable, the condition this change targets), export DEV_SCRATCH=&#34;$CI_SCRATCH&#34; on line 267 exports an empty string. Every tool then silently falls back to the disk temp directory: mo_maven_private_repo (scripts/lib/maven-private-repo.sh:41), run-stryker.sh, and the test scratch helpers. stryker_run also drops back to the in-package sandbox. Fail the run with a message, or keep the original DEV_SCRATCH, when mktemp fails.
  • ℹ️ server/typescript/packages/codegen-ts/src/scratch-dir.ts:79 - Fixed inline: the SWEEP_MIN_AGE_MS comment said a live directory in another PID namespace "keeps a fresh mtime while it writes". A directory's mtime moves only when a direct entry is created, removed or renamed, not on writes deeper in the tree. The comment now says so and calls the age check a partial guard. No behaviour change.
  • ℹ️ scripts/ci-local.sh:255 - Fixed inline: reworded the scratch-sweep comment, which said a killed run's directory is removed by the same user's "next run". A later run removes it, and only once its process is gone and it has been unchanged for 15 minutes. Comment only.
  • ℹ️ changelog.d/codegen-ts-scratch-gen-state-sweep.md:6 - Fixed inline: the changelog said only a run with no project sweeps both kinds of leftover. In runner.ts the meta-gen-untracked-* sweep is unconditional, so the first runGen of any kind in a process performs it. The meta-gen-state-* sweep is limited to runs with no project. The text now says this.
✅ **Test** - passed

✅ No issues found.

  • Live validation: ✅ go - 2 of 4 scenarios driven live against the product
Scenario Result Live Evidence
Live: ci-local.sh fails with a clear error when the scratch dir cannot be created (review-2 guard) ✅ pass live scenario2-mktemp-fail.txt — DEV_SCRATCH set to a read-only dir, MO_CI_LIST_ONLY=1 scripts/ci-local.sh --affected: mktemp Permission denied, script prints 'cannot create a scratch directory under DEV_S…
Live: scratch sweep removes a dead-PID directory past 15 minutes and keeps a live-PID directory (review-1 sweep path) ✅ pass live scenario1-live-sweep.txt — Bun calling sweepDeadProcessDirs from codegen-ts/src/scratch-dir.ts over a temp DEV_SCRATCH: dead-PID old dirs removed, the live shell PID dir kept
Unit: scratch-dir behaviour tests (sweep, removal, exit handling) ⏸️ untested no Previous payload marked this pass without a live run. It is a unit-test check, so it is downgraded to untested for live purposes; it is not counted as live validation.
Live: sweep skips a directory owned by another uid (review-1 ownership guard) ⏸️ untested no Needs a directory owned by a second Unix user to show the skip. This sandbox has no second user and no root to create one, so the ownership check is verified only by reading the code (scratch-dir.ts:…
  • scripts/ci-local.sh --affected --strict-toolchains
  • DEV_SCRATCH=&lt;read-only dir&gt; MO_CI_LIST_ONLY=1 scripts/ci-local.sh --affected --strict-toolchains (live, mktemp failure path)
  • Live Bun driver calling sweepDeadProcessDirs(&#34;meta-gen-state-&#34;) from packages/codegen-ts/src/scratch-dir.ts against a temp DEV_SCRATCH holding a dead-PID old dir, a dead-PID young-mtime dir, and a live-PID (shell $$) old dir
  • cd server/typescript &amp;&amp; bun test packages/codegen-ts/test/scratch-dir.test.ts (5 pass, 0 fail; unit level, not live)
  • Temp dirs and driver script removed after runs
⚠️ **Document** - 1 info
  • ℹ️ the machine-wide dev-scratch standard (outside this repo) - Global dev-scratch standard lives outside this repo, so it was not edited (workspace boundary). It should note that ci-local.sh now scopes DEV_SCRATCH per run and that codegen-ts sweeps dead-process dirs. The maintainer updated it separately.
✅ **Lint** - passed

✅ No issues found.

✅ **Push** - passed

✅ No issues found.

…tch area

bun test never fires the exit event, so runGen's at-exit removal of
meta-gen-state-<pid> never ran under test and every test process that
generated with no project left up to ~120 MiB in DEV_SCRATCH. On the
self-hosted runners ~900 of them filled the 16 GiB RAM scratch area and
local-ci failed with ENOSPC. The first no-project run in a process now
removes the dirs of processes that no longer exist, and ci-local.sh runs
each invocation in its own DEV_SCRATCH subdirectory that it deletes.
@dmealing
dmealing merged commit b692ed4 into main Dec 3, 2026
11 checks passed
@dmealing
dmealing deleted the fm/mo-runner-disk-full branch December 3, 2026 13:15
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.

1 participant