Skip to content

Retry native C++ project builds on transient MSBuild PCH failure - #2117

Merged
Tyrie Vella (tyrielv) merged 2 commits into
microsoft:masterfrom
tyrielv:tyrielv/retry-native-pch-build
Sep 24, 2026
Merged

Tyrie Vella (tyrielv) merged 2 commits into
microsoft:masterfrom
tyrielv:tyrielv/retry-native-pch-build

Conversation

@tyrielv

Copy link
Copy Markdown
Contributor

Problem and Context

PR validation builds on the arm64 runner intermittently fail with:

C3859: Failed to create virtual memory for PCH [...\GVFS.PostIndexChangedHook.vcxproj]

This happens while cl.exe builds one of the native hook projects. It is a
transient failure caused by memory pressure on the build host, not a problem
with the code being built — the same commit reliably builds successfully on
a manual re-run of the pipeline. Several PRs have hit this in the same week,
each requiring a human to notice the failure and manually retry the job.

Changes

  • scripts/Build.bat now retries the MSBuild invocation for each native
    vcxproj project (GitHooksLoader, GVFS.NativeTests,
    GVFS.PostIndexChangedHook, GVFS.ReadObjectHook,
    GVFS.VirtualFileSystemHook) up to 3 times, with a short delay between
    attempts, before failing the build. Only the native C++ build step is
    retried; a build that fails for a real compilation error will still fail
    after exhausting the retries.
  • This is local to Build.bat, so it applies uniformly to CI and to local
    developer builds without any workflow YAML changes.

PR validation builds on the arm64 runner intermittently fail with
`C3859: Failed to create virtual memory for PCH` while building one of
the native hook projects (e.g. GVFS.PostIndexChangedHook). This is a
transient cl.exe failure caused by memory pressure on the build host,
not a problem with the code being built, and the same build reliably
succeeds on a manual re-run.

Build.bat now retries each native vcxproj build (GitHooksLoader,
GVFS.NativeTests, GVFS.PostIndexChangedHook, GVFS.ReadObjectHook,
GVFS.VirtualFileSystemHook) up to 3 times, with a short delay between
attempts, before failing the build. This removes the need for a human
to notice the failure and manually re-run the pipeline.

Assisted-by: GitHub Copilot
Signed-off-by: Tyrie Vella <[email protected]>
@tyrielv
Tyrie Vella (tyrielv) marked this pull request as draft September 24, 2026 19:18
The previous commit retried a native vcxproj build on any MSBuild
failure, which would also retry (and mask, and waste time on) a real
compile error introduced by a code change. Capture each attempt's
output and only retry when the log actually contains the transient
C3859 "Failed to create virtual memory for PCH" error; any other
failure now fails immediately on the first attempt.

Assisted-by: GitHub Copilot
Signed-off-by: Tyrie Vella <[email protected]>
@tyrielv
Tyrie Vella (tyrielv) marked this pull request as ready for review September 24, 2026 19:43
@tyrielv
Tyrie Vella (tyrielv) merged commit cb20708 into microsoft:master Sep 24, 2026
35 checks passed

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I walked the batch control flow carefully before commenting, because retry wrappers in .bat are easy to get subtly wrong. The good news: this one is correct. %ERRORLEVEL% is captured at statement level rather than inside a parenthesized block, so there is no delayed-expansion trap; the FINDSTR inversion (IF NOT %NATIVE_IS_TRANSIENT% EQU 0) is the right way round; CALL :BuildNativeProject %%P || GOTO ERROR correctly propagates EXIT /B 1; and the subroutine sits after GOTO :EOF so it cannot be fallen into. I also checked that no native build loop was missed — the other two FOR loops are managed dotnet publish.

Eight findings, ranked.


1. Medium — TIMEOUT /T 10 /NOBREAK >NUL defeats itself in CI

TIMEOUT aborts immediately with ERROR: Input redirection is not supported, exiting the process immediately. whenever stdin is redirected — which is the case in GitHub Actions, i.e. precisely the environment this retry exists for.

Two consequences: the backoff silently becomes zero, so you retry straight back into the same memory pressure that just failed; and every retry injects a spurious ERROR: line into the log right next to a real build failure.

Use powershell -NoProfile -Command "Start-Sleep 10" or ping -n 11 127.0.0.1 >NUL instead.

2. Medium — buffering MSBuild to a temp file kills streaming output

> "%NATIVE_LOG%" 2>&1 followed by TYPE means nothing prints during the native build phase, in CI or locally.

You can keep live output and get a scannable file by dropping the redirect and adding MSBuild's own file logger:

/flp:LogFile="%NATIVE_LOG%";Verbosity=normal

Same FINDSTR afterwards, no loss of interactivity.

3. Medium — a failed redirect loses all diagnostics

If %TEMP% is unset or unwritable the redirect fails and all MSBuild output is lost. FINDSTR then matches nothing, the failure is classified non-transient, and the build dies with only ERROR: Build of X failed with a non-transient error; not retrying. and zero diagnostics — the worst possible outcome for a genuine compile error. The /flp approach in finding 2 fails loudly instead of silently.

4. Medium — is retry the right layer for this?

C3859 is a memory-pressure symptom, and retrying is a band-aid. All five of these .vcxproj are tiny — a handful of sources each — yet all five set <PrecompiledHeader>Use</PrecompiledHeader> with a Create on stdafx.cpp. Is PCH buying enough on projects this small to be worth a retry loop that can add 30+ seconds and still fail?

Dropping PCH from them removes the failure mode permanently rather than absorbing it. Failing that, /Zm or reduced parallelism. Has either been measured?

5. Low — FINDSTR /C:"C3859" is looser than it needs to be

It matches the bare code anywhere in the log, including in a file path or a source line that happens to contain it. /C:"error C3859" is the exact form MSVC emits.

6. Low — ECHO WARNING: is invisible in Actions

Plain ECHO WARNING: is not surfaced by GitHub Actions. ECHO ::warning::Build of %~n1 retried after C3859 puts it in the run summary, so you notice if this starts firing on every build instead of quietly masking a degrading runner pool. That matters for a retry whose whole purpose is to hide a failure.

7. Low — subroutine leaks variables

:BuildNativeProject runs without SETLOCAL, so NATIVE_PROJ, NATIVE_ATTEMPT, NATIVE_LOG, NATIVE_BUILD_RESULT and NATIVE_IS_TRANSIENT persist into the caller for the rest of the script. SETLOCAL plus the existing EXIT /B keeps it contained.

8. Low — comment narrates the investigation

"(observed on the arm64 runners, where PCH creation can collide with other concurrent processes for address space). It is not caused by anything in this repo's code and reliably succeeds on a retry" — the field report belongs in the PR and the linked work item. The script only needs the invariant: C3859 is a transient PCH allocation failure, so retry it and nothing else. Keep the "only retry C3859" rationale, which is the genuinely valuable half.

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.

3 participants