Skip to content

Crash: "Object synchronization method was called from an unsynchronized block of code" in DotnetGlobalToolInstaller.EnsureCoreAsync (Mutex.ReleaseMutex) #145

Open

Description

@xperiandri

Bug Report

Exception

System.ApplicationException: Object synchronization method was called from an unsynchronized block of code.
   at System.Threading.Mutex.ReleaseMutex()
   at Studio.Common.DotnetGlobalToolInstaller.EnsureCoreAsync(...)

The exception is thrown from Mutex.ReleaseMutex() (internal .NET implementation) when Win32Native.ReleaseMutex fails.

Call Stack (key frames)

  • Studio.Common.DotnetGlobalToolInstaller.EnsureCoreAsync
  • Async continuation: AsyncMethodBuilderCore.MoveNextRunner.InvokeMoveNext
  • System.Threading.ExecutionContext.RunInternal

This happens during resumption of an async method (EnsureCoreAsync).

Environment

  • IDE: Microsoft Visual Studio Enterprise 2026 (18.10.0-insiders)
  • OS: Windows_NT
  • Process: devenv.exe (PID 22796)
  • Thread: 4852 (where the exception occurred)

Reproduction

The crash occurs while the Uno Platform Studio extension attempts to ensure .NET global tools (likely dotnet tool installation/restore logic protected by a Mutex).

Root Cause Analysis

Mutex in .NET is a thread-affine synchronization primitive:

  • It must be released by the exact same thread that acquired it.
  • It must be released exactly once per successful WaitOne.

In async code (EnsureCoreAsync), after an await, the continuation can resume on a different thread (especially without ConfigureAwait(false) or when a SynchronizationContext / TaskScheduler switches threads, which is common inside Visual Studio).

When the continuation later calls mutex.Release(), it is no longer the owning thread → ReleaseMutex fails → ApplicationException.

Suggested Fix

  1. Replace Mutex with SemaphoreSlim (preferred for async code).
  2. Or wrap the critical section to ensure release happens on the original thread (e.g., using SynchronizationContext capture or Task.Run on a dedicated thread).
  3. Ensure proper try/finally around ReleaseMutex and avoid double-release paths.

Additional Context

Full debugger context and source of Mutex.cs (lines around ReleaseMutex) available in the attached diagnostic session.

This is a blocking crash for users of the Uno Studio extension inside Visual Studio.

Please investigate Studio.Common.DotnetGlobalToolInstaller.

Activity

  1. changed the title [-]Crash: "Object synchronization method was called from an unsynchronized block of code" in DotnetGlobalToolInstaller.EnsureCoreAsync (Mutex.ReleaseMutex)[/-] [+]Crash: "Object synchronization method was called from an unsynchronized block of code" in `DotnetGlobalToolInstaller.EnsureCoreAsync (Mutex.ReleaseMutex)`[/+] on Aug 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions