Skip to content

Request Batching Database Context Scoping #8233

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
tnc1997 opened this issue Apr 8, 2025 · 2 comments
Closed

Request Batching Database Context Scoping #8233

tnc1997 opened this issue Apr 8, 2025 · 2 comments

Comments

@tnc1997
Copy link
Contributor

tnc1997 commented Apr 8, 2025

Product

Hot Chocolate

Version

15.1.3

Link to minimal reproduction

https://github.com/tnc1997/hot-chocolate-request-batching

Steps to reproduce

git checkout v15
docker compose up --build --detach
curl http://localhost:5135

What is expected?

The blogs and posts should be added to the database without any exceptions being thrown.

What is actually happening?

The blogs and posts are not being completely added to the database (some are failing).

Relevant log output

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.","stackTrace":"   at Microsoft.EntityFrameworkCore.Infrastructure.Internal.ConcurrencyDetector.EnterCriticalSection()\n   at Microsoft.EntityFrameworkCore.Query.Internal.SingleQueryingEnumerable`1.AsyncEnumerator.MoveNextAsync()\n   at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.SingleAsync[TSource](IAsyncEnumerable`1 asyncEnumerable, CancellationToken cancellationToken)\n   at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.SingleAsync[TSource](IAsyncEnumerable`1 asyncEnumerable, CancellationToken cancellationToken)\n   at Server.Mutation.AddPostAsync(BloggingContext context, ILogger`1 logger, Guid id, String title, Guid blogId, CancellationToken cancellationToken) in /src/Server/Types.cs:line 86\n   at HotChocolate.Resolvers.Expressions.ExpressionHelper.AwaitTaskHelper[T](Task`1 task)\n   at HotChocolate.Types.Helpers.FieldMiddlewareCompiler.<>c__DisplayClass9_0.<<CreateResolverMiddleware>b__0>d.MoveNext()\n--- End of stack trace from previous location ---\n   at HotChocolate.Types.MutationConventionTypeInterceptor.<>c__DisplayClass16_0.<<ApplyResultMiddleware>b__1>d.MoveNext()\n--- End of stack trace from previous location ---\n   at HotChocolate.Types.MutationConventionMiddleware.InvokeAsync(IMiddlewareContext context)\n   at HotChocolate.Types.ErrorMiddleware.InvokeAsync(IMiddlewareContext context)\n   at HotChocolate.Execution.Processing.Tasks.ResolverTask.ExecuteResolverPipelineAsync(CancellationToken cancellationToken)\n   at HotChocolate.Execution.Processing.Tasks.ResolverTask.TryExecuteAsync(CancellationToken cancellationToken)

Additional context

The v13 branch works as expected however the v14 branch and the v15 branch have issues with multiple operations being performed concurrently using the same database context when batching requests.

@michaelstaib
Copy link
Member

That is by design .... Batching will execute operations in parallel and share between these requests a DataLoader context ...
Each async field has its own DI context that can work in isolation. We have a series about the new Data APIs here:
https://www.youtube.com/@chillicreamtv

@tnc1997
Copy link
Contributor Author

tnc1997 commented Apr 8, 2025

Each async field has its own DI context that can work in isolation.

Excuse my naivety but does this isolation mechanism apply to both queries and mutations?

Was this a change introduced from version 13 to 14?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants