The following (web) role entry point, after returning, causes the below exception to be thrown.
public class WebRole : RoleEntryPoint
{
public override bool OnStart()
{
Task.Run(() =>
{
// Anything can be here, but the lamdbda can be empty too...
}).Wait();
return true;
}
}
The exception:
A first chance exception of type 'System.Threading.WaitHandleCannotBeOpenedException' occurred in mscorlib.dll
Additional information: No handle of the given name exists.
As apparent this is thrown from the framework. The exception appears to be harmless, I haven't experienced any issues.
Still I'm curious, why this happens? Is there a way to run async code in the role start method not causing such exceptions?
Edit:
This is the exception's call stack:
> mscorlib.dll!System.Threading.EventWaitHandle.OpenExisting(string name, System.Security.AccessControl.EventWaitHandleRights rights) Unknown
Microsoft.WindowsAzure.ServiceRuntime.dll!Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.StartRoleInternal() Unknown
Microsoft.WindowsAzure.ServiceRuntime.dll!Microsoft.WindowsAzure.ServiceRuntime.RoleEnvironment.StartRole() Unknown
Microsoft.WindowsAzure.ServiceRuntime.dll!Microsoft.WindowsAzure.ServiceRuntime.Implementation.Loader.RoleRuntimeBridge.StartRole.AnonymousMethod__2() Unknown
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) Unknown
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) Unknown
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) Unknown
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart() Unknown
[Native to Managed Transition]
Edit 2:
This is the Azure runtime source in question:
private void StartRoleInternal()
{
string environmentVariable = Environment.GetEnvironmentVariable("RdRoleId");
string name = RoleEnvironment.NormalizeEventName("Global\\started_{0}", environmentVariable);
try
{
using (EventWaitHandle eventWaitHandle = EventWaitHandle.OpenExisting(name, EventWaitHandleRights.Modify))
{
eventWaitHandle.Set();
}
}
catch (WaitHandleCannotBeOpenedException)
{
}
RoleEnvironment.TraceSource.TraceEvent(TraceEventType.Information, 203, "Role entrypoint . CALLING Run(): " + this.role);
SystemEvents.LogEventToSystemEvents("Windows Azure Runtime 2.3.0.0", RuntimeEventType.OnRoleRunBegin, "Role is running: OnRun(): " + this.role);
this.role.Run();
RoleEnvironment.TraceSource.TraceEvent(TraceEventType.Warning, 204, "Role entrypoint . COMPLETED Run() ==> ROLE RECYCLING INITIATED: " + this.role);
SystemEvents.LogEventToSystemEvents("Windows Azure Runtime 2.3.0.0", RuntimeEventType.OnRoleRunEnd, "Role will recyle: OnRun(): " + this.role);
RoleEnvironment.RequestRecycle();
}