Retry execution strategy with multiple DbContexts doesn't retry

Open
#19,079 2 comments 0 reactions 1 assignee View on GitHub

@AndriySvyryd is already working on this.

Since Dec 2, 2019.

Assessment

This issue has not been assessed yet.

Description

area-dbcontext area-perf customer-reported

When using execution strategies to retry transient errors we've run into an issue where if one DbContext uses an explicit execution strategy because of a transaction another DbContext won't retry transient errors.

It looks like that's caused by the ExecutionStrategy class using a static AsyncLocal<bool?> to track if it's inside a strategy execution already. Since it's static it's applying to all DbContext rather than just the context the strategy belongs to. That was originally added to fix https://github.com/aspnet/EntityFramework6/issues/16

Steps to reproduce

I tried to simplify what we're actually doing for a small repro in the attached project.

EfConcurrentExecutionStrategy.zip

On startup it runs an execution strategy with a transaction on MainDbContext. While that's running it's using InfrastructureDbContext to write log messages and get the count of log messages. Writing log messages works but reading the log count fails. When the the BackgroundService LoggingWritingService calls LoggingService.WriteLogsAsync the execution strategy's RetriesOnFailure is true but when calling LoggingService.GetLogCountAsync from inside MainDbContext's execution strategy the InfrastructureDbContext's execution strategy's RetriesOnFailure is false.

Further technical details

EF Core version: 3.0.1
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: NET Core 3.0
Operating system: Windows 10
IDE: Visual Studio 2019 16.3.6

Dominant language
C#
Stars
14.8k
Forks
3.4k
Avg merge
2d 5h
Merged PRs (30d)
134

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from dotnet/efcore

All issues in dotnet/efcore

Similar issues

More C# issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.