Skip to content
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

Cancelled token does not prevent execution of ExecuteDeleteAsync(token) #31448

Closed
dsgordeev opened this issue Aug 12, 2023 · 5 comments
Closed
Assignees
Labels
area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. community-contribution customer-reported Servicing-approved type-bug
Milestone

Comments

@dsgordeev
Copy link
Contributor

dsgordeev commented Aug 12, 2023

Steps to reproduce

Following csproj and source code can be used to reproduce the issue.
ExecuteDeleteAsync send DELETE request to database even if cancellation already is requested by token.

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.EntityFrameworkCore.Relational" Version="7.0.10" />
    <PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.10" />
  </ItemGroup>
</Project>
public class Item
{
    public DateTimeOffset Date { get; set; }
}

public class ItemsDbContext: DbContext
{
    public ItemsDbContext(DbContextOptions options) : base(options) {}
    public DbSet<Item> Items { get; set; }
    protected override void OnModelCreating(ModelBuilder modelBuilder)
    {
        modelBuilder.Entity<Item>().HasNoKey();
    }
    protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
    {
        optionsBuilder.UseSqlServer("Data Source=(localdb)\\mssqllocaldb;Initial Catalog=Items;Integrated Security=True");
    }
}

var sp = new ServiceCollection()
    .AddDbContext<ItemsDbContext>()
    .BuildServiceProvider().CreateScope().ServiceProvider;
var ctx = sp.GetRequiredService<ItemsDbContext>();

using var cts = new CancellationTokenSource();
cts.Cancel();
count = await ctx.Items.Where(x => x.Date < DateTimeOffset.UtcNow).ExecuteDeleteAsync(cts.Token);
Console.WriteLine(count);

Include provider and version information

EF Core version: 7.0.10
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .NET 7.0
Operating system: Win10
IDE: Visual Studio 2022 17.7

dsgordeev added a commit to dsgordeev/efcore that referenced this issue Aug 12, 2023
   - Affects ExecuteDeleteAsync(token)
   - Affects ExecuteUpdateAsync(token)

Fixes [dotnet#31448](dotnet#31448)
@dsgordeev
Copy link
Contributor Author

dsgordeev commented Aug 12, 2023

A proposed solution is to pass token taken from relational query context to call of IExecutionStrategy.ExecuteAsync(...)

#31449

@dsgordeev dsgordeev changed the title Cancelled token does not prevent execution of ExecuteDeleteAsync(token) [BUG-FIX] Cancelled token does not prevent execution of ExecuteDeleteAsync(token) Sep 1, 2023
@dsgordeev dsgordeev changed the title [BUG-FIX] Cancelled token does not prevent execution of ExecuteDeleteAsync(token) Cancelled token does not prevent execution of ExecuteDeleteAsync(token) Sep 6, 2023
@ajcvickers ajcvickers added this to the Backlog milestone Sep 22, 2023
@ajcvickers ajcvickers modified the milestones: Backlog, 9.0.0 Sep 25, 2023
@ajcvickers ajcvickers added closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. and removed consider-for-current-release labels Sep 25, 2023
@roji roji removed this from the 9.0.0 milestone Nov 3, 2023
@roji
Copy link
Member

roji commented Nov 3, 2023

Clearing milestone to discuss patching 8.0

@ajcvickers ajcvickers reopened this Nov 14, 2023
@dsgordeev

This comment was marked as resolved.

@roji

This comment was marked as resolved.

@dsgordeev

This comment was marked as resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-query closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. community-contribution customer-reported Servicing-approved type-bug
Projects
None yet
Development

No branches or pull requests

3 participants