-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Randomly getting InvalidOperationException: The connection was not closed #3900
Comments
I hope this gets more attention. Our CI and smoke tests fail every now and then because of this problem. I'm attaching a detailed log. Notice how the connection was closed before iterating the results of the last command. Also notice that there are 2 queries there but the connection was only opened once for the first query. Although this might be a little different than what I first reported, I am almost certain that they're caused by the same problem.
|
Hey, So looking at the original code, you are ending up with two async operations running against the same context at the same time. This is not supported as a DbContext instance is not thread safe. We have #3291 currently being worked on to provide a clear exception message when you do this. The best rule of thumb to apply is to always await an async operation. Applying that rule, you should await the call to Here are some more details about what is causing the exception. This line of code in GetPets is kicking off the first async database operation:
Since the async call is not awaited, the code continues one and then the following line kicks of the second database operation.
Since count is a pretty quick operation, it would be sporadic because sometimes the Count operation will complete before the second operation kicks off. |
BTW rather than |
Yes, that's what I thought. I know that this has been a limitation of the previous versions of EF. I assumed this is no longer the case in EF7. Thanks for clearing that. Was using |
@dealdiane this shouldn't be causing you any issues, it's not awaiting the call to |
Just to be clear... using |
@rowanmiller Thanks. We'll stick with using If I always On another note, if we can have |
@dealdiane yep, just await the |
Seems to happen every now and then for the first few queries after launching an ASPNET5 website. Refreshing the page after crashing
almost alwayssometimesrarely fixes it and I never had an instance where it crashed again after that. I'm using SQL2012. Could this be some sort of race condition?I'll try my best to replicate the code I have that causes this exception but it's hard to give repro steps because this doesn't happen every time. It does happen about 40% of the time after the website is restarted though.
Now when I call
GetPets
usingthis happens about half of the time. Interestingly, when I change it to:
I haven't had it crash even once. Could be purely coincidental though..
Stack trace:
This might be related to #3887
The text was updated successfully, but these errors were encountered: