Async Include with group join causes deadlock
Test which causes the hang
Include_collection_on_inner_group_join_clause_with_filter
line Assert.Equal(6, customers.SelectMany(c => c.g).Count());
Stack trace:
System.Private.CoreLib.ni.dll!System.Threading.ManualResetEventSlim.Wait(int millisecondsTimeout, System.Threading.CancellationToken cancellationToken) Unknown
System.Private.CoreLib.ni.dll!System.Threading.Tasks.Task.SpinThenBlockingWait(int millisecondsTimeout, System.Threading.CancellationToken cancellationToken) Unknown
System.Private.CoreLib.ni.dll!System.Threading.Tasks.Task.InternalWait(int millisecondsTimeout, System.Threading.CancellationToken cancellationToken) Unknown
System.Private.CoreLib.ni.dll!System.Threading.Tasks.Task<bool>.GetResultCore(bool waitCompletionNotification) Unknown
System.Interactive.Async.dll!System.Linq.AsyncEnumerable.ToEnumerable_<Microsoft.EntityFrameworkCore.Specification.Tests.TestModels.Northwind.Order>(System.Collections.Generic.IAsyncEnumerable<Microsoft.EntityFrameworkCore.Specification.Tests.TestModels.Northwind.Order> source) Unknown
System.Linq.dll!System.Linq.Enumerable.Count<Microsoft.EntityFrameworkCore.Specification.Tests.TestModels.Northwind.Order>(System.Collections.Generic.IEnumerable<Microsoft.EntityFrameworkCore.Specification.Tests.TestModels.Northwind.Order> source) Unknown
System.Linq.dll!System.Linq.Enumerable.SelectManySingleSelectorIterator<<>f__AnonymousType176<Microsoft.EntityFrameworkCore.Specification.Tests.TestModels.Northwind.Customer, System.Collections.Generic.IEnumerable<Microsoft.EntityFrameworkCore.Specification.Tests.TestModels.Northwind.Order>>, Microsoft.EntityFrameworkCore.Specification.Tests.TestModels.Northwind.Order>.GetCount(bool onlyIfCheap) Unknown
System.Linq.dll!System.Linq.Enumerable.Count<Microsoft.EntityFrameworkCore.Specification.Tests.TestModels.Northwind.Order>(System.Collections.Generic.IEnumerable<Microsoft.EntityFrameworkCore.Specification.Tests.TestModels.Northwind.Order> source) Unknown
> Microsoft.EntityFrameworkCore.Specification.Tests.dll!Microsoft.EntityFrameworkCore.Specification.Tests.IncludeAsyncTestBase<Microsoft.EntityFrameworkCore.SqlServer.FunctionalTests.NorthwindQuerySqlServerFixture>.Include_collection_on_inner_group_join_clause_with_filter() Line 317 C#
[Resuming Async Method]
System.Private.CoreLib.ni.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) Unknown
xunit.execution.dotnet.dll!Xunit.Sdk.AsyncTestSyncContext.Post.AnonymousMethod__1(object _) Unknown
xunit.execution.dotnet.dll!Xunit.Sdk.MaxConcurrencySyncContext.RunOnSyncContext(System.Threading.SendOrPostCallback callback, object state) Unknown
System.Private.CoreLib.ni.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) Unknown
xunit.execution.dotnet.dll!Xunit.Sdk.MaxConcurrencySyncContext.WorkerThreadProc() Unknown
xunit.execution.dotnet.dll!Xunit.Sdk.XunitWorkerThread.QueueUserWorkItem.AnonymousMethod__5_0(object _) Unknown
System.Private.CoreLib.ni.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) Unknown
System.Private.CoreLib.ni.dll!System.Threading.Tasks.Task.ExecuteWithThreadLocal(ref System.Threading.Tasks.Task currentTaskSlot) Unknown
System.Private.CoreLib.ni.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) Unknown
[Async Call]
xunit.execution.dotnet.dll!Xunit.Sdk.TestInvoker<Xunit.Sdk.IXunitTestCase>.InvokeTestMethodAsync.AnonymousMethod__1() Unknown
[Async Call]
xunit.execution.dotnet.dll!Xunit.Sdk.ExecutionTimer.AggregateAsync(System.Func<System.Threading.Tasks.Task> asyncAction) Unknown
[Async Call]
xunit.core.dll!Xunit.Sdk.ExceptionAggregator.RunAsync(System.Func<System.Threading.Tasks.Task> code) Unknown
[Async Call]
xunit.execution.dotnet.dll!Xunit.Sdk.TestInvoker<Xunit.Sdk.IXunitTestCase>.InvokeTestMethodAsync(object testClassInstance) Unknown
[Async Call]
xunit.execution.dotnet.dll!Xunit.Sdk.TestInvoker<Xunit.Sdk.IXunitTestCase>.RunAsync.AnonymousMethod__47_0() Unknown
[Async Call]
xunit.core.dll!Xunit.Sdk.ExceptionAggregator.RunAsync<decimal>(System.Func<System.Threading.Tasks.Task<decimal>> code) Unknown
[Async Call]
xunit.execution.dotnet.dll!Xunit.Sdk.XunitTestRunner.InvokeTestAsync(Xunit.Sdk.ExceptionAggregator aggregator) Unknown
[Async Call]
xunit.core.dll!Xunit.Sdk.ExceptionAggregator.RunAsync<System.Tuple<decimal, string>>(System.Func<System.Threading.Tasks.Task<System.Tuple<decimal, string>>> code) Unknown
[Async Call]
xunit.execution.dotnet.dll!Xunit.Sdk.TestRunner<Xunit.Sdk.IXunitTestCase>.RunAsync() Unknown
[Async Call]
xunit.execution.dotnet.dll!Xunit.Sdk.TestCaseRunner<Xunit.Sdk.IXunitTestCase>.RunAsync() Unknown
[Async Call]
xunit.execution.dotnet.dll!Xunit.Sdk.TestMethodRunner<Xunit.Sdk.IXunitTestCase>.RunTestCasesAsync() Unknown
[Async Call]
xunit.execution.dotnet.dll!Xunit.Sdk.TestMethodRunner<Xunit.Sdk.IXunitTestCase>.RunAsync() Unknown
[Async Call]
xunit.execution.dotnet.dll!Xunit.Sdk.TestClassRunner<Xunit.Sdk.IXunitTestCase>.RunTestMethodsAsync() Unknown
[Async Call]
xunit.execution.dotnet.dll!Xunit.Sdk.TestClassRunner<Xunit.Sdk.IXunitTestCase>.RunAsync() Unknown
[Async Call]
xunit.execution.dotnet.dll!Xunit.Sdk.TestCollectionRunner<Xunit.Sdk.IXunitTestCase>.RunTestClassesAsync() Unknown
[Async Call]
xunit.execution.dotnet.dll!Xunit.Sdk.TestCollectionRunner<Xunit.Sdk.IXunitTestCase>.RunAsync() Unknown
Concurrently running test, (perhaps can be any)
Select_correlated_subquery_ordered
StackTrace:
System.Private.CoreLib.ni.dll!System.Threading.ManualResetEventSlim.Wait(int millisecondsTimeout, System.Threading.CancellationToken cancellationToken) Unknown
System.Private.CoreLib.ni.dll!System.Threading.Tasks.Task.SpinThenBlockingWait(int millisecondsTimeout, System.Threading.CancellationToken cancellationToken) Unknown
System.Private.CoreLib.ni.dll!System.Threading.Tasks.Task.InternalWait(int millisecondsTimeout, System.Threading.CancellationToken cancellationToken) Unknown
System.Private.CoreLib.ni.dll!System.Threading.Tasks.Task<bool>.GetResultCore(bool waitCompletionNotification) Unknown
System.Interactive.Async.dll!System.Linq.AsyncEnumerable.ToEnumerable_<Microsoft.EntityFrameworkCore.Specification.Tests.TestModels.Northwind.Order>(System.Collections.Generic.IAsyncEnumerable<Microsoft.EntityFrameworkCore.Specification.Tests.TestModels.Northwind.Order> source) Unknown
System.Linq.dll!System.Collections.Generic.LargeArrayBuilder<Microsoft.EntityFrameworkCore.Specification.Tests.TestModels.Northwind.Order>.AddRange(System.Collections.Generic.IEnumerable<Microsoft.EntityFrameworkCore.Specification.Tests.TestModels.Northwind.Order> items) Unknown
System.Linq.dll!System.Collections.Generic.SparseArrayBuilder<Microsoft.EntityFrameworkCore.Specification.Tests.TestModels.Northwind.Order>.AddRange(System.Collections.Generic.IEnumerable<Microsoft.EntityFrameworkCore.Specification.Tests.TestModels.Northwind.Order> items) Unknown
System.Linq.dll!System.Collections.Generic.SparseArrayBuilder<Microsoft.EntityFrameworkCore.Specification.Tests.TestModels.Northwind.Order>.ReserveOrAdd(System.Collections.Generic.IEnumerable<Microsoft.EntityFrameworkCore.Specification.Tests.TestModels.Northwind.Order> items) Unknown
System.Linq.dll!System.Linq.Enumerable.SelectManySingleSelectorIterator<object, Microsoft.EntityFrameworkCore.Specification.Tests.TestModels.Northwind.Order>.ToArray() Unknown
System.Linq.dll!System.Linq.Buffer<Microsoft.EntityFrameworkCore.Specification.Tests.TestModels.Northwind.Order>.Buffer(System.Collections.Generic.IEnumerable<Microsoft.EntityFrameworkCore.Specification.Tests.TestModels.Northwind.Order> source) Unknown
System.Linq.dll!System.Linq.OrderedEnumerable<Microsoft.EntityFrameworkCore.Specification.Tests.TestModels.Northwind.Order>.GetEnumerator() Unknown
xunit.assert.dll!Xunit.Sdk.AssertEqualityComparer<System.__Canon>.CheckIfEnumerablesAreEqual(System.__Canon x, System.__Canon y) Unknown
xunit.assert.dll!Xunit.Sdk.AssertEqualityComparer<System.Linq.IOrderedEnumerable<Microsoft.EntityFrameworkCore.Specification.Tests.TestModels.Northwind.Order>>.Equals(System.Linq.IOrderedEnumerable<Microsoft.EntityFrameworkCore.Specification.Tests.TestModels.Northwind.Order> x, System.Linq.IOrderedEnumerable<Microsoft.EntityFrameworkCore.Specification.Tests.TestModels.Northwind.Order> y) Unknown
xunit.assert.dll!Xunit.Assert.Equal<System.Linq.IOrderedEnumerable<Microsoft.EntityFrameworkCore.Specification.Tests.TestModels.Northwind.Order>>(System.Linq.IOrderedEnumerable<Microsoft.EntityFrameworkCore.Specification.Tests.TestModels.Northwind.Order> expected, System.Linq.IOrderedEnumerable<Microsoft.EntityFrameworkCore.Specification.Tests.TestModels.Northwind.Order> actual, System.Collections.Generic.IEqualityComparer<System.Linq.IOrderedEnumerable<Microsoft.EntityFrameworkCore.Specification.Tests.TestModels.Northwind.Order>> comparer) Unknown
Microsoft.EntityFrameworkCore.Specification.Tests.dll!Microsoft.EntityFrameworkCore.Specification.Tests.AsyncQueryTestBase<Microsoft.EntityFrameworkCore.SqlServer.FunctionalTests.NorthwindQuerySqlServerFixture>.Select_correlated_subquery_ordered.AnonymousMethod__141_1(System.Collections.Generic.IList<object> l2oResults, System.Collections.Generic.IList<object> efResults) Line 1528 C#
Microsoft.EntityFrameworkCore.Specification.Tests.dll!Microsoft.EntityFrameworkCore.Specification.Tests.TestHelpers.AssertResults<object>(System.Collections.Generic.IList<object> expected, System.Collections.Generic.IList<object> actual, bool assertOrder, System.Action<System.Collections.Generic.IList<object>, System.Collections.Generic.IList<object>> asserter) Line 363 C#
Microsoft.EntityFrameworkCore.Specification.Tests.dll!Microsoft.EntityFrameworkCore.Specification.Tests.AsyncQueryTestBase<Microsoft.EntityFrameworkCore.SqlServer.FunctionalTests.NorthwindQuerySqlServerFixture>.AssertQuery<Microsoft.EntityFrameworkCore.Specification.Tests.TestModels.Northwind.Customer, Microsoft.EntityFrameworkCore.Specification.Tests.TestModels.Northwind.Order>(System.Func<System.Linq.IQueryable<Microsoft.EntityFrameworkCore.Specification.Tests.TestModels.Northwind.Customer>, System.Linq.IQueryable<Microsoft.EntityFrameworkCore.Specification.Tests.TestModels.Northwind.Order>, System.Linq.IQueryable<object>> query, bool assertOrder, int? entryCount, System.Action<System.Collections.Generic.IList<object>, System.Collections.Generic.IList<object>> asserter) Line 3956 C#
[Resuming Async Method]
System.Private.CoreLib.ni.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) Unknown
xunit.execution.dotnet.dll!Xunit.Sdk.AsyncTestSyncContext.Post.AnonymousMethod__1(object _) Unknown
xunit.execution.dotnet.dll!Xunit.Sdk.MaxConcurrencySyncContext.RunOnSyncContext(System.Threading.SendOrPostCallback callback, object state) Unknown
System.Private.CoreLib.ni.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) Unknown
xunit.execution.dotnet.dll!Xunit.Sdk.MaxConcurrencySyncContext.WorkerThreadProc() Unknown
xunit.execution.dotnet.dll!Xunit.Sdk.XunitWorkerThread.QueueUserWorkItem.AnonymousMethod__5_0(object _) Unknown
System.Private.CoreLib.ni.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) Unknown
System.Private.CoreLib.ni.dll!System.Threading.Tasks.Task.ExecuteWithThreadLocal(ref System.Threading.Tasks.Task currentTaskSlot) Unknown
System.Private.CoreLib.ni.dll!System.Threading.ExecutionContext.Run(System.Threading.ExecutionContext executionContext, System.Threading.ContextCallback callback, object state) Unknown
[Async Call]
> Microsoft.EntityFrameworkCore.Specification.Tests.dll!Microsoft.EntityFrameworkCore.Specification.Tests.AsyncQueryTestBase<Microsoft.EntityFrameworkCore.SqlServer.FunctionalTests.NorthwindQuerySqlServerFixture>.Select_correlated_subquery_ordered() Line 1512 C#
[Async Call]
xunit.execution.dotnet.dll!Xunit.Sdk.TestInvoker<Xunit.Sdk.IXunitTestCase>.InvokeTestMethodAsync.AnonymousMethod__1() Unknown
[Async Call]
xunit.execution.dotnet.dll!Xunit.Sdk.ExecutionTimer.AggregateAsync(System.Func<System.Threading.Tasks.Task> asyncAction) Unknown
[Async Call]
xunit.core.dll!Xunit.Sdk.ExceptionAggregator.RunAsync(System.Func<System.Threading.Tasks.Task> code) Unknown
[Async Call]
xunit.execution.dotnet.dll!Xunit.Sdk.TestInvoker<Xunit.Sdk.IXunitTestCase>.InvokeTestMethodAsync(object testClassInstance) Unknown
[Async Call]
xunit.execution.dotnet.dll!Xunit.Sdk.TestInvoker<Xunit.Sdk.IXunitTestCase>.RunAsync.AnonymousMethod__47_0() Unknown
[Async Call]
xunit.core.dll!Xunit.Sdk.ExceptionAggregator.RunAsync<decimal>(System.Func<System.Threading.Tasks.Task<decimal>> code) Unknown
[Async Call]
xunit.execution.dotnet.dll!Xunit.Sdk.XunitTestRunner.InvokeTestAsync(Xunit.Sdk.ExceptionAggregator aggregator) Unknown
[Async Call]
xunit.core.dll!Xunit.Sdk.ExceptionAggregator.RunAsync<System.Tuple<decimal, string>>(System.Func<System.Threading.Tasks.Task<System.Tuple<decimal, string>>> code) Unknown
[Async Call]
xunit.execution.dotnet.dll!Xunit.Sdk.TestRunner<Xunit.Sdk.IXunitTestCase>.RunAsync() Unknown
[Async Call]
xunit.execution.dotnet.dll!Xunit.Sdk.TestCaseRunner<Xunit.Sdk.IXunitTestCase>.RunAsync() Unknown
[Async Call]
xunit.execution.dotnet.dll!Xunit.Sdk.TestMethodRunner<Xunit.Sdk.IXunitTestCase>.RunTestCasesAsync() Unknown
[Async Call]
xunit.execution.dotnet.dll!Xunit.Sdk.TestMethodRunner<Xunit.Sdk.IXunitTestCase>.RunAsync() Unknown
[Async Call]
xunit.execution.dotnet.dll!Xunit.Sdk.TestClassRunner<Xunit.Sdk.IXunitTestCase>.RunTestMethodsAsync() Unknown
[Async Call]
xunit.execution.dotnet.dll!Xunit.Sdk.TestClassRunner<Xunit.Sdk.IXunitTestCase>.RunAsync() Unknown
[Async Call]
xunit.execution.dotnet.dll!Xunit.Sdk.TestCollectionRunner<Xunit.Sdk.IXunitTestCase>.RunTestClassesAsync() Unknown
[Async Call]
xunit.execution.dotnet.dll!Xunit.Sdk.TestCollectionRunner<Xunit.Sdk.IXunitTestCase>.RunAsync() Unknown
[Async Call]
xunit.execution.dotnet.dll!Xunit.Sdk.XunitTestAssemblyRunner.RunTestCollectionsAsync(Xunit.Sdk.IMessageBus messageBus, System.Threading.CancellationTokenSource cancellationTokenSource) Unknown
[Async Call]
xunit.execution.dotnet.dll!Xunit.Sdk.TestAssemblyRunner<Xunit.Sdk.IXunitTestCase>.RunAsync() Unknown
[Async Call]
xunit.execution.dotnet.dll!Xunit.Sdk.XunitTestFrameworkExecutor.RunTestCases(System.Collections.Generic.IEnumerable<Xunit.Sdk.IXunitTestCase> testCases, Xunit.Abstractions.IMessageSink executionMessageSink, Xunit.Abstractions.ITestFrameworkExecutionOptions executionOptions) Unknown
Relevant code in AsyncEnumerable.ToEnumerable:
public static IEnumerable<TSource> ToEnumerable<TSource>(this IAsyncEnumerable<TSource> source)
{
if (source == null)
throw new ArgumentNullException("source");
return AsyncEnumerable.ToEnumerable_<TSource>(source);
}
private static IEnumerable<TSource> ToEnumerable_<TSource>(IAsyncEnumerable<TSource> source)
{
IAsyncEnumerator<TSource> e = source.GetEnumerator();
try
{
while (e.MoveNext(CancellationToken.None).Result)
yield return e.Current;
}
finally
{
if (e != null)
e.Dispose();
}
e = (IAsyncEnumerator<TSource>) null;
}
It also started appearing around ingesting SDK 2.0.0-preview1-005807 & commit https://github.com/aspnet/EntityFramework/commit/585f221c79e1926261ac5743487a639ca29383f3 Very consistent on AppVeyor (single core machine), Happens more intermittently on 2 core win7 machine. More core/faster machines avoids the issue.
Related issue on xunit : https://github.com/xunit/xunit/issues/864
Promoting this to a product issue. We would really like to remove the call to .Result here. Some options:
- Instead of calling ToEnumerable, buffer the inner results on each async outer iteration.
- Investigate specific cases of this. E.g. GroupJoin groups are usually already buffered. We can omit any ToAsyncEnumerable/ToEnumerable calls in this case.
- Allow AsAsyncEnumerable at end of inner query chain so user can asynchronously iterate the inner sequence.
I'm also encountering this deadlock issue. I think it should be defined as high priority...
@anpete is this the same problem covered by https://github.com/aspnet/EntityFramework/issues/9128? Should we close as dupe or assign both to the 2.1 milestone?
@diego Probably the same but keep this one and assign to 2.1.
Any progress on this? I'd just like to add it affects nested .FirstOrDefault() queries too. I use this technique a fair bit for my models which have optional sub-models.
@Nathan-Ryan This was fixed by https://github.com/aspnet/EntityFrameworkCore/commit/d9077d19967443cbe181e5b238aebca15823b451
@AndriySvyryd Wow that was fast. Your my hero. Do we have to wait for the 2.1 release now?
@Nathan-Ryan It was fixed a week ago, we just forgot to close this issue 😄 The fix should be available in 2.0.1
@AndriySvyryd Awesome, I'll check it out.
@AndriySvyryd Which package source would I need to add to get 2.0.1?
@Nathan-Ryan It would be https://dotnet.myget.org/gallery/aspnetcore-patch, but we haven't published it yet. For now you can try 2.1.0 from https://dotnet.myget.org/gallery/aspnetcore-dev
@AndriySvyryd Thank you very much.