Fix RegFree manifest-file race under parallel MSBuild#988
Conversation
Six EXE projects (FieldWorks, LCMBrowser, UnicodeCharEditor, GenerateHCConfig, ComManifestTestHost, NativeBuild) each import RegFree.targets and generate manifests for the same shared managed assemblies (FwUtils.dll, SimpleRootSite.dll, ManagedVwWindow.dll) into the same $(OutDir). Under a parallel MSBuild build, their CreateComponentManifests targets can run in different MSBuild worker processes at the same time and race to read/write the exact same manifest file, throwing an IOException that fails the whole build (observed intermittently in CI, e.g. FieldWorks PR #964). Wrap RegFree.Execute()'s read-modify-write of the manifest file in a cross-process named Mutex keyed by the resolved output path, so concurrent invocations targeting the same file serialize instead of racing; invocations for different manifest files are unaffected and still run fully in parallel. string.GetHashCode() is deliberately not used for the mutex name since .NET randomizes it per process, which would defeat cross-process synchronization - MD5 is used instead as a deterministic fingerprint. Added a regression test that runs 12 concurrent RegFree.Execute() calls against the same manifest path and asserts they all succeed and produce valid, uncorrupted XML. Verified it actually catches the regression: temporarily reverted the mutex fix, confirmed the test fails reliably (3/3 runs), then restored the fix and confirmed it passes reliably (5/5 runs). Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
33cdfd2 to
a646ea3
Compare
Derive a terse-by-default comment convention from the existing codebase (one-line /// summaries; inline // only where non-obvious; expand only for rationale, concurrency/ordering hazards, interop quirks, deliberate anti-obvious choices, or bug-driven behavior) and record it in AGENTS.md. Apply it to this PR's own comments: drop the project/DLL enumerations from the mutex block comment (that detail lives in the commit message and goes stale) and tighten the ManifestLockName summary, keeping the load-bearing "why not GetHashCode" reasoning. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Code review (verified against the code at the PR head)Overall: the mutex approach is sound and the regression test (12 concurrent MEDIUM —
|
If a prior MSBuild worker holding the manifest mutex is killed mid-write, the next waiter's WaitOne() throws AbandonedMutexException outside the existing try/catch, turning a handled build error into an unhandled task failure (and skipping the mutex release in the finally). Ownership is still granted on this exception, so treat it as a normal acquire. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
|
Fixed in 73ea066: wrapped |
Summary
FieldWorks,LCMBrowser,UnicodeCharEditor,GenerateHCConfig,ComManifestTestHost,NativeBuild) each importBuild/RegFree.targetsand generate manifests for the same shared managed assemblies (FwUtils.dll,SimpleRootSite.dll,ManagedVwWindow.dll) into the same$(OutDir). Under a parallel MSBuild build, theirCreateComponentManifeststargets can run in different MSBuild worker processes at the same time and race to read/write the exact same manifest file, throwing anIOExceptionthat fails the whole build.phase1-base(PR Phase-1 base: Avalonia migration spine (UIMode defaults Legacy) #964):RegFree.targets(91,3): error : IOException: The process cannot access the file 'FwUtils.manifest' because it is being used by another process. A retry of the same CI job passed cleanly, confirming it's a timing-dependent race, not a code regression.Build/Src/FwBuildTasks/RegFree.cs: the crash is atXmlWriter.Create(manifestFile, settings)(line 349 in the observed stack trace), which is exactly where multiple processes writing the same output path collide.Fix
RegFree.Execute()'s full read-modify-write of the manifest file (load existing manifest → process assemblies/DLLs/fragments → write result) is now wrapped in a cross-process namedMutexkeyed by the resolved output path (ManifestLockName). Invocations targeting the same manifest file now serialize; invocations for different manifest files are unaffected and still run fully in parallel — this doesn't reduce build parallelism for the common case, only for the specific shared-file collision.string.GetHashCode()is deliberately not used for the mutex name: .NET randomizes string hash codes per process for security, so two different MSBuild worker processes could compute different hash codes for the identical path, defeating cross-process synchronization entirely. MD5 (not used for anything security-sensitive here, just as a stable fingerprint) is deterministic across processes, machines, and .NET versions.Test plan
RegFreeConcurrencyTests.Execute_ConcurrentInvocationsTargetingSameManifest_AllSucceedAndProduceValidXml: runs 12 concurrentRegFree.Execute()calls against the same manifest path (simulating what the 6 EXE projects' parallel MSBuild nodes do) and asserts they all succeed and produce valid, uncorrupted XML..\build.ps1 -BuildTestssucceeds.FwBuildTasksTestssuite: 146 passed, 3 pre-existing skips, 0 failures.🤖 Generated with Claude Code
This change is
Also in this PR (docs)
Adds a short "Comments" heuristic to
AGENTS.md(terse by default; expand only for rationale, concurrency/interop hazards, deliberate anti-obvious choices, or bug-driven behavior), derived from the existing codebase, and applies it to this PR's own comments — trimming the mutex block comment and theManifestLockNamesummary while keeping the load-bearing "why notGetHashCode" reasoning.