6

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();
}
Piedone
  • 2,693
  • 2
  • 24
  • 43
  • Is this code literally the only code that we'd have to upload into a fresh worker role to reproduce this? I can't believe it. – usr Sep 02 '14 at 23:20
  • 2
    Let me clarify an important detail I've forgotten to add: I see this exception from the debugger, when running locally through the emulator. And yes, it happens with exactly this code too. – Piedone Sep 03 '14 at 05:34
  • Post the full exception details (especially stack). It sounds like this is an internal, handled exception. – usr Sep 03 '14 at 11:55
  • I guess it is. Nevertheless it's something that is annoying for the team (you have to exclude this exception all the time from getting surfaced). The exception doesn't have any details but I added the call stack. – Piedone Sep 03 '14 at 16:23
  • Looks like they tried to implement the process singleton pattern using a Mutex but they failed to use the TryOpen method. If you like, decompile the Azure methods that are on the stack and I'll take a look. – usr Sep 03 '14 at 16:42
  • Though it's now purely an intellectual game I added it :-). Interesting though that this supposedly calls Run() but the web role OnStart() is called. – Piedone Sep 05 '14 at 10:11
  • Yeah, that's a quality of implementation problem. Report it. They are investing heavily in Azure. Maybe they will fix it. – usr Sep 05 '14 at 17:27
  • Here it is: https://connect.microsoft.com/VisualStudio/feedbackdetail/view/966413/waithandlecannotbeopenedexception-on-azure-web-role-start-with-task-wait Let's see what happens. – Piedone Sep 07 '14 at 21:49
  • @Piedone, would you mind contacting Microsoft to provide us more details for the exception you described here? We are investigating into this: https://connect.microsoft.com/VisualStudio/feedbackdetail/view/966413/waithandlecannotbeopenedexception-on-azure-web-role-start-with-task-wait – Cathy Nalan Sep 15 '14 at 20:56
  • So, is this fixed? 2 years later and no clue if I'm having the same problem! – Adam Heeg Apr 28 '16 at 18:31
  • 2
    @AdamHeeg No, now 3.5 years later it is not fixed – Jeppe Jan 09 '18 at 06:33
  • Cloud Services are on the way out anyway so I wouldn't expect a solution to this ever. – Shane Courtrille Feb 13 '18 at 23:45

0 Answers0