Skip to content

test(services): the process shim races itself — intermittent "Text file busy" in run_answers_the_child_exit_code_and_capture_answers_its_output #1351

Done

Description

@chappie-daemon

cargo test -p nostream-services --lib fails intermittently with

the child runs: Spawn { program: "/tmp/.tmpQpdddb/env-check", message: "Text file busy (os error 26)" }
  at crates/nostream-services/src/process.rs:362

It hit the Rust Checks job on #1350, whose diff touches no nostream-services file.

Measured

With --test-threads=8, on two trees that have identical nostream-services sources:

Tree Failures
e5a78d70 (rewrite/main) 4 of 15 runs
a branch off it touching only nostream-config, nostream-admin, nostream-mcp 3 of 15 runs

Every failure is the same case, run_answers_the_child_exit_code_and_capture_answers_its_output, and every one is ETXTBSY on that test's own env-check shim. At the default test-thread count it passed 15 of 15 in the same loop, so it is a race, not a broken fixture.

Why

shim() (process.rs:279) writes the script with std::fs::write and then makes it executable. The fd it writes through is O_CLOEXEC, but the tests run in parallel threads: when another thread's Command::spawn forks inside that write window, the child holds the new script open for writing until it execs, and the exec of env-check answers ETXTBSY. Scripts written by fs::write and executed by a nearby spawn cannot be made safe by closing earlier — the window belongs to the forking thread, not the writing one.

Suggested fix

Write the shim under a temporary name and std::fs::rename it into place: the rename swaps the inode, so the exec'd path is never the one a writer holds open. A retry loop on ETXTBSY would also work but leaves the race in place.

Simplest reproduction:

for i in $(seq 1 15); do cargo test -q -p nostream-services --lib -- --test-threads=8; done

Activity

  1. added
    test
    on Dec 2, 2026
  2. ItsAbir005 commented on Dec 3, 2026

    @ItsAbir005
    Contributor

    Hi @cameri @chappie-daemon, I'd like to take this one. My plan is to reproduce the flake first (the 15-run loop at --test-threads=8), then write the shim to a temp name and rename it into place as suggested, and re-measure over a larger number of runs. Since ETXTBSY follows the inode rather than the path, I'll also check whether a forked child can still hold the write descriptor after the rename, and if so I'd add a lock around write-and-spawn or retry on ETXTBSY. Could you assign it to me?

  3. ItsAbir005 commented on Dec 3, 2026

    @ItsAbir005
    Contributor

    I have a fix ready on my fork (ItsAbir005/nostream, branch fix/1351-process-shim-race, fork PR ItsAbir005#3) if I can be assigned to open the PR against rewrite/main.

  4. cameri commented on Dec 3, 2026

    @cameri
    Owner

    Assigned to @ItsAbir005

  5. ItsAbir005 commented on Dec 3, 2026

    @ItsAbir005
    Contributor

    @cameri @chappie-daemon I have also raised the PR at ItsAbir005#3 pls review

  6. chappie-daemon commented on Dec 3, 2026

    @chappie-daemon
    CollaboratorAuthor

    Thank you @ItsAbir005 — #1358 carries your commit (1f641f23, cherry-picked with -x as 497930d5, authorship preserved) into rewrite/main. The fork-PR route into this repo needs a permission this account does not hold, so the branch lives here instead; your own PR ItsAbir005#3 links to it.

    Verified locally before opening: cargo test -p nostream-services --lib process::tests (5 passed) and the reproduction loop from this issue, 15 consecutive runs at --test-threads=8, 0 failures.

  7. added a commit that references this issue on Dec 3, 2026
    46abd69
  8. chappie-daemon commented on Dec 3, 2026

    @chappie-daemon
    CollaboratorAuthor

    Fixed by #1358, merged as e7ce03e7 on rewrite/main. The shim is now written to a unique temporary file in the same directory, closed, chmodded and renamed into place, so the test's own spawn cannot inherit a write descriptor on the file it is about to execute.

    Authored by @ItsAbir005; thanks also to @avantikonde, whose #1357 named the mechanism and is kept as the fallback if the narrow fork window ever recurs.

    Verification on the merged commit: cargo test -p nostream-services --lib process::tests (5 passed) and 15 consecutive runs at --test-threads=8 with 0 failures; CI green on the required contexts.

  9. added a commit that references this issue on Dec 3, 2026
    497930d
  10. added a commit that references this issue on Dec 3, 2026
    e4b18eb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

test

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions