Repository navigation
fix(fd): serialize move_to_high_fd's probe-and-dup2 under a process lock - #4283
Merged
Merged
Conversation
move_to_high_fd scanned downward from a shared ceiling for an empty slot and then dup2'd into it: probe and dup2 were not atomic. Two threads necessarily find the same first empty slot T; the second dup2 atomically closes the first thread's descriptor, the first Drop then closes the second thread's live descriptor, and the second Drop's close hits EBADF -- the std debug-build IO Safety abort of #4274. The hot trigger is file-redirect execution lazily installing the cwd anchor (probe_output_redirect_file -> output_fifo_validation_via_anchor), several moves per test pair in lock-step threads. A process-level static Mutex now covers the scan+dup2+close section (the HANDSHAKE_LOCK pattern); landing band, cloexec and failure fallback are unchanged, and every caller (capture pipes, signal relay, anchor refresh) is covered by the single mechanism. Adds a barrier-synchronized regression pin that aborts 10/10 on base and passes 20/20 with the fix. Refs #4274 (not Fixes).
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.
Summary
Fixes the parallel in-process abort tracked in #4274:
fatal runtime error: IO Safety violation: owned file descriptor already closed(rc 134), hit at high frequency by theparameter_transform_testsbinary on macOS (full binary 4/10 rounds; the two prompt tests as a minimal pair 14/20 runs).Refs #4274. UNVERIFIED-BY-CAPTAIN.
Root cause
fd::move_to_high_fd(src/fd/unix.rs) relocates a descriptor with a probe-then-dup2 scan:fcntl(F_GETFD)to find the first free slot scanning down from the ceiling, thendup2onto it. The probe and the dup2 are not atomic, and every mover scans from the same ceiling, so two threads moving a descriptor at the same time probe the same first free slot T. The seconddup2atomically closes the first mover's descriptor at T and replaces it, leaving two owners for one fd number; the first Drop closes the other's descriptor and the second Drop's close hits EBADF, which std's IO Safety check turns into a process abort.The firing path in the observed failure (backtrace-verified): an execute whose output redirects to a file validates the target through the cwd anchor (
shell_options::probe_output_redirect_file->output_fifo_validation_via_anchor), which lazily installs the Executor's anchor viamove_file_to_high_fd— one move per redirected execute, three per prompt test. Two tests the harness starts together run near-identical code in lockstep, so their moves land in the same microsecond window again and again. The same race covers the function's other callers (capture pipes, the signal relay pipe, cd/spawn anchor refreshes) because they share the one move path.GNU is single-threaded, so general.c:702's check-then-dup2 needs no lock there; a process hosting many Executors does.
Fix
Serialize the whole scan+dup2+close of
move_to_high_fdwith a process-levelstatic Mutex<()>(the crate's existing idiom, cf. signals/delivery.rs HANDSHAKE_LOCK). The landing band, the cloexec re-arm, and the failure fallbacks are unchanged; the critical section is a handful of syscalls at command-setup frequency, so contention is negligible.What this PR deletes
Nothing. There is still exactly one relocation path (
move_to_high_fd); it is now serialized. No shim, no second mechanism.Net lines: src/fd/unix.rs +12 (2 code, 10 comment); tests +94 (aggregator +2, new pin file +92).
Verification (lane-run, captain to re-verify)
New pin
issue4274_concurrent_anchor_moves(tests/issues_4200_4299/): barrier-synchronized threads loop the prompt-test body (three redirected executes on fresh Executors per round, real files in per-thread scratch dirs). Fail-first on master: 10/10 runs abort. Post-fix: 0/20.Original repro, back-to-back on macOS:
parameter_transform_testsbinary, default parallelism: master 4/10 abort -> fixed 0/10, no other failures.Census differential (macOS): a full-suite back-to-back was attempted but the shared devin-mac disk filled mid-run (five lanes building concurrently; the full target set alone grows target/ by ~3.2 GB), which invalidates that attempt — its failure window is ENOSPC artifacts in file-writing tests, not code results. Scoped differential over the change's risk surface instead (lib unittests + 19 fd/anchor/redirect/procsub/coproc/pipeline/signal/spawn targets, ~3090 tests per side): FAILED name sets identical on master and fixed (76 names each, symmetric difference empty — the standing macOS baseline for this subset, present on pristine master under identical conditions); the fixed side's pass count is +1 (the new pin); no test binary died by signal on either side. Full-suite adjudication is left to this PR's CI.
Changed files are rustfmt-clean (segmented check on the three touched files only).
Kinship
Different face from #4252 (macOS procsub output race) and from the issue1838 fd-ownership face fixed by #4231: this one is neither procsub nor the redirect table, it is the shared high-fd relocation primitive itself. Recommend keeping #4274's characterization separate.