WaitHandleCannotBeOpenedException on Azure web role start with Task.Wait()

我怕爱的太早我们不能终老 提交于 2020-01-11 05:17:26

问题


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();
}

来源:https://stackoverflow.com/questions/25633495/waithandlecannotbeopenedexception-on-azure-web-role-start-with-task-wait

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!