Skip to content

Perform inner repo clean-up asynchronously - #8168

Merged
ViktorHofer merged 2 commits into
mainfrom
cleanup-repo-async
Aug 7, 2026
Merged

ViktorHofer merged 2 commits into
mainfrom
cleanup-repo-async

Conversation

@ViktorHofer

@ViktorHofer ViktorHofer commented Aug 6, 2026 •

Copy link
Copy Markdown
Member

Fixes #8167.

CleanupRepo deleted the repo's artifacts dir synchronously as part of the repo wrapper project's build, so every dependent repo waited for that deletion before it could start building. The artifacts dir is isolated to the repo that produced it and nothing else reads from it, so there's no reason for the P2P protocol to wait on it.

It's now spawned as a detached background process.

Why detaching is subtle

Exec doesn't just wait on the process it launches, it waits until the stdout/stderr pipes it handed out are closed. Any child that ends up holding a copy of those pipes keeps Exec blocked for the full deletion.

On Windows start /B doesn't achieve this: cmd gives the spawned process an inheritable copy of those pipes, so Exec waits for the whole delete even though start itself returns in ~30 ms. Redirecting the streams doesn't change that. Start-Process goes through ShellExecute, which doesn't inherit handles, so Exec is released immediately.

On Unix nohup ... & does detach, but only if the streams are redirected — </dev/null and >/dev/null are load-bearing rather than cosmetic.

Measured by timing a complete MSBuild invocation against an 8s command:

Windows Unix
synchronous 9488 ms 8257 ms
start /B, redirected to a file 9587 ms –
start /B, redirected to NUL 9565 ms –
backgrounded, no redirection – 8203 ms
backgrounded, redirected to /dev/null – 126 ms
Start-Process -WindowStyle Hidden 2006 ms –

The Windows figure is dominated by powershell start-up. Each case was also checked against a real directory to confirm the deletion actually ran to completion rather than being silently dropped.

No clean-up log

Nothing consumes the output and the deletion is best effort, so both platforms redirect to the null device and no CleanupRepo.log is produced or published. On Unix such a log would always be empty regardless: POSIX unlinks directory entries even when handles are open, so rm -rf doesn't fail on locked files.

Also removed

Removed Why
The build server shutdown Only needed back when a failed clean operation failed the build. Also makes the source-build hang workaround for dotnet/source-build#4796 unnecessary, and drops the two BuildServerShutdownTimeout* properties, which had no other consumers.
Inputs/Outputs/MakeDir/Touch The target's existing Exists('$(RepoArtifactsDir)') condition already expresses whether cleanup is needed, and more truthfully than a stamp written before the detached deletion completes. Nothing else read CleanupRepo.complete.
DirSize After CleanupRepo With the deletion running in the background this races the other repos and no longer measures anything. DirSize Before Building and DirSize After Building are unchanged and still go to the console.

Behaviour changes worth knowing

  • Deletion failures are no longer observable. Since the build server is no longer shut down first, a held file handle on Windows makes rmdir skip that file and its whole parent chain, so some space silently isn't reclaimed. The build still succeeds and nothing reports it.
  • Cancelling the build doesn't cancel the deletion. The detached process has no console attachment, and POSIX has & ignore SIGINT in non-interactive shells, so the deletion runs to completion either way. This is benign — the space still gets freed and the artifacts are garbage after a cancel — but restarting a build immediately after cancelling can race an in-flight deletion. CleanWhileBuilding is opt-in (--clean-while-building) and predominantly used in CI, where cancellation kills the agent anyway.
  • There is no backpressure. Several repos can now be deleting concurrently, so peak disk usage is slightly higher than before. The deletion still starts at the same point, it just no longer blocks dependents, so space is reclaimed while subsequent repos build rather than before they start.

Validation

  • dotnet msbuild repo-projects\roslyn.proj -getProperty:RepoArtifactsDir evaluates cleanly.
  • No remaining references to BuildServerShutdown* or CleanupRepo.complete outside src/.
  • Verified on both platforms that Exec returns immediately, that the deletion completes afterwards, and that the detached process outlives its parent shell.
  • Verified the spawn creates no new console window (conhost and windowed-process counts unchanged) and no Terminate batch job (Y/N)? prompt — the detached command is cmd /c rmdir, an internal command rather than a batch file.
  • Verified the Windows command against a path containing spaces.

Note

This pull request description was generated with GitHub Copilot.

The CleanupRepo target deleted the repo's artifacts dir synchronously as part
of the repo wrapper project's build, so every dependent repo waited for that
deletion to finish before it could start building. The artifacts dir is
isolated to the repo that produced it and nothing else reads from it, so
there is no reason for the P2P protocol to wait on it.

Spawn the deletion as a detached background process instead. Redirecting all
three standard streams is required rather than cosmetic: Exec waits on the
pipes it hands out, so a detached process that inherits them keeps Exec
blocked for the full deletion. Measured against an 8s command, the Exec call
returns in 654ms instead of 8793ms on Windows and 125ms instead of 8242ms on
Unix; without the redirection it still takes the full 8.7s.

Also:

- Remove the build server shutdown. It was only needed back when a failed
  clean operation failed the build, which is no longer the case. This also
  makes the source-build hang workaround for
  dotnet/source-build#4796 unnecessary.
- Remove the Inputs/Outputs/Touch incrementality. The target's existing
  Exists(RepoArtifactsDir) condition already expresses whether cleanup is
  needed, and it does so more truthfully than a stamp file that would be
  written before the detached deletion completes.
- Remove the 'DirSize After CleanupRepo' measurement. With the deletion
  running in the background it races the other repos and no longer measures
  anything meaningful.

Deletion failures are no longer observable through the build, so stderr is
captured in artifacts/log/<repo>/CleanupRepo.log. The log is empty on success.

Fixes #8167

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 8df1e800-36c4-4797-b8d7-d2c8e23c1342
Copilot AI review requested due to automatic review settings August 6, 2026 13:05
@ViktorHofer
ViktorHofer requested review from a team as code owners August 6, 2026 13:05
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).
1 pipeline(s) were filtered out due to trigger conditions.
There may be pipelines that require an authorized user to comment /azp run to run.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

This PR updates the VMR repo-wrapper build orchestration so that per-inner-repo artifact cleanup no longer blocks downstream repo builds, improving parallel build performance by running cleanup as a detached background process.

Changes:

  • Run CleanupRepo deletion of $(RepoArtifactsDir) asynchronously via a detached process (Windows: start /B; Unix: nohup … &) with stream redirection to avoid Exec blocking.
  • Remove the build server shutdown step and related timeout properties from the cleanup path.
  • Remove incremental/stamp-based cleanup bookkeeping (Inputs/Outputs, CleanupRepo.complete) and the post-cleanup disk usage measurement.
Show a summary per file
File Description
repo-projects/Directory.Build.targets Detaches CleanupRepo artifact deletion (with redirected streams) to avoid blocking dependent repos during project-to-project builds.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 1/1 changed files
  • Comments generated: 1

Comment thread repo-projects/Directory.Build.targets Resolved
Comment thread repo-projects/Directory.Build.targets Resolved
Comment thread repo-projects/Directory.Build.targets Resolved
@ViktorHofer
ViktorHofer requested a review from mmitche August 6, 2026 17:42
start /B doesn't detach: cmd gives the spawned process an inheritable copy
of Exec's stdout/stderr pipes, so Exec waited for the full delete anyway.
Measured with an 8s command under Exec: start /B 9587ms vs 9488ms
synchronous, i.e. no gain. Start-Process uses ShellExecute, which doesn't
inherit handles: 2006ms, and the delete still runs to completion.

Redirection is still needed on Unix to close the inherited pipes (188ms vs
8203ms without), but it doesn't need to go to a file. Since a failed delete
is best effort and nothing consumes the output, redirect to /dev/null and
stop producing and publishing an empty CleanupRepo.log.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 8df1e800-36c4-4797-b8d7-d2c8e23c1342
@ViktorHofer

Copy link
Copy Markdown
Member Author

/azp run dotnet-unified-build-full

@dotnet dotnet deleted a comment from azure-pipelines Bot Aug 7, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 1 pipeline(s).

@ViktorHofer
ViktorHofer merged commit 10d231d into main Aug 7, 2026
66 of 69 checks passed
@ViktorHofer
ViktorHofer deleted the cleanup-repo-async branch August 7, 2026 12:14
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.

Build perf: Perform inner repo clean-up async

4 participants