Skip to content

Issues with user locking around connection open/close #1127

Open

Description

@roji

Currently, connection open and close operations aren't protected via the user lock. This means that if someone tries to close (or use) a connection while it's still connecting, undefined stuff will occur instead of a clean, predictable connection.

Do this once the open/close logic is cleaned up as part of 3.2's distributed transaction redo.

Once this issue is done, the EFCore provider's Throws_on_concurrent_query_first can be unskipped.

Activity

  1. added this to the 3.2 milestone on May 25, 2016
  2. modified the milestones: , 3.2 on Dec 18, 2016
  3. modified the milestones: 4.0, on Mar 14, 2018
  4. Procee commented on Jun 29, 2019

    @Procee

    Is there some kind of work around for this?

  5. 1 remaining item

  6. modified the milestones: 4.1, 5.0 on Aug 26, 2019
  7. engineer-andrew commented on Apr 20, 2020

    @engineer-andrew

    @roji I get what you're saying, but I'm not sure how to do it. I have multiple REST endpoints that could potentially access the same data at the same time. Perhaps my overall setup is wrong, but it was definitely working when my app was running on dotnet core 2.1. I'm trying to migrate to 3.1 (2.1 > 2.2 > 3.0 > 3.1) and as soon as I updated the reference to Npgsql.EntityFrameworkCore.PostgreSQL to a 3+ version I started running into this error.

    Where and how do we check to make sure we're not concurrently accessing a connection? I know this is specific to my application, but I also have seen several other issues link to this one and no one seems to be detailing a method for implementing this work around and I'm honestly just completely perplexed.

  8. roji commented on Apr 20, 2020

    @roji
    MemberAuthor

    @engineer-andrew I'm missing a lot of detail, but it sounds like you may be keeping a DbContext on a singleton service. DbContext instances, like connections, are not meant to be shared and can only be used once at a time. Assuming your service is a singleton (since multiple REST endpoints potentially access it at the same time), create a new DbContext each time you want to do a database operation (or more precisely, a unit of work). In general, don't store the instance in some member variable where others can use it - keep it in the stack for your own private us, and dispose it once you don't need anymore.

    Hope that helps, if not you're going to have to provide a bit more context on what your application does.

  9. engineer-andrew commented on Apr 21, 2020

    @engineer-andrew

    @roji Honestly, I posted my question mostly out of frustration with my problem and was not really expecting an answer. I definitely wasn't expecting an answer within 4 hours. And, of course, your answer sent me in the right direction. I'm using a Unit of Work pattern with a new DbContext created for each request (which is the unit of work). However, in one of my requests I was retrieving data from two repositories asynchronously. I somehow got so focused on the idea that it was the multiple requests coming into the API simultaneously that I was unable to see what was really causing the problem.

    Thank you so much for getting back to me so quickly. I appreciate the help on this question and the overall work you're doing on this tool.

  10. roji commented on Apr 21, 2020

    @roji
    MemberAuthor

    Glad I could help - FWIW this is a very common issue, and lots of people end up accidentally reusing the same DbContext concurrently.

  11. modified the milestones: 5.0, 6.0.0 on Sep 17, 2020
  12. modified the milestones: 6.0.0, 7.0.0 on Oct 3, 2021
  13. modified the milestones: 7.0.0, Backlog on Sep 11, 2022
  14. added theissue type on Jun 14, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions