Hangfire disposes of autofac dependencies which are defined as SingleInstance - c#

Using hangfire 1.3.4 with hangfire.autofac 1.0.0 in an ASP.NET application.
I have the following scenario:
class MyType : IDisposable
{
public void Start()
{
RecurringJob.AddOrUpdate("background-update", () => ProcessData(), Cron.Daily());
RecurringJob.Trigger("background-update");
}
public void ProcessData(){...}
public void Dispose(){...}
}
...
var builder = new ContainerBuilder();
builder.RegisterType<MyType>().SingleInstance();
var cont = builder.Build();
app.UseHangfire(config =>
{
var options = new SqlServerStorageOptions();
config.UseAutofacActivator(cont);
config.UseSqlServerStorage("MyServer", options);
config.UseServer();
});
...
var c = cont.Resolve<MyType>();
c.Start();
What I see is that Autofac executes the recurrent job as requested but then disposes of instance of MyType, which obviously causes failures on subsequent calls for it, since it's defined as a singleton, and should be disposed of by Autofac upon shutdown.
Am I missing something or is this a bug?
Here's the callstack:
MyDll.dll!MyType.Dispose() Line 316 C#
Hangfire.Core.dll!Hangfire.Common.Job.Dispose(object instance) Unknown
Hangfire.Core.dll!Hangfire.Common.Job.Perform(Hangfire.JobActivator activator, Hangfire.IJobCancellationToken cancellationToken) Unknown
Hangfire.Core.dll!Hangfire.Server.JobPerformanceProcess.PerformJobWithFilters.AnonymousMethod__6() Unknown
Hangfire.Core.dll!Hangfire.Server.JobPerformanceProcess.InvokePerformFilter(Hangfire.Server.IServerFilter filter, Hangfire.Server.PerformingContext preContext, System.Func continuation) Unknown
Hangfire.Core.dll!Hangfire.Server.JobPerformanceProcess.PerformJobWithFilters.AnonymousMethod__8() Unknown
Hangfire.Core.dll!Hangfire.Server.JobPerformanceProcess.InvokePerformFilter(Hangfire.Server.IServerFilter filter, Hangfire.Server.PerformingContext preContext, System.Func continuation) Unknown
Hangfire.Core.dll!Hangfire.Server.JobPerformanceProcess.PerformJobWithFilters.AnonymousMethod__8() Unknown
Hangfire.Core.dll!Hangfire.Server.JobPerformanceProcess.PerformJobWithFilters(Hangfire.Server.PerformContext context, Hangfire.Server.IJobPerformer performer, System.Collections.Generic.IEnumerable filters) Unknown
Hangfire.Core.dll!Hangfire.Server.JobPerformanceProcess.Run(Hangfire.Server.PerformContext context, Hangfire.Server.IJobPerformer performer) Unknown
Hangfire.Core.dll!Hangfire.Server.Worker.ProcessJob(string jobId, Hangfire.Storage.IStorageConnection connection, Hangfire.Server.IJobPerformanceProcess process, System.Threading.CancellationToken shutdownToken) Unknown
Hangfire.Core.dll!Hangfire.Server.Worker.Execute(System.Threading.CancellationToken cancellationToken) Unknown
Hangfire.Core.dll!Hangfire.Server.AutomaticRetryServerComponentWrapper.ExecuteWithAutomaticRetry(System.Threading.CancellationToken cancellationToken) Unknown
Hangfire.Core.dll!Hangfire.Server.AutomaticRetryServerComponentWrapper.Execute(System.Threading.CancellationToken cancellationToken) Unknown
Hangfire.Core.dll!Hangfire.Server.ServerSupervisor.ExecuteComponent() Unknown
Hangfire.Core.dll!Hangfire.Server.ServerSupervisor.RunComponent() Unknown
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object state) 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]

This is fixed in latest (beta) releases of hangfire, see https://github.com/HangfireIO/Hangfire/issues/329 for details.

Related

WindowsIdentity.Impersonate() in owin middleware

We're using following OWIN middleware in our web app to impersonate user based on incoming request.
public class SomeMiddleware : OwinMiddleware
{
public SomeMiddleware(OwinMiddleware next) : base(next)
public override async Task Invoke(IOwinContext context)
{
var identity = context.Authentication.User?.Identity;
if(identity is WindowsIdentity windowsIdentity)
{
using var ctx = windowsIdentity.Impersonate();
await Next.Invoke(context);
}
}
}
The other middleware in pipeline logs stuff with log4net. The appender layout template has user identity.
Sometimes the web app blows up with following error.
System.ObjectDisposedException: Safe handle has been closed
at System.Runtime.InteropServices.SafeHandle.DangerousAddRef(Boolean& success)
at System.StubHelpers.StubHelpers.SafeHandleAddRef(SafeHandle pHandle, Boolean& success)
at Microsoft.Win32.Win32Native.GetTokenInformation(SafeAccessTokenHandle TokenHandle, UInt32 TokenInformationClass, SafeLocalAllocHandle TokenInformation, UInt32 TokenInformationLength, UInt32& ReturnLength)
at System.Security.Principal.WindowsIdentity.GetTokenInformation(SafeAccessTokenHandle tokenHandle, TokenInformationClass tokenInformationClass)
at System.Security.Principal.WindowsIdentity.get_User()
at System.Security.Principal.WindowsIdentity.GetName()
at System.Security.Principal.WindowsIdentity.get_Name()
at log4net.Core.LoggingEvent.get_Identity()
at log4net.Layout.Pattern.IdentityPatternConverter.Convert(TextWriter writer, LoggingEvent loggingEvent)
at log4net.Layout.Pattern.PatternLayoutConverter.Convert(TextWriter writer, Object state)
at log4net.Util.PatternConverter.Format(TextWriter writer, Object state)
at log4net.Layout.PatternLayout.Format(TextWriter writer, LoggingEvent loggingEvent)
at log4net.Appender.AppenderSkeleton.RenderLoggingEvent(TextWriter writer, LoggingEvent loggingEvent)
at log4net.Appender.TextWriterAppender.Append(LoggingEvent loggingEvent)
at log4net.Appender.FileAppender.Append(LoggingEvent loggingEvent)
at log4net.Appender.RollingFileAppender.Append(LoggingEvent loggingEvent)
at log4net.Appender.AppenderSkeleton.DoAppend(LoggingEvent loggingEvent)
...
Invalid token for impersonation - it cannot be duplicated.</Message><StackTrace> at System.Security.Principal.WindowsIdentity.CreateFromToken(IntPtr userToken)
at System.Security.Principal.WindowsIdentity..ctor(IntPtr userToken, String authType, Int32 isAuthenticated)
at System.Security.Principal.WindowsIdentity..ctor(SafeAccessTokenHandle safeTokenHandle)
at System.Security.SecurityContext.CaptureCore(Reader currThreadEC, StackCrawlMark&amp; stackMark)
at System.Threading.ExecutionContext.Capture(StackCrawlMark&amp; stackMark, CaptureOptions options)
at System.Threading.ExecutionContext.FastCapture()
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.GetCompletionAction(Task taskForTracing, MoveNextRunner&amp; runnerToInitialize)
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.AwaitUnsafeOnCompleted[TAwaiter,TStateMachine](TAwaiter&amp; awaiter, TStateMachine&amp; stateMachine)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.&lt;&gt;c.&lt;ThrowAsync&gt;b__6_1(Object state)
at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()</StackTrace><ExceptionString>System.ArgumentException: Invalid token for impersonation - it cannot be duplicated.
at System.Security.Principal.WindowsIdentity.CreateFromToken(IntPtr userToken)
at System.Security.Principal.WindowsIdentity..ctor(IntPtr userToken, String authType, Int32 isAuthenticated)
at System.Security.Principal.WindowsIdentity..ctor(SafeAccessTokenHandle safeTokenHandle)
at System.Security.SecurityContext.CaptureCore(Reader currThreadEC, StackCrawlMark&amp; stackMark)
at System.Threading.ExecutionContext.Capture(StackCrawlMark&amp; stackMark, CaptureOptions options)
at System.Threading.ExecutionContext.FastCapture()
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.GetCompletionAction(Task taskForTracing, MoveNextRunner&amp; runnerToInitialize)
at System.Runtime.CompilerServices.AsyncTaskMethodBuilder`1.AwaitUnsafeOnCompleted[TAwaiter,TStateMachine](TAwaiter&amp; awaiter, TStateMachine&amp; stateMachine)
--- End of stack trace from previous location where exception was thrown ---
at System.Runtime.CompilerServices.AsyncMethodBuilderCore.&lt;&gt;c.&lt;ThrowAsync&gt;b__6_1(Object state)
at System.Threading.QueueUserWorkItemCallback.WaitCallback_Context(Object state)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem()
at System.Threading.ThreadPoolWorkQueue.Dispatch()
at System.Threading._ThreadPoolWaitCallback.PerformWaitCallback()</ExceptionString></Exception></TraceRecord>
An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll
How likely is the middleware at fault here? Am I looking at the wrong place?

SignalR HubConnection StopAsync throws ObjectDisposedException

We have a WPF client that connects with SignalR to one of our servers. When closing the WPF client we also close the SignalR HubConnection:
public HubConnection Connection => _connection ??= GetConnection();
private HubConnection GetConnection()
{
var host = ConfigurationManager.AppSettings["SignalRHost"];
var connection = new HubConnectionBuilder()
.WithUrl(host)
.Build();
connection.Closed += ConnectionOnClosed;
return connection;
}
private async Task ConnectionOnClosed(Exception exception)
{
// The connection is disconnected
if (_disconnecting)
{
// The disconnect was requested by the client
Connection.Closed -= ConnectionOnClosed;
Logger.Information("The connection to SignalR hub was closed.");
}
else
{
await StartAsync();
}
}
public async Task Disconnect()
{
if (_disconnecting) return;
try
{
_disconnecting = true;
await _connection.StopAsync();
}
catch (Exception e)
{
Logger.Error(e, "Error in closing connection");
}
finally
{
await _connection.DisposeAsync();
_connection = null;
}
}
Calling CloseAsync results in two ObjectDisposedExceptions. I only have a partial stacktrace (that seems to be another problem in my VS):
System.dll!System.Net.Sockets.NetworkStream.InternalSocket.get() Line 136
System.dll!System.Net.PooledStream.CancelPendingIoAndCloseIfSafe(bool closeWithTimeout, int timeout) Line 596
System.dll!System.Net.PooledStream.Close(int timeout) Line 682
System.dll!System.Net.Connection.AbortOrDisassociate(System.Net.HttpWebRequest request, System.Net.WebException webException) Line 2859
System.dll!System.Net.HttpWebRequest.Abort(System.Exception exception, int abortState) Line 3173
System.dll!System.Net.HttpWebRequest.Abort() Line 3133
System.dll!System.Net.WebSockets.ClientWebSocket.AbortRequest(object obj) Line 366
mscorlib.dll!System.Threading.CancellationCallbackInfo.ExecutionContextCallback(object obj) Line 1031
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) Line 980
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) Line 928
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) Line 917
mscorlib.dll!System.Threading.CancellationCallbackInfo.ExecuteCallback() Line 1021
mscorlib.dll!System.Threading.CancellationTokenSource.CancellationCallbackCoreWork(System.Threading.CancellationCallbackCoreWorkArguments args) Line 863
mscorlib.dll!System.Threading.CancellationTokenSource.ExecuteCallbackHandlers(bool throwOnFirstException) Line 808
mscorlib.dll!System.Threading.CancellationTokenSource.NotifyCancellation(bool throwOnFirstException) Line 743
System.dll!System.Net.WebSockets.ClientWebSocket.Dispose() Line 377
System.dll!System.Net.WebSockets.ClientWebSocket.Abort() Line 360
Microsoft.AspNetCore.Http.Connections.Client.dll!Microsoft.AspNetCore.Http.Connections.Client.Internal.WebSocketsTransport.ProcessSocketAsync(System.Net.WebSockets.WebSocket socket) Line 220
And the next:
> System.dll!System.Net.Sockets.NetworkStream.InternalSocket.get() Line 136 C#
System.dll!System.Net.PooledStream.CancelPendingIoAndCloseIfSafe(bool closeWithTimeout, int timeout) Line 596 C#
System.dll!System.Net.PooledStream.Close(int timeout) Line 682 C#
System.dll!System.Net.Connection.AbortSocket(bool isAbortState) Line 2901 C#
System.dll!System.Net.ConnectStream.CloseInternal(bool internalCall, bool aborting) Line 2707 C#
System.dll!System.Net.ConnectStream.System.Net.ICloseEx.CloseEx(System.Net.CloseExState closeState) Line 2426 C#
System.dll!System.Net.HttpWebResponse.Abort() Line 479 C#
System.dll!System.Net.HttpWebRequest.SetResponse(System.Exception E) Line 3968 C#
System.dll!System.Net.HttpWebRequest.SetAndOrProcessResponse(object responseOrException) Line 3661 C#
System.dll!System.Net.ConnectionReturnResult.SetResponses(System.Net.ConnectionReturnResult returnResult) Line 205 C#
System.dll!System.Net.Connection.AbortOrDisassociate(System.Net.HttpWebRequest request, System.Net.WebException webException) Line 2873 C#
System.dll!System.Net.HttpWebRequest.Abort(System.Exception exception, int abortState) Line 3173 C#
System.dll!System.Net.HttpWebRequest.Abort() Line 3133 C#
System.Net.Http.dll!System.Net.Http.HttpClientHandler.OnCancel(object state) Line 1210 C#
mscorlib.dll!System.Threading.CancellationCallbackInfo.ExecutionContextCallback(object obj) Line 1031 C#
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) Line 980 C#
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) Line 928 C#
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) Line 917 C#
mscorlib.dll!System.Threading.CancellationCallbackInfo.ExecuteCallback() Line 1021 C#
mscorlib.dll!System.Threading.CancellationTokenSource.CancellationCallbackCoreWork(System.Threading.CancellationCallbackCoreWorkArguments args) Line 863 C#
mscorlib.dll!System.Threading.CancellationTokenSource.ExecuteCallbackHandlers(bool throwOnFirstException) Line 808 C#
mscorlib.dll!System.Threading.CancellationTokenSource.NotifyCancellation(bool throwOnFirstException) Line 743 C#
mscorlib.dll!System.Threading.CancellationTokenSource.LinkedTokenCancelDelegate(object source) Line 91 C#
mscorlib.dll!System.Threading.CancellationCallbackInfo.ExecutionContextCallback(object obj) Line 1031 C#
mscorlib.dll!System.Threading.CancellationCallbackInfo.ExecuteCallback() Line 1021 C#
mscorlib.dll!System.Threading.CancellationTokenSource.CancellationCallbackCoreWork(System.Threading.CancellationCallbackCoreWorkArguments args) Line 863 C#
mscorlib.dll!System.Threading.CancellationTokenSource.ExecuteCallbackHandlers(bool throwOnFirstException) Line 808 C#
mscorlib.dll!System.Threading.CancellationTokenSource.NotifyCancellation(bool throwOnFirstException) Line 743 C#
mscorlib.dll!System.Threading.CancellationTokenSource.TimerCallbackLogic(object obj) Line 534 C#
mscorlib.dll!System.Threading.TimerQueueTimer.CallCallbackInContext(object state) Line 730 C#
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) Line 980 C#
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx) Line 928 C#
mscorlib.dll!System.Threading.TimerQueueTimer.CallCallback() Line 713 C#
mscorlib.dll!System.Threading.TimerQueueTimer.Fire() Line 670 C#
mscorlib.dll!System.Threading.TimerQueue.FireQueuedTimerCompletion(object state) Line 444 C#
mscorlib.dll!System.Threading.QueueUserWorkItemCallback.System.Threading.IThreadPoolWorkItem.ExecuteWorkItem() Line 1252 C#
mscorlib.dll!System.Threading.ThreadPoolWorkQueue.Dispatch() Line 820 C#
mscorlib.dll!System.Threading._ThreadPoolWaitCallback.PerformWaitCallback() Line 1161 C#
The server is running in Visual Studio at http://localhost:5008/tubahub. When running the server in IIS on the same machine we also get a SocketException when calling StopAsync due to a timeout. Perhaps that is related.

using MySqlBulkCopy of MySqlConnector to upload a datatable into MySql. Exception thrown

Well i switched to MySqlConnector in order to be able to use its functionality to upload a Datatable into a MySql Table.
I'm following their simple example for this function as follow :
DataTable datatable = Globals.ds.Tables["Song"];
using (var connection = new MySqlConnection(Globals.connString + "AllowLoadLocalInfile = true;"))
{
await connection.OpenAsync();
var bulkCopy = new MySqlBulkCopy(connection);
bulkCopy.DestinationTableName = "Song2";
await bulkCopy.WriteToServerAsync(datatable);
}
The Song2 table is a empty copy of the Song table that contains 17000 lines and that is feeding the Datatable through a adapter.fill() method (i even did a .FillSchema() before).
When i run this, i have 2 exceptions :
The first one (the innerexception) is Failed to read the result set. while the second one is "System.Reflection.TargetInvocationException"
Anyone can help ?
Thanks in advance
EDIT :
Here is the call stack
System.Reflection.TargetInvocationException
HResult=0x80131604
Message=Une exception a été levée par la cible d'un appel.
Source=mscorlib
StackTrace:
at System.RuntimeMethodHandle.InvokeMethod(Object target, Object[] arguments, Signature sig, Boolean constructor)
at System.Reflection.RuntimeMethodInfo.UnsafeInvokeInternal(Object obj, Object[] parameters, Object[] arguments)
at System.Delegate.DynamicInvokeImpl(Object[] args)
at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)
at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)
at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)
at System.Windows.Forms.Control.InvokeMarshaledCallbacks()
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at MP3_manager.Program.Main() in C:\Users\Utilisateur\source\repos\MP3-manager\MP3-manager\Program.cs:line 23
This exception was originally thrown at this call stack:
MySqlConnector.Utilities.SocketAwaitable.GetResult() in SocketAwaitable.cs
MySqlConnector.Protocol.Serialization.SocketByteHandler.DoWriteBytesAsync(System.ReadOnlyMemory<byte>) in SocketByteHandler.cs
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task)
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task)
System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable<TResult>.ConfiguredValueTaskAwaiter.GetResult()
MySqlConnector.Protocol.Serialization.ProtocolUtility.WritePacketAsync.__WritePacketAsyncAwaited|8_0(System.Threading.Tasks.ValueTask<int>, byte[]) in ProtocolUtility.cs
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(System.Threading.Tasks.Task)
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(System.Threading.Tasks.Task)
System.Runtime.CompilerServices.ConfiguredValueTaskAwaitable<TResult>.ConfiguredValueTaskAwaiter.GetResult()
MySqlConnector.Core.ServerSession.SendReplyAsyncAwaited(System.Threading.Tasks.ValueTask<int>) in ServerSession.cs
...
[Call Stack Truncated]
Inner Exception 1:
MySqlException: Failed to read the result set.
Inner Exception 2:
SocketException: Une connexion existante a dû être fermée par l’hôte distant

Entity Framework Core async/wait deadlock when using "one-to-many" relationship and WebApi

I'm encountering a deadlock when using asynchronous implementation of an EF Core provider.
Say I have the following models:
public class Player
{
public string PlayerId { get; set;}
public string Name { get; set;}
public List<Game> Games { get; set;}
}
public class Game
{
public string GameId { get; set; }
public string PlayerId { get; set; }
public Player Player { get; set;}
}
Now I want to run the following query:
ctx.Players
.Include(p => p.Games)
.Where(p => p.PlayerId == "123")
.Select(p => new {
PlayerId = p.PlayerId,
Games = p.Games.ToList()
}).ToListAsync();
When I run this code via Console Application/XUnit test it works as excepted...
But when I run it via ASP.Net WebApi it get into deadlock and never ends...
I used ConfigureAwait(false) all the way down in order to prevent these kind of situations but it seems that the problematic code is down underneath.
I think that it might be under the System.Interactive.Async library which EFCore use - To be more specific it is under: https://github.com/Reactive-Extensions/Rx.NET/blob/develop/Ix.NET/Source/System.Interactive.Async/ToAsyncEnumerable.cs#L72
there is a call to "Result" which actually blocks the execution thread.
Does anyone encountered this behavior, maybe there is some workaround?
Notice that if I don't load the "Games" entities then everything also works fine...
Edit: Added the StackTrace: (See the call to ToEnumerable)
Not Flagged 21672 5 Worker Thread grpc 0 (cq 0) System.Interactive.Async.dll!System.Linq.AsyncEnumerable.**ToEnumerable_**<NG.Admin.BL.DAL.Entities.Game> Normal
mscorlib.dll!System.Threading.Monitor.Wait(object obj, int millisecondsTimeout, bool exitContext)
mscorlib.dll!System.Threading.Monitor.Wait(object obj, int millisecondsTimeout)
mscorlib.dll!System.Threading.ManualResetEventSlim.Wait(int millisecondsTimeout, System.Threading.CancellationToken cancellationToken)
mscorlib.dll!System.Threading.Tasks.Task.SpinThenBlockingWait(int millisecondsTimeout, System.Threading.CancellationToken cancellationToken)
mscorlib.dll!System.Threading.Tasks.Task.InternalWait(int millisecondsTimeout, System.Threading.CancellationToken cancellationToken)
mscorlib.dll!System.Threading.Tasks.Task.GetResultCore(bool waitCompletionNotification)
mscorlib.dll!System.Threading.Tasks.Task.Result.get()
System.Interactive.Async.dll!System.Linq.AsyncEnumerable.ToEnumerable_<NG.Admin.BL.DAL.Entities.Game>(System.Collections.Generic.IAsyncEnumerable<NG.Admin.BL.DAL.Entities.Game> source)
mscorlib.dll!System.Collections.Generic.List<NG.Admin.BL.DAL.Entities.Game>.List(System.Collections.Generic.IEnumerable<NG.Admin.BL.DAL.Entities.Game> collection)
System.Core.dll!System.Linq.Enumerable.ToList<NG.Admin.BL.DAL.Entities.Game>(System.Collections.Generic.IEnumerable<NG.Admin.BL.DAL.Entities.Game> source)
[Lightweight Function]
Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.SelectAsyncEnumerable<Microsoft.EntityFrameworkCore.Storage.ValueBuffer, NG.Admin.BL.Api.Player>.SelectAsyncEnumerator.MoveNext(System.Threading.CancellationToken cancellationToken)
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.InvokeMoveNext(object stateMachine)
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.Run()
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.OutputAsyncCausalityEvents.AnonymousMethod__0()
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.ContinuationWrapper.Invoke()
mscorlib.dll!System.Runtime.CompilerServices.TaskAwaiter.OutputWaitEtwEvents.AnonymousMethod__0()
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.ContinuationWrapper.Invoke()
mscorlib.dll!System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Action action, bool allowInlining, ref System.Threading.Tasks.Task currentTask)
mscorlib.dll!System.Threading.Tasks.Task.FinishContinuations()
mscorlib.dll!System.Threading.Tasks.Task.FinishStageThree()
mscorlib.dll!System.Threading.Tasks.Task.TrySetResult(bool result)
mscorlib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder.SetResult(bool result)
NG.Data.Spanner.EF.dll!NG.Data.Spanner.EF.Query.Internal.SpannerAsyncQueryingEnumerable<Microsoft.EntityFrameworkCore.Storage.ValueBuffer>.SpannerAsyncEnumerator.MoveNext(System.Threading.CancellationToken cancellationToken) Line 55
[Resuming Async Method]
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.InvokeMoveNext(object stateMachine)
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.Run()
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.OutputAsyncCausalityEvents.AnonymousMethod__0()
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.ContinuationWrapper.Invoke()
mscorlib.dll!System.Runtime.CompilerServices.TaskAwaiter.OutputWaitEtwEvents.AnonymousMethod__0()
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.ContinuationWrapper.Invoke()
mscorlib.dll!System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Action action, bool allowInlining, ref System.Threading.Tasks.Task currentTask)
mscorlib.dll!System.Threading.Tasks.Task.FinishContinuations()
mscorlib.dll!System.Threading.Tasks.Task.FinishStageThree()
mscorlib.dll!System.Threading.Tasks.Task.TrySetResult(bool result)
mscorlib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder.SetResult(bool result)
Microsoft.EntityFrameworkCore.dll!Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.SelectAsyncEnumerable<Microsoft.EntityFrameworkCore.Storage.ValueBuffer, Microsoft.EntityFrameworkCore.Storage.ValueBuffer>.SelectAsyncEnumerator.MoveNext(System.Threading.CancellationToken cancellationToken)
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.InvokeMoveNext(object stateMachine)
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.Run()
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.OutputAsyncCausalityEvents.AnonymousMethod__0()
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.ContinuationWrapper.Invoke()
mscorlib.dll!System.Runtime.CompilerServices.TaskAwaiter.OutputWaitEtwEvents.AnonymousMethod__0()
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.ContinuationWrapper.Invoke()
mscorlib.dll!System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Action action, bool allowInlining, ref System.Threading.Tasks.Task currentTask)
mscorlib.dll!System.Threading.Tasks.Task.FinishContinuations()
mscorlib.dll!System.Threading.Tasks.Task.FinishStageThree()
mscorlib.dll!System.Threading.Tasks.Task.TrySetResult(bool result)
mscorlib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder.SetResult(bool result)
Microsoft.EntityFrameworkCore.Relational.dll!Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable.AsyncEnumerator.MoveNext(System.Threading.CancellationToken cancellationToken)
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.InvokeMoveNext(object stateMachine)
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.Run()
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.OutputAsyncCausalityEvents.AnonymousMethod__0()
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.ContinuationWrapper.Invoke()
mscorlib.dll!System.Runtime.CompilerServices.TaskAwaiter.OutputWaitEtwEvents.AnonymousMethod__0()
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.ContinuationWrapper.Invoke()
mscorlib.dll!System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Action action, bool allowInlining, ref System.Threading.Tasks.Task currentTask)
mscorlib.dll!System.Threading.Tasks.Task.FinishContinuations()
mscorlib.dll!System.Threading.Tasks.Task.FinishStageThree()
mscorlib.dll!System.Threading.Tasks.Task.TrySetResult(bool result)
mscorlib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder.SetResult(bool result)
Microsoft.EntityFrameworkCore.Relational.dll!Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable.AsyncEnumerator.BufferlessMoveNext(bool buffer, System.Threading.CancellationToken cancellationToken)
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.InvokeMoveNext(object stateMachine)
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.Run()
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.OutputAsyncCausalityEvents.AnonymousMethod__0()
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.ContinuationWrapper.Invoke()
mscorlib.dll!System.Runtime.CompilerServices.TaskAwaiter.OutputWaitEtwEvents.AnonymousMethod__0()
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.ContinuationWrapper.Invoke()
mscorlib.dll!System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Action action, bool allowInlining, ref System.Threading.Tasks.Task currentTask)
mscorlib.dll!System.Threading.Tasks.Task.FinishContinuations()
mscorlib.dll!System.Threading.Tasks.Task.FinishStageThree()
mscorlib.dll!System.Threading.Tasks.Task<System.__Canon>.TrySetResult(System.__Canon result)
mscorlib.dll!System.Threading.Tasks.TaskCompletionSource<System.__Canon>.TrySetResult(System.__Canon result)
Microsoft.EntityFrameworkCore.Relational.dll!System.Threading.Tasks.TaskExtensions.Cast.AnonymousMethod__0(System.Threading.Tasks.Task t)
mscorlib.dll!System.Threading.Tasks.ContinuationTaskFromResultTask.InnerInvoke()
mscorlib.dll!System.Threading.Tasks.Task.Execute()
mscorlib.dll!System.Threading.Tasks.Task.ExecutionContextCallback(object obj)
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Threading.Tasks.Task.ExecuteWithThreadLocal(ref System.Threading.Tasks.Task currentTaskSlot)
mscorlib.dll!System.Threading.Tasks.Task.ExecuteEntry(bool bPreventDoubleExecution)
mscorlib.dll!System.Threading.Tasks.ThreadPoolTaskScheduler.TryExecuteTaskInline(System.Threading.Tasks.Task task, bool taskWasPreviouslyQueued)
mscorlib.dll!System.Threading.Tasks.TaskScheduler.TryRunInline(System.Threading.Tasks.Task task, bool taskWasPreviouslyQueued)
mscorlib.dll!System.Threading.Tasks.TaskContinuation.InlineIfPossibleOrElseQueue(System.Threading.Tasks.Task task, bool needsProtection)
mscorlib.dll!System.Threading.Tasks.StandardTaskContinuation.Run(System.Threading.Tasks.Task completedTask, bool bCanInlineContinuationTask)
mscorlib.dll!System.Threading.Tasks.Task.FinishContinuations()
mscorlib.dll!System.Threading.Tasks.Task.FinishStageThree()
mscorlib.dll!System.Threading.Tasks.Task<System.__Canon>.TrySetResult(System.__Canon result)
mscorlib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder.SetResult(object result)
NG.Data.Spanner.EF.dll!NG.Data.Spanner.EF.Storage.Internal.SpannerRelationalCommand.ExecuteAsync(Microsoft.EntityFrameworkCore.Storage.IRelationalConnection connection, string executeMethod, System.Collections.Generic.IReadOnlyDictionary<string, object> parameterValues, bool closeConnection, System.Threading.CancellationToken cancellationToken) Line 41
[Resuming Async Method]
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.InvokeMoveNext(object stateMachine)
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.Run()
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.OutputAsyncCausalityEvents.AnonymousMethod__0()
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.ContinuationWrapper.Invoke()
mscorlib.dll!System.Runtime.CompilerServices.TaskAwaiter.OutputWaitEtwEvents.AnonymousMethod__0()
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.ContinuationWrapper.Invoke()
mscorlib.dll!System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Action action, bool allowInlining, ref System.Threading.Tasks.Task currentTask)
mscorlib.dll!System.Threading.Tasks.Task.FinishContinuations()
mscorlib.dll!System.Threading.Tasks.Task.FinishStageThree()
mscorlib.dll!System.Threading.Tasks.Task<System.__Canon>.TrySetResult(System.__Canon result)
mscorlib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder.SetResult(object result)
NG.Data.Spanner.EF.dll!NG.Data.Spanner.EF.Storage.Internal.SpannerRelationalCommand.ExecuteAsync(NG.Data.Spanner.EF.Storage.Internal.IOBehavior ioBehavior, Microsoft.EntityFrameworkCore.Storage.IRelationalConnection connection, string executeMethod, System.Collections.Generic.IReadOnlyDictionary<string, object> parameterValues, bool closeConnection, System.Threading.CancellationToken cancellationToken) Line 128
[Resuming Async Method]
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.InvokeMoveNext(object stateMachine)
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.Run()
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.OutputAsyncCausalityEvents.AnonymousMethod__0()
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.ContinuationWrapper.Invoke()
mscorlib.dll!System.Runtime.CompilerServices.TaskAwaiter.OutputWaitEtwEvents.AnonymousMethod__0()
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.ContinuationWrapper.Invoke()
mscorlib.dll!System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Action action, bool allowInlining, ref System.Threading.Tasks.Task currentTask)
mscorlib.dll!System.Threading.Tasks.Task.FinishContinuations()
mscorlib.dll!System.Threading.Tasks.Task.FinishStageThree()
mscorlib.dll!System.Threading.Tasks.Task<System.__Canon>.TrySetResult(System.__Canon result)
mscorlib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder<System.Data.Common.DbDataReader>.SetResult(System.Data.Common.DbDataReader result)
NG.Data.Spanner.dll!NG.Data.Spanner.SpannerCommand.ExecuteDbDataReaderAsync(System.Data.CommandBehavior behavior, System.Threading.CancellationToken cancellationToken) Line 67
[Resuming Async Method]
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.InvokeMoveNext(object stateMachine)
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.Run()
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.OutputAsyncCausalityEvents.AnonymousMethod__0()
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.ContinuationWrapper.Invoke()
mscorlib.dll!System.Runtime.CompilerServices.TaskAwaiter.OutputWaitEtwEvents.AnonymousMethod__0()
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.ContinuationWrapper.Invoke()
mscorlib.dll!System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Action action, bool allowInlining, ref System.Threading.Tasks.Task currentTask)
mscorlib.dll!System.Threading.Tasks.Task.FinishContinuations()
mscorlib.dll!System.Threading.Tasks.Task.FinishStageThree()
mscorlib.dll!System.Threading.Tasks.Task<System.__Canon>.TrySetResult(System.__Canon result)
mscorlib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder<Google.Cloud.Spanner.V1.ResultSet>.SetResult(Google.Cloud.Spanner.V1.ResultSet result)
NG.Data.Spanner.dll!NG.Data.Spanner.SpannerConnection.RunQuery(string commandText) Line 118
[Resuming Async Method]
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.InvokeMoveNext(object stateMachine)
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.Run()
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.OutputAsyncCausalityEvents.AnonymousMethod__0()
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.ContinuationWrapper.Invoke()
mscorlib.dll!System.Runtime.CompilerServices.TaskAwaiter.OutputWaitEtwEvents.AnonymousMethod__0()
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.ContinuationWrapper.Invoke()
mscorlib.dll!System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Action action, bool allowInlining, ref System.Threading.Tasks.Task currentTask)
mscorlib.dll!System.Threading.Tasks.Task.FinishContinuations()
mscorlib.dll!System.Threading.Tasks.Task.FinishStageThree()
mscorlib.dll!System.Threading.Tasks.Task<System.__Canon>.TrySetResult(System.__Canon result)
mscorlib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder<Google.Cloud.Spanner.V1.ResultSet>.SetResult(Google.Cloud.Spanner.V1.ResultSet result)
Google.Api.Gax.Grpc.dll!Google.Api.Gax.Grpc.ApiCallRetryExtensions.WithRetry.AnonymousMethod__0(Google.Cloud.Spanner.V1.ExecuteSqlRequest request, Google.Api.Gax.Grpc.CallSettings callSettings)
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.InvokeMoveNext(object stateMachine)
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.MoveNextRunner.Run()
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.OutputAsyncCausalityEvents.AnonymousMethod__0()
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.ContinuationWrapper.Invoke()
mscorlib.dll!System.Runtime.CompilerServices.TaskAwaiter.OutputWaitEtwEvents.AnonymousMethod__0()
mscorlib.dll!System.Runtime.CompilerServices.AsyncMethodBuilderCore.ContinuationWrapper.Invoke()
mscorlib.dll!System.Threading.Tasks.AwaitTaskContinuation.RunOrScheduleAction(System.Action action, bool allowInlining, ref System.Threading.Tasks.Task currentTask)
mscorlib.dll!System.Threading.Tasks.Task.FinishContinuations()
mscorlib.dll!System.Threading.Tasks.Task.FinishStageThree()
mscorlib.dll!System.Threading.Tasks.Task<System.__Canon>.TrySetResult(System.__Canon result)
mscorlib.dll!System.Threading.Tasks.TaskCompletionSource<System.__Canon>.TrySetResult(System.__Canon result)
mscorlib.dll!System.Threading.Tasks.TaskCompletionSource<System.__Canon>.SetResult(System.__Canon result)
Grpc.Core.dll!Grpc.Core.Internal.AsyncCall<Google.Cloud.Spanner.V1.ExecuteSqlRequest, Google.Cloud.Spanner.V1.ResultSet>.HandleUnaryResponse(bool success, Grpc.Core.Internal.ClientSideStatus receivedStatus, byte[] receivedMessage, Grpc.Core.Metadata responseHeaders)
Grpc.Core.dll!Grpc.Core.Internal.CallSafeHandle.StartUnary.AnonymousMethod__0(bool success, Grpc.Core.Internal.BatchContextSafeHandle context)
Grpc.Core.dll!Grpc.Core.Internal.CompletionRegistry.HandleBatchCompletion(bool success, Grpc.Core.Internal.BatchContextSafeHandle ctx, Grpc.Core.Internal.BatchCompletionDelegate callback)
Grpc.Core.dll!Grpc.Core.Internal.CompletionRegistry.RegisterBatchCompletion.AnonymousMethod__0(bool success)
Grpc.Core.dll!Grpc.Core.Internal.GrpcThreadPool.RunHandlerLoop(Grpc.Core.Internal.CompletionQueueSafeHandle cq, Grpc.Core.Profiling.IProfiler optionalProfiler)
Grpc.Core.dll!Grpc.Core.Internal.GrpcThreadPool.CreateAndStartThread.AnonymousMethod__0()
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart_Context(object state)
mscorlib.dll!System.Threading.ExecutionContext.RunInternal(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state, bool preserveSyncCtx)
mscorlib.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state)
mscorlib.dll!System.Threading.ThreadHelper.ThreadStart()
So after a very long debugging I found the problem and did some work around to solve it. I'll try to explain:
I'm building EF Core provider to the Google Spanner db. For this I'm using Google Spanner DotNet API - although it is not yet public and very unmature API (actually it is just auto-generated code).
This API uses the gRPC API and what I saw is that on the first async call with this API, the gRPC thread (The gRPC has its own thread pool implementation) is used and will be used for the next execution calls - since the continuations will run only on this thread from the gRPC thread pool.
EFCore calls the ToEnumerable for the "Games" list which use the System.Interactive.Async lib. On the the ToEnumerable implementation it actually calls "Result" which blocks the execution thread and waits for the result. Because the gRPC thread is used and wait for the execution to be finished we have a deadlock...
My workaround was to use the following code:
GrpcEnvironment.SetCompletionQueueCount(1);
This way the continuations is allowed to run on other threads from the gRPC thread pool.
This is only a workaround and should be used. However it help solving the issue in the meantime...
The real solution should probably be in the EFCore usage of the System.Interactive.Async lib, in regarding to the ToEnumerable... (It should be pure async).
Edit:
If it might be relevant to anyone:
Just released an EF Core provider for the Google Spanner db:
https://github.com/NoGame/NG.Data.Spanner
Your issue is that you are attempting to materialize Games synchronously and the projected object asynchronously. Note the following:
.Select(p => new {
PlayerId = p.PlayerId,
Games = p.Games.ToList() // sync over async is blocking
}).ToListAsync();
What you would expect to work (in regular EF for instance) is:
.Select(p => new {
PlayerId = p.PlayerId,
Games = p.Games // let the provider handle the enumeration materialization
}).ToListAsync();
But according to your comments that doesn't actually return the games on the anonymous projected object. The issue, it seems then, is that EF Core seems to not let you get away with naive assignment (you should update the question to indicate that). I wonder if this isn't related to how .Include() works in EF Core. I am not familiar enough with the core version of EF to offer much help beyond telling you the "cause," but essentially the issue is that you are waiting to get an enumerator from a source that is waiting to materialize based on that enumeration.

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

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

Categories