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

Deadlock when concurrently deleting disjoint graphs with two levels of cascade #15180

Closed
divega opened this issue Mar 27, 2019 · 2 comments · Fixed by #25874
Closed

Deadlock when concurrently deleting disjoint graphs with two levels of cascade #15180

divega opened this issue Mar 27, 2019 · 2 comments · Fixed by #25874
Labels
area-save-changes closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported punted-for-3.0 type-bug
Milestone

Comments

@divega
Copy link
Contributor

divega commented Mar 27, 2019

Cross-posting from dotnet/ef6#723 (comment) since the behavior repros for both EF6 and EF Core.

Note that this may be a duplicate of #14371. The models look very similar, however the repro in that case:

  • Was alternatively inserting and deleting data, while on this one it only deletes
  • Would deadlock in every other request, while on this one it deadlocks every time
using System;
using System.Linq;
using System.Collections.Generic;
using Microsoft.EntityFrameworkCore;
using System.Threading.Tasks;

namespace EF6Issue723
{
    class Program
    {
        static void Main(string[] args)
        {

            using (Context ctx = new Context())
            {
                ctx.Database.EnsureDeleted();
                ctx.Database.EnsureCreated();
                List<City> cities = new List<City>() { new City() { Name = "New York" } };
                for (int h = 1; h <= 50; h++)
                {
                    string code = "A";
                    if (h % 2 == 0)
                        code = "B";
                    House house = new House() { Number = h, Code = code };
                    cities[0].Houses.Add(house);
                    for (int i = 0; i <= 100; i++)
                        house.Residents.Add(new Person() { Firstname = "A", Lastname = "B" });
                }

                ctx.Cities.AddRange(cities);
                ctx.SaveChanges();
            }

            string[] to = new string[] { "A", "B" };
            Parallel.ForEach(to, code =>
            {
                DeleteHouses(code);
            });
        }

        private static void DeleteHouses(string code)
        {
            using (var ctx = new Context())
            {
                var todel = ctx.Houses.Where(d => d.Code == code);
                if (todel != null)
                {
                    ctx.Houses.RemoveRange(todel);
                    ctx.SaveChanges();
                }
            }
        }


    }

    public class Context : DbContext
    {
        public DbSet<City> Cities { get; set; }
        public DbSet<House> Houses { get; set; }
        public DbSet<Person> Persons { get; set; }

        public Context() : base()
        {
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(@"Server=(localdb)\mssqllocaldb;Database=test;Trusted_Connection=Yes;ConnectRetryCount=0");

        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            modelBuilder.Entity<City>().HasMany(c => c.Houses).WithOne(c => c.City).IsRequired().OnDelete(DeleteBehavior.Cascade);
            modelBuilder.Entity<House>().HasMany(p => p.Residents).WithOne(p => p.House).IsRequired().OnDelete(DeleteBehavior.Cascade);
        }
    }

    public class City
    {
        public Guid Id { get; set; }
        public string Name { get; set; }
        public virtual ICollection<House> Houses { get; set; }

        public City()
        {
            Id = Guid.NewGuid();
            Houses = new List<House>();
        }
    }

    public class House
    {
        public Guid Id { get; set; }
        public int Number { get; set; }
        public string Code { get; set; }
        public virtual City City { get; set; }
        public virtual ICollection<Person> Residents { get; set; }

        public House()
        {
            Id = Guid.NewGuid();
            Residents = new List<Person>();
        }
    }


    public class Person
    {
        public Guid Id { get; set; }
        public string Firstname { get; set; }
        public string Lastname { get; set; }

        public virtual House House { get; set; }

        public Person()
        {
            Id = Guid.NewGuid();
        }
    }
}
@ajcvickers
Copy link
Contributor

Triage: putting this in 3.0 for now to investigate:

  • Can we do something better in Core?
  • What workarounds/different patterns exist that will not hit this issue?

@ajcvickers ajcvickers modified the milestones: 3.0.0, Backlog Jun 28, 2019
@ajcvickers ajcvickers modified the milestones: Backlog, 6.0.0 Nov 2, 2020
@AndriySvyryd AndriySvyryd removed their assignment Sep 4, 2021
@AndriySvyryd AndriySvyryd added the closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. label Sep 4, 2021
AndriySvyryd added a commit that referenced this issue Sep 4, 2021
Always sort deletes before updates and updates before inserts for the same table.

Fixes #14371
Fixes #15180
Fixes #25228
@AndriySvyryd
Copy link
Member

After 05725c9 this now gets retried and passes successfully.

AndriySvyryd added a commit that referenced this issue Sep 4, 2021
AndriySvyryd added a commit that referenced this issue Sep 4, 2021
AndriySvyryd added a commit that referenced this issue Sep 7, 2021
@ajcvickers ajcvickers modified the milestones: 6.0.0, 6.0.0-rc2 Sep 8, 2021
@ajcvickers ajcvickers removed this from the 6.0.0-rc2 milestone Nov 8, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-save-changes closed-fixed The issue has been fixed and is/will be included in the release indicated by the issue milestone. customer-reported punted-for-3.0 type-bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants