Repository navigation
Description
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
The BlazorWebAppMovies sample (presumably 9.0 too) throws an exception System.InvalidOperationException in the Pages/MoviewPages/Index.razor component if the titleFilter search box is changed in quick succession.
The error is due to multiple input events running on different threads on the same circuit passing the same context to the QuickGrid, which runs queries asynchronously while EF Core does not support it.
cc: @guardrex dotnet/blazor-samples#480
Expected Behavior
The grid should handle quick typing in the search box. It should handle any number of input events generated by a single user for that matter.
Steps To Reproduce
- Initialize the database with a large number of movie records to introduce some load and delay.
for (int i = 0; i < 50000; i++)
{
context.Movie.Add(
new Movie
{
Title = $"Movie #{i:00000}",
ReleaseDate = new DateOnly(Random.Shared.Next(1970, 2025), Random.Shared.Next(1, 13), Random.Shared.Next(1, 29)),
Genre = "Test",
Price = Convert.ToDecimal(Random.Shared.Next(1, 5)),
Rating = "NR"
}
);
}- Go to the 'Movies' index page.
- Quickly type some text in the search box.
- Observe an exception.
Exceptions (if any)
fail: Microsoft.EntityFrameworkCore.Query[10100]
An exception occurred while iterating over the results of a query for context type 'BlazorWebAppMovies.Data.BlazorWebAppMoviesContext'.
System.InvalidOperationException: A second operation was started on this context instance before a previous operation completed. This is usually caused by different threads concurrently using the same instance of DbContext. For more information on how to avoid threading issues with DbContext, see https://go.microsoft.com/fwlink/?linkid=2097913.
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
System.InvalidOperationException: A second operation was started on this context instance before a previous operation completed. This is usually caused by different threads concurrently using the same instance of DbContext. For more information on how to avoid threading issues with DbContext, see https://go.microsoft.com/fwlink/?linkid=2097913.
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
warn: Microsoft.AspNetCore.Components.Server.Circuits.RemoteRenderer[100]
Unhandled exception rendering component: A second operation was started on this context instance before a previous operation completed. This is usually caused by different threads concurrently using the same instance of DbContext. For more information on how to avoid threading issues with DbContext, see https://go.microsoft.com/fwlink/?linkid=2097913.
System.InvalidOperationException: A second operation was started on this context instance before a previous operation completed. This is usually caused by different threads concurrently using the same instance of DbContext. For more information on how to avoid threading issues with DbContext, see https://go.microsoft.com/fwlink/?linkid=2097913.
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.SingleAsync[TSource](IAsyncEnumerable`1 asyncEnumerable, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.SingleAsync[TSource](IAsyncEnumerable`1 asyncEnumerable, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Components.QuickGrid.QuickGrid`1.ResolveItemsRequestAsync(GridItemsProviderRequest`1 request)
at Microsoft.AspNetCore.Components.QuickGrid.QuickGrid`1.RefreshDataCoreAsync()
at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
fail: Microsoft.AspNetCore.Components.Server.Circuits.CircuitHost[111]
Unhandled exception in circuit 'T40BpmO-meEOigVitZBZSUulErKT01iXsm_-RWKhVVI'.
System.InvalidOperationException: A second operation was started on this context instance before a previous operation completed. This is usually caused by different threads concurrently using the same instance of DbContext. For more information on how to avoid threading issues with DbContext, see https://go.microsoft.com/fwlink/?linkid=2097913.
at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()
at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.SingleAsync[TSource](IAsyncEnumerable`1 asyncEnumerable, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.SingleAsync[TSource](IAsyncEnumerable`1 asyncEnumerable, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Components.QuickGrid.QuickGrid`1.ResolveItemsRequestAsync(GridItemsProviderRequest`1 request)
at Microsoft.AspNetCore.Components.QuickGrid.QuickGrid`1.RefreshDataCoreAsync()
at Microsoft.AspNetCore.Components.ComponentBase.CallStateHasChangedOnAsyncCompletion(Task task)
warn: Microsoft.AspNetCore.Components.Server.Circuits.RemoteRenderer[100]
Unhandled exception rendering component: Encountered unsupported frame type during diffing: None
System.NotImplementedException: Encountered unsupported frame type during diffing: None
at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.AppendDiffEntriesForFramesWithSameSequence(DiffContext& diffContext, Int32 oldFrameIndex, Int32 newFrameIndex)
at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.AppendDiffEntriesForRange(DiffContext& diffContext, Int32 oldStartIndex, Int32 oldEndIndexExcl, Int32 newStartIndex, Int32 newEndIndexExcl)
at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.AppendDiffEntriesForFramesWithSameSequence(DiffContext& diffContext, Int32 oldFrameIndex, Int32 newFrameIndex)
at Microsoft.AspNetCore.Components.RenderTree.RenderTreeDiffBuilder.AppendDiffEntriesForRange(DiffContext& diffContext, Int32 oldStartIndex, Int32 oldEndIndexExcl, Int32 newStartIndex, Int32 newEndIndexExcl)
at Microsoft.AspNetCore.Components.Rendering.ComponentState.RenderIntoBatch(RenderBatchBuilder batchBuilder, RenderFragment renderFragment, Exception& renderFragmentException)
at Microsoft.AspNetCore.Components.RenderTree.Renderer.ProcessRenderQueue()
.NET Version
9.0.200
Anything else?
There are several workarounds, including materializing the query and not using IQueryable, or updating the search on a button click, perhaps force rendering of all components on the same threads. But I think it's a fundamental issue between the Blazor, the EF Core, and the QuickGrid that makes this sample broken.
Activity
guardrex commentedon Mar 3, 2025
Thanks @MagistratasHK.
@javiercn ... The code in question is here ...
https://github.com/dotnet/blazor-samples/blob/main/9.0/BlazorWebAppMovies/Components/Pages/MoviePages/Index.razor
I'll update it if we accidentally created a code smell 💩👃 there.
javiercn commentedon Mar 4, 2025
@MagistratasHK thanks for contacting us.
Improvements in this area are tracked by #58716
The correct pattern to use is described on the issue. Essentially you need to use the cancellation token to prevent concurrent queries from happening (a new request should trigger cancellation of the previous one).
Rombicon commentedon Mar 13, 2025
@javiercn Cancelation token moves the exceptions further down. When the token is set, it throws Micrsofot.Data.SqlClient.SqlException wrapped in System.InvalidOperationException and System.Threading.Tasks.TaskCanceledException. I'm not sure if swallowing these exceptions is a way to go. I've posted more information in the open issue #58716 .