Skip to content

fix: Mitigate the -L dependency= consolidation races on Windows - #48

Merged
dzbarsky merged 1 commit into
hermeticbuild:mainfrom
ArchangelX360:ArchangelX360/MRI-5297_race_temp_files
Sep 1, 2026
Merged

dzbarsky merged 1 commit into
hermeticbuild:mainfrom
ArchangelX360:ArchangelX360/MRI-5297_race_temp_files

Conversation

@ArchangelX360

Copy link
Copy Markdown
Member
 On Windows, consolidate_dependency_search_paths (util/process_wrapper/main.rs:156) collapses
 every -Ldependency=<dir> rustc flag into one unified directory, to avoid Windows command-line size
 overruns (commit 3f613e4cf). It does so by read_dir-ing each search path and hardlinking (falling
 back to copying) every regular file it finds into that unified dir.

 However, builds failed with:
 Error: ProcessWrapperError("unable to copy
   .../ijent_util-subscriber_contract-test.exe.tmp8ceb5a4 into unified
   dependency dir ...: Access is denied. (os error 5)")

 .exe.tmpXXXXXXX file is an LLVM's atomic-output temp file. Every LLVM tool that writes output this
 way does it: lld-link / rust-lld (the PE image and the PDB), llvm-lib (import libs),
 llvm-objcopy.
 So this file is rust-lld writing the sibling target ijent_util-subscriber_contract-test.exe.
 It only occurs with toolchain.linker_preference == "rust" (rustc.bzl:445-448);
 MSVC's link.exe writes in place.

 Lifecycle of that temp file

 ┌────────────────┬───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
 │     Phase      │                                                       What happens                                                        │
 ├────────────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
 │ Created        │ At the start of the sibling's link step. TempFile::create passes OF_Delete, then immediately calls                        │
 │                │ setDeleteDisposition(H, true) → DELETE_PENDING from birth.                                                                │
 ├────────────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
 │ Alive          │ For the entire duration of that link, while lld mmaps and writes the image into it.                                       │
 ├────────────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
 │ Deleted        │ FileOutputBuffer::commit() → TempFile::keep(FinalPath): setDeleteDisposition(H, false), then rename_handle(H,             │
 │ (success)      │ "…-test.exe"). The temp name vanishes by rename.                                                                          │
 ├────────────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
 │ Deleted        │ TempFile::discard() — the already-set delete disposition does it on close; RemoveFileOnSignal covers crashes.             │
 │ (failure)      │                                                                                                                           │
 └────────────────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

 Why os error 5 and not os error 2

 Because of DELETE_PENDING. On Windows a delete-pending file:

 - still appears in directory enumeration — fs::read_dir yields it and entry.file_type()
   succeeds (metadata comes from the dirent);
 - but every CreateFile on it fails with STATUS_DELETE_PENDING → ERROR_ACCESS_DENIED (5).

 So fs::hard_link (CreateHardLinkW) fails with 5, the fs::copy (CopyFileExW) fallback fails
 with 5, and the wrapper escalates to a fatal ProcessWrapperError. LLVM's own createUniqueEntity
 retries on errc::permission_denied for exactly this reason.

 This is not a narrow race. The window is the whole link of the sibling target, and the failure is
 deterministic once read_dir observes the temp file.

 Why the temp file is in a -L dependency= directory at all

 rust/private/rustc.bzl:2994-2998 derives the search paths from the dirnames of
 dep_info.transitive_crate_outputs (_get_dirname, rustc.bzl:3433). A dirname is an entire Bazel
 package bin dir (bazel-out/<cfg>/bin/fleet/native/ijent/ijent_util/) containing every output of
 that package — sibling .exes, .pdbs, .os, .rustc-outputs, tree artifacts, in-flight linker
 temps — not a curated set of crate artifacts.

 Bazel has no sandboxing on Windows, so those scanned directories are the live bazel-out tree
 with every concurrent action's activity visible.

```
 On Windows, consolidate_dependency_search_paths (util/process_wrapper/main.rs:156) collapses
 every -Ldependency=<dir> rustc flag into one unified directory, to avoid Windows command-line size
 overruns (commit 3f613e4). It does so by read_dir-ing each search path and hardlinking (falling
 back to copying) every regular file it finds into that unified dir.

 However, builds failed with:
 Error: ProcessWrapperError("unable to copy
   .../ijent_util-subscriber_contract-test.exe.tmp8ceb5a4 into unified
   dependency dir ...: Access is denied. (os error 5)")

 .exe.tmpXXXXXXX file is an LLVM's atomic-output temp file. Every LLVM tool that writes output this
 way does it: lld-link / rust-lld (the PE image and the PDB), llvm-lib (import libs),
 llvm-objcopy.
 So this file is rust-lld writing the sibling target ijent_util-subscriber_contract-test.exe.
 It only occurs with toolchain.linker_preference == "rust" (rustc.bzl:445-448);
 MSVC's link.exe writes in place.

 Lifecycle of that temp file

 ┌────────────────┬───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
 │     Phase      │                                                       What happens                                                        │
 ├────────────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
 │ Created        │ At the start of the sibling's link step. TempFile::create passes OF_Delete, then immediately calls                        │
 │                │ setDeleteDisposition(H, true) → DELETE_PENDING from birth.                                                                │
 ├────────────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
 │ Alive          │ For the entire duration of that link, while lld mmaps and writes the image into it.                                       │
 ├────────────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
 │ Deleted        │ FileOutputBuffer::commit() → TempFile::keep(FinalPath): setDeleteDisposition(H, false), then rename_handle(H,             │
 │ (success)      │ "…-test.exe"). The temp name vanishes by rename.                                                                          │
 ├────────────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
 │ Deleted        │ TempFile::discard() — the already-set delete disposition does it on close; RemoveFileOnSignal covers crashes.             │
 │ (failure)      │                                                                                                                           │
 └────────────────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

 Why os error 5 and not os error 2

 Because of DELETE_PENDING. On Windows a delete-pending file:

 - still appears in directory enumeration — fs::read_dir yields it and entry.file_type()
   succeeds (metadata comes from the dirent);
 - but every CreateFile on it fails with STATUS_DELETE_PENDING → ERROR_ACCESS_DENIED (5).

 So fs::hard_link (CreateHardLinkW) fails with 5, the fs::copy (CopyFileExW) fallback fails
 with 5, and the wrapper escalates to a fatal ProcessWrapperError. LLVM's own createUniqueEntity
 retries on errc::permission_denied for exactly this reason.

 This is not a narrow race. The window is the whole link of the sibling target, and the failure is
 deterministic once read_dir observes the temp file.

 Why the temp file is in a -L dependency= directory at all

 rust/private/rustc.bzl:2994-2998 derives the search paths from the dirnames of
 dep_info.transitive_crate_outputs (_get_dirname, rustc.bzl:3433). A dirname is an entire Bazel
 package bin dir (bazel-out/<cfg>/bin/fleet/native/ijent/ijent_util/) containing every output of
 that package — sibling .exes, .pdbs, .os, .rustc-outputs, tree artifacts, in-flight linker
 temps — not a curated set of crate artifacts.

 Bazel has no sandboxing on Windows, so those scanned directories are the live bazel-out tree
 with every concurrent action's activity visible.
```
@ArchangelX360

Copy link
Copy Markdown
Member Author

The proper fix would probably be to stop scanning that -L dirs and pass a list of artifacts to copy over.
But it's not as trivial as it sounds, so that workaround patch seemed like a good enough mitigation for us.

@dzbarsky
dzbarsky merged commit b224135 into hermeticbuild:main Sep 1, 2026
3 checks passed
@ArchangelX360
ArchangelX360 deleted the ArchangelX360/MRI-5297_race_temp_files branch September 1, 2026 16:34
genevieve-me pushed a commit to genevieve-me/rules_rust that referenced this pull request Sep 8, 2026
…eticbuild#48)

```
 On Windows, consolidate_dependency_search_paths (util/process_wrapper/main.rs:156) collapses
 every -Ldependency=<dir> rustc flag into one unified directory, to avoid Windows command-line size
 overruns (commit 3f613e4). It does so by read_dir-ing each search path and hardlinking (falling
 back to copying) every regular file it finds into that unified dir.

 However, builds failed with:
 Error: ProcessWrapperError("unable to copy
   .../ijent_util-subscriber_contract-test.exe.tmp8ceb5a4 into unified
   dependency dir ...: Access is denied. (os error 5)")

 .exe.tmpXXXXXXX file is an LLVM's atomic-output temp file. Every LLVM tool that writes output this
 way does it: lld-link / rust-lld (the PE image and the PDB), llvm-lib (import libs),
 llvm-objcopy.
 So this file is rust-lld writing the sibling target ijent_util-subscriber_contract-test.exe.
 It only occurs with toolchain.linker_preference == "rust" (rustc.bzl:445-448);
 MSVC's link.exe writes in place.

 Lifecycle of that temp file

 ┌────────────────┬───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
 │     Phase      │                                                       What happens                                                        │
 ├────────────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
 │ Created        │ At the start of the sibling's link step. TempFile::create passes OF_Delete, then immediately calls                        │
 │                │ setDeleteDisposition(H, true) → DELETE_PENDING from birth.                                                                │
 ├────────────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
 │ Alive          │ For the entire duration of that link, while lld mmaps and writes the image into it.                                       │
 ├────────────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
 │ Deleted        │ FileOutputBuffer::commit() → TempFile::keep(FinalPath): setDeleteDisposition(H, false), then rename_handle(H,             │
 │ (success)      │ "…-test.exe"). The temp name vanishes by rename.                                                                          │
 ├────────────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
 │ Deleted        │ TempFile::discard() — the already-set delete disposition does it on close; RemoveFileOnSignal covers crashes.             │
 │ (failure)      │                                                                                                                           │
 └────────────────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

 Why os error 5 and not os error 2

 Because of DELETE_PENDING. On Windows a delete-pending file:

 - still appears in directory enumeration — fs::read_dir yields it and entry.file_type()
   succeeds (metadata comes from the dirent);
 - but every CreateFile on it fails with STATUS_DELETE_PENDING → ERROR_ACCESS_DENIED (5).

 So fs::hard_link (CreateHardLinkW) fails with 5, the fs::copy (CopyFileExW) fallback fails
 with 5, and the wrapper escalates to a fatal ProcessWrapperError. LLVM's own createUniqueEntity
 retries on errc::permission_denied for exactly this reason.

 This is not a narrow race. The window is the whole link of the sibling target, and the failure is
 deterministic once read_dir observes the temp file.

 Why the temp file is in a -L dependency= directory at all

 rust/private/rustc.bzl:2994-2998 derives the search paths from the dirnames of
 dep_info.transitive_crate_outputs (_get_dirname, rustc.bzl:3433). A dirname is an entire Bazel
 package bin dir (bazel-out/<cfg>/bin/fleet/native/ijent/ijent_util/) containing every output of
 that package — sibling .exes, .pdbs, .os, .rustc-outputs, tree artifacts, in-flight linker
 temps — not a curated set of crate artifacts.

 Bazel has no sandboxing on Windows, so those scanned directories are the live bazel-out tree
 with every concurrent action's activity visible.
```
dzbarsky pushed a commit that referenced this pull request Sep 23, 2026
```
 On Windows, consolidate_dependency_search_paths (util/process_wrapper/main.rs:156) collapses
 every -Ldependency=<dir> rustc flag into one unified directory, to avoid Windows command-line size
 overruns (commit 3f613e4). It does so by read_dir-ing each search path and hardlinking (falling
 back to copying) every regular file it finds into that unified dir.

 However, builds failed with:
 Error: ProcessWrapperError("unable to copy
   .../ijent_util-subscriber_contract-test.exe.tmp8ceb5a4 into unified
   dependency dir ...: Access is denied. (os error 5)")

 .exe.tmpXXXXXXX file is an LLVM's atomic-output temp file. Every LLVM tool that writes output this
 way does it: lld-link / rust-lld (the PE image and the PDB), llvm-lib (import libs),
 llvm-objcopy.
 So this file is rust-lld writing the sibling target ijent_util-subscriber_contract-test.exe.
 It only occurs with toolchain.linker_preference == "rust" (rustc.bzl:445-448);
 MSVC's link.exe writes in place.

 Lifecycle of that temp file

 ┌────────────────┬───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
 │     Phase      │                                                       What happens                                                        │
 ├────────────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
 │ Created        │ At the start of the sibling's link step. TempFile::create passes OF_Delete, then immediately calls                        │
 │                │ setDeleteDisposition(H, true) → DELETE_PENDING from birth.                                                                │
 ├────────────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
 │ Alive          │ For the entire duration of that link, while lld mmaps and writes the image into it.                                       │
 ├────────────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
 │ Deleted        │ FileOutputBuffer::commit() → TempFile::keep(FinalPath): setDeleteDisposition(H, false), then rename_handle(H,             │
 │ (success)      │ "…-test.exe"). The temp name vanishes by rename.                                                                          │
 ├────────────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
 │ Deleted        │ TempFile::discard() — the already-set delete disposition does it on close; RemoveFileOnSignal covers crashes.             │
 │ (failure)      │                                                                                                                           │
 └────────────────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

 Why os error 5 and not os error 2

 Because of DELETE_PENDING. On Windows a delete-pending file:

 - still appears in directory enumeration — fs::read_dir yields it and entry.file_type()
   succeeds (metadata comes from the dirent);
 - but every CreateFile on it fails with STATUS_DELETE_PENDING → ERROR_ACCESS_DENIED (5).

 So fs::hard_link (CreateHardLinkW) fails with 5, the fs::copy (CopyFileExW) fallback fails
 with 5, and the wrapper escalates to a fatal ProcessWrapperError. LLVM's own createUniqueEntity
 retries on errc::permission_denied for exactly this reason.

 This is not a narrow race. The window is the whole link of the sibling target, and the failure is
 deterministic once read_dir observes the temp file.

 Why the temp file is in a -L dependency= directory at all

 rust/private/rustc.bzl:2994-2998 derives the search paths from the dirnames of
 dep_info.transitive_crate_outputs (_get_dirname, rustc.bzl:3433). A dirname is an entire Bazel
 package bin dir (bazel-out/<cfg>/bin/fleet/native/ijent/ijent_util/) containing every output of
 that package — sibling .exes, .pdbs, .os, .rustc-outputs, tree artifacts, in-flight linker
 temps — not a curated set of crate artifacts.

 Bazel has no sandboxing on Windows, so those scanned directories are the live bazel-out tree
 with every concurrent action's activity visible.
```
ArchangelX360 added a commit to ArchangelX360/rules_rust that referenced this pull request Oct 5, 2026
… `cc_common.link`

Keep `.a` files out of the unified dependency directory of the Windows process wrapper.

On Windows, `process_wrapper` merges every `-Ldependency=` directory into one, to keep the command line short. It scans each directory with `read_dir`, so it also sees files that are no input of the action. `rules_rust` puts a `lib<name>.a` symlink next to each rlib, for `cc_common.link`. With `experimental_use_cc_common_link`, Bazel builds those symlinks while sibling `Rustc` actions scan the directory, and the copy fails with "Access is denied".

`-L dependency=` only searches for crates, and no crate is a `.a` file, so skip the extension.

Relates to hermeticbuild#48
dzbarsky pushed a commit that referenced this pull request Oct 5, 2026
… `cc_common.link` (#82)

Keep `.a` files out of the unified dependency directory of the Windows process wrapper.

On Windows, `process_wrapper` merges every `-Ldependency=` directory into one, to keep the command line short. It scans each directory with `read_dir`, so it also sees files that are no input of the action. `rules_rust` puts a `lib<name>.a` symlink next to each rlib, for `cc_common.link`. With `experimental_use_cc_common_link`, Bazel builds those symlinks while sibling `Rustc` actions scan the directory, and the copy fails with "Access is denied".

`-L dependency=` only searches for crates, and no crate is a `.a` file, so skip the extension.

Relates to #48
walter-zeromatter pushed a commit to ZeromatterOSS/rules_rust that referenced this pull request Nov 6, 2026
… `cc_common.link` (bazelbuild#82)

Keep `.a` files out of the unified dependency directory of the Windows process wrapper.

On Windows, `process_wrapper` merges every `-Ldependency=` directory into one, to keep the command line short. It scans each directory with `read_dir`, so it also sees files that are no input of the action. `rules_rust` puts a `lib<name>.a` symlink next to each rlib, for `cc_common.link`. With `experimental_use_cc_common_link`, Bazel builds those symlinks while sibling `Rustc` actions scan the directory, and the copy fails with "Access is denied".

`-L dependency=` only searches for crates, and no crate is a `.a` file, so skip the extension.

Relates to hermeticbuild#48
dzbarsky pushed a commit that referenced this pull request Nov 30, 2026
```
 On Windows, consolidate_dependency_search_paths (util/process_wrapper/main.rs:156) collapses
 every -Ldependency=<dir> rustc flag into one unified directory, to avoid Windows command-line size
 overruns (commit 3f613e4). It does so by read_dir-ing each search path and hardlinking (falling
 back to copying) every regular file it finds into that unified dir.

 However, builds failed with:
 Error: ProcessWrapperError("unable to copy
   .../ijent_util-subscriber_contract-test.exe.tmp8ceb5a4 into unified
   dependency dir ...: Access is denied. (os error 5)")

 .exe.tmpXXXXXXX file is an LLVM's atomic-output temp file. Every LLVM tool that writes output this
 way does it: lld-link / rust-lld (the PE image and the PDB), llvm-lib (import libs),
 llvm-objcopy.
 So this file is rust-lld writing the sibling target ijent_util-subscriber_contract-test.exe.
 It only occurs with toolchain.linker_preference == "rust" (rustc.bzl:445-448);
 MSVC's link.exe writes in place.

 Lifecycle of that temp file

 ┌────────────────┬───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐
 │     Phase      │                                                       What happens                                                        │
 ├────────────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
 │ Created        │ At the start of the sibling's link step. TempFile::create passes OF_Delete, then immediately calls                        │
 │                │ setDeleteDisposition(H, true) → DELETE_PENDING from birth.                                                                │
 ├────────────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
 │ Alive          │ For the entire duration of that link, while lld mmaps and writes the image into it.                                       │
 ├────────────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
 │ Deleted        │ FileOutputBuffer::commit() → TempFile::keep(FinalPath): setDeleteDisposition(H, false), then rename_handle(H,             │
 │ (success)      │ "…-test.exe"). The temp name vanishes by rename.                                                                          │
 ├────────────────┼───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤
 │ Deleted        │ TempFile::discard() — the already-set delete disposition does it on close; RemoveFileOnSignal covers crashes.             │
 │ (failure)      │                                                                                                                           │
 └────────────────┴───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘

 Why os error 5 and not os error 2

 Because of DELETE_PENDING. On Windows a delete-pending file:

 - still appears in directory enumeration — fs::read_dir yields it and entry.file_type()
   succeeds (metadata comes from the dirent);
 - but every CreateFile on it fails with STATUS_DELETE_PENDING → ERROR_ACCESS_DENIED (5).

 So fs::hard_link (CreateHardLinkW) fails with 5, the fs::copy (CopyFileExW) fallback fails
 with 5, and the wrapper escalates to a fatal ProcessWrapperError. LLVM's own createUniqueEntity
 retries on errc::permission_denied for exactly this reason.

 This is not a narrow race. The window is the whole link of the sibling target, and the failure is
 deterministic once read_dir observes the temp file.

 Why the temp file is in a -L dependency= directory at all

 rust/private/rustc.bzl:2994-2998 derives the search paths from the dirnames of
 dep_info.transitive_crate_outputs (_get_dirname, rustc.bzl:3433). A dirname is an entire Bazel
 package bin dir (bazel-out/<cfg>/bin/fleet/native/ijent/ijent_util/) containing every output of
 that package — sibling .exes, .pdbs, .os, .rustc-outputs, tree artifacts, in-flight linker
 temps — not a curated set of crate artifacts.

 Bazel has no sandboxing on Windows, so those scanned directories are the live bazel-out tree
 with every concurrent action's activity visible.
```
dzbarsky pushed a commit that referenced this pull request Nov 30, 2026
… `cc_common.link` (#82)

Keep `.a` files out of the unified dependency directory of the Windows process wrapper.

On Windows, `process_wrapper` merges every `-Ldependency=` directory into one, to keep the command line short. It scans each directory with `read_dir`, so it also sees files that are no input of the action. `rules_rust` puts a `lib<name>.a` symlink next to each rlib, for `cc_common.link`. With `experimental_use_cc_common_link`, Bazel builds those symlinks while sibling `Rustc` actions scan the directory, and the copy fails with "Access is denied".

`-L dependency=` only searches for crates, and no crate is a `.a` file, so skip the extension.

Relates to #48
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.

2 participants