Repository navigation
Description
Description
After Csc and Vbc opted into MSBuild multithreaded task execution in dotnet/roslyn#84701, a dotnet/sdk file-based-app test can intermittently fail when -mt builds two project references in the same MSBuild process with shared compilation enabled:
Microsoft.CSharp.Core.targets(97,5): error MSB3883: Unexpected exception:
Microsoft.CSharp.Core.targets(97,5): error : Cannot release a lock that is not owned by the current thread.
The failure is tracked by dotnet/sdk#56238. It occurred on Ubuntu 22.04 x64 with .NET 11 SDK 11.0.100-ci; test history reported a 0.52% failure rate. The failing Helix log shows LibraryB completing while another compiler invocation fails.
Distinguishing trigger
The failing test creates a file-based app with two #:project references and runs:
dotnet build Program.cs -mt
It also sets MSBUILDUSESERVER=0, so the MSBuild server is disabled while Roslyn shared compilation remains enabled. In the same 108-test work item, the ordinary file-based builds passed; only MultiThreadedArgument_BuildsProjectReferencesInProcess(0, -mt) failed. Its -mt:false row passed immediately afterward.
dotnet/sdk#56239 proposes UseSharedCompilation=false for this one test to isolate its MSBuild in-process-node assertion. That is useful test isolation, but it is a mitigation rather than a fix for this failure.
Relevant implementation and history
BuildServerConnection.RunServerBuildRequestAsync serializes server startup with a named client mutex. Its tryConnectToServerAsync local function is deliberately non-async so WaitOne and ReleaseMutex execute on the same thread. However, Roslyn's current tests exercise requests sequentially and do not appear to cover concurrent calls sharing the same pipe/client mutex from one process.
This resembles:
- ReleaseMutex failed. WaitOne Id: {originalThreadId} Release Id: {releaseThreadId} in Microsoft.Extensions.Logging.Generators.Roslyn4.0 roslyn#63863
- dotnet/runtime#53420, closed in July after the failure had not been observed following the .NET 11 wait-subsystem transition
- dotnet/dotnet#4088, where the same raw exception came from
ServerNamedMutex.Dispose
The runtime synchronization correction associated with dotnet/dotnet#4088 is present in the runtime used by this failure, so this appears to be a recurrence exposed by the new concurrent compiler-task execution rather than simply a missing backflow.
The current failure also lacks Roslyn's WaitOne Id / Release Id diagnostic. tryConnectToServerAsync catches ApplicationException around mutex disposal, while the .NET 11 Unix implementation surfaced this ownership failure as InvalidOperationException in dotnet/dotnet#4088.
Requested investigation
- Add a Linux regression/stress test that invokes
RunServerBuildRequestAsyncconcurrently with the same pipe name, or otherwise runs concurrent shared-compilationCsctasks. - Determine whether Roslyn is violating the mutex lifetime contract or whether this is a .NET 11 Unix named-mutex regression; transfer or split the runtime portion if needed.
- Preserve the acquisition/release thread IDs when this exception occurs on .NET 11 so a future occurrence is actionable.
Note
This report was prepared with GitHub Copilot assistance from the linked failure logs and source investigation.
Activity
JanProvaznik commentedon Sep 15, 2026
Local investigation found an independently reproducible .NET 11 Linux named-mutex ownership defect, and captured a failure through the actual shared-compilation
Cscpath. This supports a runtime fix, with separate Roslyn regression coverage and diagnostics work.Observed product failure
Environment: Ubuntu 24.04 x64 under WSL, SDK
11.0.100-rc.1.26453.118, runtime11.0.0-rc.1.26453.118, MSBuild18.12.0-1.26453.118, and Roslyn build tasks5.12.0-1.26453.118. All report VMR commit3c8d132bfb012af4bbba80de22149a9f58e737f0. The product binaries were not modified.An ordinary project with 32 project references failed on repetition 7 of:
The binlog identifies
Lib17.csproj/CoreCompile/Csc, withMSB3883andCannot release a lock that is not owned by the current thread.atMicrosoft.CSharp.Core.targets(97,5).A first-chance exception handler captured the live stack before Roslyn caught the exception (abridged; arguments omitted):
A native observer also recorded the first
pthread_mutex_trylockfollowing initialization of a shared, robust mutex on that thread:EBUSY(16)11:53:02.507844650InvalidOperationException11:53:02.5135684/proc/self/mapsidentified the backing file as:The observer calls the real pthread functions and returns their results unchanged; it does not deliberately schedule threads, sleep, retry, or manufacture failures. Instrumentation can affect timing, and the interval above includes diagnostic overhead.
This is a local product-build reproduction, not a recovered trace of the original Helix occurrence. The smaller two-reference build passed 200 repetitions. The product trace does not directly capture the competing thread's complete sequence or the managed constructor's
createdNewoutput.Source-derived diagnosis
In the exact runtime revision:
CreateOrOpenpublishes the newly initialized mutex in the process-local cache.TryAcquireLockreleases the creation/deletion process lock before native acquisition.TimedOut.CreateOrOpenchecks success only withDebug.Assert, then returns withcreatedNew=true.A separate controlled runtime experiment observed that last condition directly: the constructor returned
createdNew=trueafter its real native trylock returnedEBUSY; release on the original thread then failed.This contradicts the documented constructor contract:
initiallyOwned=truegrants ownership if this call creates the named mutex. Multiple wrappers for the same named mutex are explicitly supported.For comparison, .NET 10's native implementation acquires initial ownership while retaining the creation/deletion locks.
Uninstrumented runtime reproducer
The following tested program uses two threads, one fixed mutex name, and no barriers, sleeps, yields, async code, or native instrumentation:
11.0.0-rc.1.26453.118created=true, acquisition and release managed thread IDs both 410.0.11These are stress observations, not estimates of customer failure rates.
Complete minimal project and program
MutexNatural.csproj:Program.cs:Build using a .NET 10 SDK, then run the same DLL with each runtime:
The .NET 11 SDK used for the product trace is available at:
https://ci.dot.net/public/Sdk/11.0.100-rc.1.26453.118/dotnet-sdk-11.0.100-rc.1.26453.118-linux-x64.tar.gz
Suggested split and remaining work
InvalidOperationExceptionas well asApplicationException. The current catch misses this runtime exception.The fix associated with dotnet/dotnet#4088 is present: it made the creation/deletion file lock blocking. It does not exclude the same-process cache-hit interleaving above. #53420 is relevant symptom history, but the pre-.NET-11 failures are not explained by this diagnosis; its closure comment explicitly requests a new issue for recurrence.
No production fix or checked-in Roslyn regression test has been made by this investigation. The full local binlog/capture bundle is not attached to this comment; the product trace excerpts and runnable runtime reproducer are included above.
Investigation and write-up performed with GitHub Copilot. Observed results, source-derived diagnosis, and unvalidated proposals are distinguished above.
JanProvaznik commentedon Sep 15, 2026
@jkoritzinsky Does this analysis sound relevant and reasonable? Is this indeed a runtime bug introduced with the 10->11 change you implemented?
jkoritzinsky commentedon Sep 15, 2026
The analysis sounds reasonable. I do remember changing the process lock rules here because we hit a deadlock. Will see what change I need to make to get this working.
dotnet-policy-service commentedon Sep 16, 2026
Tagging subscribers to this area: @JulieLeeMSFT, @VSadov
See info in area-owners.md if you want to be subscribed.
Fix concurrent Unix named mutex creation (#134111)
EgorBo commentedon Sep 29, 2026
Ugh. My EgorBot bot keeps failing because of this bug - I assume we still use the buggy bootstrap SDK.