System.InvalidOperationException: Mutex could not be created.

We've seen a few of these issues come through lately andwanted to get something posted so people can find it.

If you created a custom account to run the process that ASP.NET is running in, you may encounter the following exception:

System.InvalidOperationException: Mutex could not be created.Stack Trace: [InvalidOperationException: Mutex could not be created.] System.Web.Compilation.CompilationMutex..ctor(String name, String comment) +3464689 System.Web.Compilation.CompilationLock..cctor() +158

[TypeInitializationException: The type initializer for 'System.Web.Compilation.CompilationLock' threw an exception.] System.Web.Compilation.CompilationLock.GetLock(Boolean& gotLock) +32 System.Web.Compilation.BuildManager.EnsureTopLevelFilesCompiled() +114

[HttpException (0x80004005): The type initializer for 'System.Web.Compilation.CompilationLock' threw an exception.] System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +3426871 System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +88 System.Web.HttpRuntime.ProcessRequestInternal(HttpWorkerRequest wr) +149--------------------------------------------------------------------------------Version Information: Microsoft .NET Framework Version:2.0.50727.42; ASP.NET Version:2.0.50727.42

Note: The error will appear to be random, but once it appears, will remain until a reboot or until the handles to the mutex are closed.

Cause:
======
This occurs because the permissions on the following registry key no longer have your custom account:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\ASP.NET\2.0.50727.0\CompilationMutexName

The compilation mutex gets it's permissions from this registry key and with your custom account missing, the process running ASP.NET cannot get a handle to the mutex and fails during compilation.

Resolution:=========

  1. Run the following to add your custom account to the registry key permissions:
             ASPNET_regiis -ga domain\account.
  2. Either restart the server or close the handles to the mutex (see below)

To close the handles to the Mutex:

  1. Download and launch Process Explorer from www.sysinternals.com
  2. From the Find menu, select Find Handle or DLL
  3. In the Handle or DLL substring box, type “mutant” (without quotes) and click Search
  4. Click the Handle or DLL column heading to sort the items and find a mutex that starts with CL. The handle will typically be devenv.exe (the IDE for Visual Studio) and aspnet_wp.exe or w3wp.exe. The handle will look similar to:
  5. \BaseNameObjects\CLbdd6aa8f
  6. Select the handle in the search box and Process Explorer locates the Process and the handle in the main window
  7. Right-click the handle in the main window and select Close Handle

Additional Info:
============
The error may occur anytime you run ASP.NET as a custom account and the above mentioned registry key does not have the account listed in the permissions.

If you receive the error and you are running with a custom account on IIS 6 in worker process mode (code running in w3wp.exe), you can add your custom account to the IIS_WPG group on the server. The IIS_WPG group is granted access to the registry key by default.