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

With a standard dbContext (Scoped), my context is being shared across requests. #6712

Closed
mgallig opened this issue Oct 7, 2016 · 3 comments

Comments

@mgallig
Copy link

mgallig commented Oct 7, 2016

Steps to reproduce

Add a scoped lifetime datacontext

In my controller it is injected in this way


  private ApplicationDbContext _context;
        private UserManager<ApplicationUser> _userManager;
        public PatchSetsController(ApplicationDbContext context, UserManager<ApplicationUser> mgr)
        {
            _context = context;
            _userManager = mgr;
        }

In a dotnetcore 1.0, EF7 application, add dbcontext in Startup.cs like so:

  services.AddEntityFramework().AddDbContext<ApplicationDbContext>(options =>
                options.UseSqlServer(Configuration.GetConnectionString("DefaultConnection")));

(or, explicitly set to scoped lifetime)

Create a synchronous endpoint that executes a longer (~1s) query, and returns the result.

My controller has a method to run a long-running query and respond with the results as JSON. This calls .ToList() at the end of a LINQ query.

Specifically, my actual query is this:

List<PatchSet> patches  = _context.PatchSets.Include(c => c.Baseline).Include(c => c.Patches).ThenInclude(p => p.Patch).Where(c => c.Baseline.Id == currentPatchSet.Baseline.Id && c.Status == PatchSetStatus.Released).ToList();

Call this method, twice (via AJAX/xhr), with different parameters from the same page at nearly the same time.

Client logs show these requests occur within 1ms of eachother. Debugging shows that the operations occur concurrently.

The issue

The context appears to be shared between requests and causes some internal implementation errors (NPE, index out of range, etc) because both queries are running simultaneously.

If I use a mutex and lock around the query statement, the issue disappears entirely. The concurrent requests should be separate datacontexts, no?

  lock (locker)
            {
      patches  = _context.PatchSets.Include(c => c.Baseline).Include(c => c.Patches).ThenInclude(p => p.Patch).Where(c => c.Baseline.Id == currentPatchSet.Baseline.Id && c.Status == PatchSetStatus.Released).ToList();

            }            

The above code throws no exceptions at any time.

Further technical details

EF Core version: 1.0.0
Operating system: Windows 10
Visual Studio version: VS 2015

Other details about my project setup:

@mgallig
Copy link
Author

mgallig commented Oct 7, 2016

The errors I am getting are a Null Pointer Error, and an Index Out of Range error, these appear randomly but fairly consistently.

Using a lock statement prevents these 100% of the time. These requests are on separate threads and separate XHR requests

@mgallig
Copy link
Author

mgallig commented Oct 7, 2016

This appears to be fixed in 1.0.1, so this issue may be moot. I will report after further testing

@rowanmiller
Copy link
Contributor

This looks like #5456 which was fixed in 1.0.1

@ajcvickers ajcvickers reopened this Oct 16, 2022
@ajcvickers ajcvickers closed this as not planned Won't fix, can't repro, duplicate, stale Oct 16, 2022
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

3 participants