Skip to content

fix(tests): seed tempfile's name generator per process - #4693

Merged
max-sixty merged 6 commits into
mainfrom
fix/issue-4692
Nov 4, 2026
Merged

max-sixty merged 6 commits into
mainfrom
fix/issue-4692

Conversation

@worktrunk-bot

@worktrunk-bot worktrunk-bot commented Nov 4, 2026 •

Copy link
Copy Markdown
Collaborator

On Windows CI, temp entries under D:\tmp failed with Access is denied because concurrent test processes were generating the same names. tempfile takes its names from fastrand, and fastrand seeds its thread-local generator only from Instant::now() and the thread id (smol-rs/fastrand#130). Under nextest each test is a separate process whose thread ids start from the same values, so two processes that seed in the same clock tick generate the same names. If the duplicate name is held by a live entry, tempfile gets AlreadyExists and retries. If another process has just deleted that entry, Windows returns PermissionDenied (os error 5), which tempfile passes to the caller. That is the failure in #3613, #3619 and the TempDir::new() panic in #4692.

The suite's temp helpers now reseed that generator per process:

  • worktrunk::testing::tempdir() and named_tempfile() replace tempfile::tempdir(), TempDir::new() and NamedTempFile::new() at every test call site, including wt-perf. test_tempdir() and the fixture-template scratch directory go through the same builder. On a thread's first use, the builder calls fastrand::seed with a value from RandomState::new(), which std keys from OS randomness, so the seed differs between processes even when the clock reading matches. A probe confirmed that seeding fastrand fixes the next tempfile name, so both crates share the one generator. Names keep tempfile's .tmpXXXXXX length, which matters because a pid prefix pushed the macOS fsmonitor socket path past sun_path.
  • A new root clippy.toml uses disallowed-methods to reject those three constructors in new code. The one production caller, format_migration_diff, keeps tempfile::tempdir() with a call-site #[expect].
  • fastrand becomes a direct dependency. It resolves to the same 2.5.0 that tempfile already uses.
Windows reproduction

I ran a temporary workflow on windows-2025 with TMP=D:\tmp, set up the way ci.yaml does it. Batches of short-lived processes each created TempDirs and NamedTempFiles with tempfile 3.27.0 and fastrand 2.5.0, and the parent recorded every name and error:

processes × parallel × per-process names created more than once Access is denied
3000 × 16 × 5 86 / 30,000 5
3000 × 64 × 5 49 / 30,000 8
400 × 16 × 200 763 / 160,000 14

All 27 denials hit a name that another process had also generated. The same program on Linux generated no duplicates in 30,000 names.

This PR doesn't rerun that experiment against the new helpers either. The Windows test leg on this PR is the check, and the recurrence rate (#3619 measured 4 failures in 80 loop runs) means one green leg is weak evidence.

Locally, clippy is clean across the workspace, and the lib tests covering the directive helpers, cache:: and the template claim pass. An earlier full cargo nextest run --all-features of the pid-prefix version passed 5473 of 5480, and the 7 failures fail the same way on main in this sandbox. The sandbox ran out of disk before the full suite could run on the final version, so CI is the check for it.

Not changed: production wt also creates tempfile names in shared directories, so concurrent wt processes on Windows share the same weak seed. The temp-index path already falls back to the Git directory on any error.

Closes #4692 — automated triage

tempfile draws names from fastrand, whose thread-local generator seeds
from the time and the thread id alone (smol-rs/fastrand#130). Nextest runs
each test in its own process, so processes that seed in the same clock
tick generate the same names. A duplicate that hits an entry another
process just deleted fails on Windows with PermissionDenied (os error 5),
which tempfile does not retry.

A stress run on windows-2025 with TMP=D:\tmp generated duplicate names
in ~0.3% of creations across short-lived processes (0 of 30,000 on
Linux), and all 27 Access-is-denied failures were on duplicated names.

worktrunk::testing::{tempdir, named_tempfile} and test_tempdir now prefix
names with the process id, every test call site uses them, and clippy's
disallowed-methods keeps tempfile::tempdir, TempDir::new and
NamedTempFile::new out of new tests.

Closes #4692
@worktrunk-bot worktrunk-bot added the automated-fix Automated CI fix label Nov 4, 2026
@max-sixty

Copy link
Copy Markdown
Owner

@worktrunk-bot src/config/deprecation.rs now makes the production format_migration_diff path call crate::testing::tempdir(). That contradicts the test-only scope described here and the base AGENTS.md guidance to keep test helpers out of library code. Please keep the production path independent of crate::testing—either narrow the new disallowed-methods rule so this production constructor can remain, or move a shared constructor into a production module and have the test helper call it. Please also update the PR description to account for the production change.

This was written by Codex on behalf of max-sixty

format_migration_diff is production code, so it goes back to
tempfile::tempdir() with a call-site expect on the disallowed-methods rule,
which only targets the test suite.

Co-Authored-By: Claude <noreply@anthropic.com>
@worktrunk-bot

Copy link
Copy Markdown
Collaborator Author

I narrowed the rule and left the production code alone. In 3bceb98, format_migration_diff goes back to tempfile::tempdir(), and a call-site #[expect(clippy::disallowed_methods, reason = …)] exempts it from the rule. I chose this over a shared production constructor because a shared one would change wt's own temp names, which is outside this PR's test-only scope. clippy.toml now says that production code opts out at the call site.

None of the PR's other crate::testing calls are in production code; each is in a #[cfg(test)] module or a test file. The PR description now covers this. cargo clippy --all-targets --all-features reports no warnings, and the deprecation unit tests pass (186).

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

HOST_MARKERS in tests/integration_tests/snapshot_formatting_guard.rs still matches tempfile names with \.tmp[A-Za-z0-9]{6}. The new names have the form .tmp<pid>-XXXXXX, so the pattern matches only when the pid has 6 or more digits. test_no_host_specific_paths_in_snapshots therefore stops catching an unredacted temp path in a committed snapshot whenever the generating process had a pid under 100000. That covers essentially every macOS run (pids top out at 99998) and most Windows runs. The file is outside the diff, so I'm pushing the fix: the pattern becomes \.tmp(?:\d+-)?[A-Za-z0-9]{6}, and its doc comment is updated to match.

…uard

worktrunk::testing's temp helpers now name entries .tmp<pid>-XXXXXX, which
\.tmp[A-Za-z0-9]{6} only matched for pids of six or more digits.
@worktrunk-bot

Copy link
Copy Markdown
Collaborator Author

test (macos) is red at 4743ec0, and the cause is this PR's naming change, not a flake. git::repository::tests::start_fsmonitor_daemon_skips_fork_when_daemon_answers binds a Unix socket at git's fixed location inside a TestRepo, and that path is now too long for macOS's 103-byte sun_path (log):

bind /private/var/folders/n6/hn37q6pj7sj_9dmqqc2113800000gn/T/wt/.tmp29046-SOiaZx/repo/.git/fsmonitor--daemon.ipc (108 bytes; sun_path holds 103 on macOS)

The old .tmpXXXXXX name put this path at 102 bytes. The <pid>- prefix adds 6 bytes for a 5-digit macOS pid. The doc comment on test_temp_root already describes this budget, and its "14 bytes that were spare" figure was computed against the shorter test.sock path, so the fsmonitor socket had only 1 byte left. The earlier head's macOS legs were cancelled, so this is the first macOS result for the change.

Any name that includes the pid has to fit in roughly the old 10 characters. Every way I found to do that gives up something:

  • Length-neutral names under test_temp_root. For example, .tmp followed by a fixed-width pid encoding and a shorter random tail. Cross-process uniqueness holds, but fewer random characters leaves within-process collisions to tempfile's AlreadyExists retry.
  • Pid prefix only on helpers outside test_temp_root. tempdir()/named_tempfile() can keep it, since names in the OS temp dir have spare bytes. test_tempdir() would keep plain names, which drops the fix for the shared wt/ root that every TestRepo uses.
  • Shorten the fsmonitor test's own path, for example by using a repo at the temp dir root instead of <tmp>/repo. That saves 5 bytes but leaves zero margin for a 5-digit pid.

Each one trades the collision fix against the documented socket budget, which is a call for a maintainer, so I haven't pushed any of them. The snapshot-guard fix from my review is in 4743ec0. Its own test passes locally.

…g the pid

The `.tmp<pid>-XXXXXX` names pushed macOS's fsmonitor socket test past
sun_path (108 bytes, 103 allowed). Seeding fastrand's thread-local
generator from RandomState, which draws OS randomness per process,
separates names across processes while keeping tempfile's `.tmpXXXXXX`
length. This reverts the snapshot guard change that matched the
pid-prefixed form.

Refs #4692
@worktrunk-bot worktrunk-bot changed the title fix(tests): put the process id in the suite's temp names fix(tests): seed tempfile's name generator per process Nov 4, 2026
@max-sixty

Copy link
Copy Markdown
Owner

@worktrunk-bot Current-head fast-checks fails in pre-merge project:doc: rustdoc reports public documentation for tempdir links to private item temp_builder at src/testing/mod.rs:869:41, then exits 101. Please fix that public doc link (for example, use plain code text or link to a public item) and rerun the focused doc check. The live macOS/Linux/Windows tests are still pending.

This was written by Codex on behalf of max-sixty

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Two of this head's checks fail because of the reseeding commit, and both are one-line fixes. I'm pushing them.

  • fast-checks fails in pre-merge project:doc: tempdir's public doc comment links to the private temp_builder, and -D warnings turns rustdoc::private_intra_doc_links into an error.
  • minimal-versions fails at cargo update -Z direct-minimal-versions: fastrand = "2" resolves to 2.0.0, below the ^2.1.1 that tempfile 3.27 requires, so resolution fails. fastrand::seed exists in every 2.x, so the floor only needs to match the version that's locked.

Comment thread Cargo.toml Resolved
Comment thread src/testing/mod.rs Resolved
minimal-versions resolved `fastrand = "2"` to 2.0.0, below tempfile's
own 2.1.1 floor.

Refs #4692
@worktrunk-bot

Copy link
Copy Markdown
Collaborator Author

I fixed the doc link in 2f94714 by replacing the link with plain text. Locally, RUSTDOCFLAGS='-Dwarnings' cargo doc --no-deps --document-private-items is now clean. The review session then pushed e8b45e4 on top of it, which fixes the minimal-versions failure. fast-checks passes at that head. The test legs are still running.

@max-sixty
max-sixty merged commit bac7f7e into main Nov 4, 2026
40 of 41 checks passed
@max-sixty
max-sixty deleted the fix/issue-4692 branch November 4, 2026 10:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

automated-fix Automated CI fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Windows CI: temp file and directory creation under D:\tmp intermittently fails with Access is denied

2 participants