-
Notifications
You must be signed in to change notification settings - Fork 56
Multiple DbCntexts select query can't work concurrently. #29
Comments
I assure you, there are tests on the sync and async queries. I don't know that the EF Core system is designed to handle multiple concurrent queries this way. Does this work with the SQL providers? The problem may very well likely be in the state manager. I'll definitely take a look at this. It would make things easier for me if you fork the repo and create a simple test using the sample ZooContext and domain that illustrates this problem. Otherwise, it might be a while before I can get to it. |
Hi @crhairr , thank you for your reply. I have forked your repo and committed two test cases in MongoDbContextTests. About the concurrent query issue, I can reproduce it in netcoreapp2.1 and framework4.6.1 is ok. Please try the code. I have to use the sync query in this case due to the async query issue.
` About the async query issue, I can easily reproduce it with the second same query. You can try the code below. After some investigation I fixed it with some adjustments in MongoDbEntityQueryModelVisitor.ReplaceClauseReferences and MongoDbDatabase.CompileAsyncQuery, but I am not sure if it has side affect. The commit is fix async query twice issue.
` |
Ultimately, this error is due to the I'm investigating whether or not there is a way around this. However, this may be a limitation beyond my immediate control. |
I have adjusted concurrent query test. The more records in mongodb, the more time query costs. It seems like that each query pull many records from mongodb. I also tried mongodatabase to query and it costed a constant time. After setting profile level to 2, I find there is no filter or pipeline transfered to mongodb. I think it will get all records from mongodb and filter in the memory. @crhairr, could you please help to investigate this issue first? It blocks my project now. Thank you! Reproduce step:
`
` |
I logged mongodb profiler data when dbContext.Users.FirstOrDefault(u => u.Name == name) executed and there were two operations sent to the mongodb. From the logs, we can see there was no filter or pipeline transfered and nreturned values were 9899(op2) + 101(op1) = 10000 which was just the total number of the records in the mongodb document. `
` |
Hi @crhairr, any progress on this? Could you please tell me where should I look into? I want to resolve the issue ASAP, otherwise I have to re-implement my query API for my project. BTW, in MongoDbDatabase.SaveChangesAsync, Task.WhenAll() missed the parameter tasks. It would cause indefinite waiting for the async processing when high concurrency requests arrived. |
I apologize, between the holidays, work, and family illness over had a hard time getting free time to put into this. I'll set aside some time tonight for it. As for your last comment, I see that pretty clearly. That's a painful omission, and might very well be at the root of a lot of these TPL issues. I'll fix it and run some additional tests tonight, definitely get a build out for at least that part. |
@crhairr, firstly, thank you for your efforts!
` |
If you check out the MongoDB C# driver's GitHub repo, it binds both Enumerable.Where and Queryable.Where: If you are still working with your changes to |
Sorry,I forgot to clairfy that I had reverted my changes about the ReplaceClauseReference and still can reproduce it.
|
I've added a test that uses multiple concurrent queries against a single Context, but it's currently disabled due to a Skip. I've worked out some of the threading issues, but there's still a problem specifically with |
@crhairr, I open a new issue to trace the missing filter in MongoQueryable. missing filter in MongoQueryable |
This is being addressed as part of #32. The old-style async query compiler apparently leaked some thread context that trickled up when a second or third context is created from the same DI container. I had to override the compilation at a different level to effectively limit the scope. However, this change only addresses the problem with async query errors propagating out of previously disposed DbContexts. This will not allow multiple concurrent queries against the same DbContext from multiple threads, which is explicitly disallowed by the base DbContext in EF Core as many of its operations are not guaranteed to be thread-safe. |
Multiple DbContexts select query can't work concurrently. Please see the code below.
BTW, if I use async query, "System.ObjectDisposedException : Cannot access a disposed object." will occur.
`
The text was updated successfully, but these errors were encountered: