Fix issue 370 temp-directory lock cleanup races (suggestions 2 and 3) - #371
Merged
Conversation
Make the temp-directory lock lifetime atomic and stop escalating best-effort cleanup failures to Debug.Fail, addressing the intermittent Windows failures in ReadCompilerCallWrongOwner and ProjectReferences_Corrupted. - Own the <guid>.lock file with FileOptions.DeleteOnClose on Windows so it is removed atomically when the handle closes (including on a hard crash), eliminating the release/delete window in Dispose. On Unix DeleteOnClose is not combined with FileShare.None (dotnet/runtime#59995 disables share enforcement in that case); the lock is a plain FileShare.None handle deleted manually in Dispose. - Redefine cleanup staleness: a directory is orphaned when its lock file is missing, or exists but can be opened exclusively (a crash-safe liveness probe since the OS releases the share lock on process death). Live owners are left alone. - Downgrade the two Debug.Fail calls in Dispose to silent best-effort no-ops. - Update CommonUtilTests and LogReaderStateTests to the probe-based semantics. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #371 +/- ##
==========================================
- Coverage 95.76% 95.75% -0.01%
==========================================
Files 56 56
Lines 7786 7803 +17
Branches 907 911 +4
==========================================
+ Hits 7456 7472 +16
- Misses 186 187 +1
Partials 144 144 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
jaredpar
enabled auto-merge (squash)
July 23, 2026 20:41
The CI failure (UnauthorizedAccessException on locks\<guid>.lock) is Race A from issue #370: the shared locks/ directory (and its parent) were being deleted via DeleteDirectoryIfEmpty in both LogReaderState.Dispose and CommonUtil.CleanupStaleTempDirectories. That delete races with another instance concurrently creating its lock file inside locks/, surfacing as UnauthorizedAccessException/DirectoryNotFoundException. Apply suggestion 1 from the issue: never delete the shared locks/ directory or the parent root. They are long-lived and shared by every concurrent LogReaderState; leaving the empty directories behind is harmless. Only the per-owner working directories and lock files are reclaimed. Verified with a 16-thread stress harness: 555 errors before, 0 after. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
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.
Why
Two unit tests (
CompilerLogReaderTests.ReadCompilerCallWrongOwnerandProjectReferences_Corrupted) fail intermittently onwindows-latestunder parallel load. Neither touches the lock system directly; they fail because everyLogReaderStateusing the default shared temp directory participates in thelocks/cleanup protocol from #366/#367, which has file-sharing races. This implements suggestions 2 and 3 from #370.What changed
Atomic lock lifetime (suggestion 2). The
<guid>.lockfile is now owned for the instance lifetime and its removal is tied to the handle:FileOptions.DeleteOnClose, so the file is removed atomically when the handle closes (including on a hard crash, where the kernel enforces it). This removes the release-then-File.Deletewindow inDisposethat let cleanup race with disposal.DeleteOnCloseis deliberately not combined withFileShare.None, because that silently disables share enforcement (On Linux, FileShare.None doesn't seem to be taken into account when retrying to open a locked file dotnet/runtime#59995) and would let the cleanup probe delete a live owner's directory. Instead the lock is a plainFileShare.Nonehandle deleted manually inDispose; Unix has no delete-pending semantics so that delete is race-free.Crash-safe staleness probe.
CleanupStaleTempDirectoriesnow treats a directory as orphaned when its lock file is missing, or exists but can be opened exclusively. A successful exclusive open is a reliable liveness test on both platforms because the OS releases the share lock when the owning process dies. Directories whose lock is still held by a live owner are left alone.No
Debug.Failon best-effort cleanup (suggestion 3). The twoDebug.Failcalls inDisposeare downgraded to silent best-effort no-ops so expected concurrent-cleanup failures never assert in Debug/CI.Tests in
CommonUtilTestsandLogReaderStateTestsare updated to the probe-based semantics (unheld lock file => delete, held lock file => skip).Notes for reviewers
DeleteOnClose+FileShare.Noneinteraction (On Linux, FileShare.None doesn't seem to be taken into account when retrying to open a locked file dotnet/runtime#59995) is the non-obvious part; there are comments in the ctor and probe explaining why the two platforms differ so nobody re-addsDeleteOnCloseon Unix. Windows validated locally (targeted + full net10.0 suite); Linux share enforcement is validated by CI.locks/directory mid-construction throwsDirectoryNotFoundException/UnauthorizedAccessExceptionwhen opening the lock file (deterministically 555 errors in a 16-thread harness, 0 after applying suggestion 1). Fixing that requires suggestion 1 (stop deleting the sharedlocks/directory), which is not included here. Without it the original flaky tests can still intermittently fail, so a follow-up for suggestion 1 is recommended.