fix(controller): stop leaking test tempdirs (closes #273) - #274
Merged
Conversation
The three `test_state`-style constructors in `http.rs` called `std::mem::forget` on their `TempDir` to keep the scratch directory alive for the duration of the test. That works, but Drop never runs, so every `cargo test -p forkd-controller` orphaned one directory per construction in $TMPDIR -- 56 of them per run on today's test count. Bind the `TempDir` to the state instead: a `#[cfg(test)] _tempdir` field on `AppState`, declared last so it drops after `registry`. Dropping the last `Arc` now reaps the directory. This mirrors the `_td: TempDir` ownership pattern the integration tests in `tests/http_integration.rs` already use. While here, collapse the two duplicated inline `AppState` literals in `branch_slot_global_cap_blocks` and `branch_slot_capacity_recovers_on_drop` into a shared `test_state_with_cap(cap)`, and move `prewarm_scratch_dir` off the shared `$TMPDIR/forkd-test-prewarm` into the per-test tempdir so nothing the daemon writes escapes it. Verified on Linux: leftover `/tmp/.tmp*` after `cargo test -p forkd-controller` goes 56 -> 0, with the same 71 + 7 tests passing. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
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.
Closes #273.
The leak
test_stateand the twobranch_slot_*tests incrates/forkd-controller/src/http.rseach built their scratch directory like this:It keeps the directory alive, but
Dropnever runs — so everycargo test -p forkd-controllerorphans one directory per construction in$TMPDIR. The issue reported ~10 per run; on today's test count it is 56.The fix
Bind the
TempDirto the state rather than leaking it — the "cheapest" option from the issue.AppStategains a#[cfg(test)] pub _tempdir: Option<tempfile::TempDir>field, declared last so it drops afterregistry. Dropping the finalArcnow reaps the directory.This mirrors the
_td: TempDirownership pattern thattests/http_integration.rsalready uses, so the unit tests and the integration tests handle scratch dirs the same way.Two cleanups carried along:
AppStateliterals inbranch_slot_global_cap_blocksandbranch_slot_capacity_recovers_on_dropcollapse into a sharedtest_state_with_cap(cap);test_state()delegates to it. Net −24 lines, and there is now one place to update whenAppStategrows a field.prewarm_scratch_dirmoves off the shared$TMPDIR/forkd-test-prewarminto the per-test tempdir, so nothing the daemon writes escapes it.Verification
Run on Linux, using the reporter's own measurement (
ls -d /tmp/.tmp* | wc -lafter acargo test -p forkd-controllerin a fresh container):/tmp/.tmp*Full CI matrix green locally:
cargo fmt --all -- --check,cargo clippy --all-targets --all-features -- -D warnings(no warnings),cargo build --all,cargo test --all.Notes
#[cfg(test)]only applies to the lib crate's own unit-test build, so the field does not exist for integration tests or downstream consumers; the single production construction site inlib.rsgets a matching#[cfg(test)] _tempdir: None.mem::forget,ManuallyDrop,TempDir::into_path(), or.keep()anywhere incrates/.🤖 Generated with Claude Code