question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. Itย collects links to all the places you might be looking at while hunting down a tough bug.

And, if youโ€™re still stuck at the end, weโ€™re happy to hop on a call to see how we can help out.

Threading issues with DbContext

See original GitHub issue

I am having threading issues with DbContext + Lamar. Seeing a few errors, but this one is the most common A second operation started on this context before a previous operation completed. If I switch over to Microsoft DI, I do not have the same issue.

I created a reproducible sample here: https://github.com/jsheetzati/lamarExample. Currently, the project is setup to use Lamar DI, but I also have comments to switch the project over to Microsoft DI.

In order to run the project, please add a DB connection string to Context and SecondContext. I attempted to recreate the problem using the InMemoryProvider but was not having luck with that provider. If you use SQL Server, create a sample table in the database with the following definition:

create table Book
(
    Id int identity(1,1),
    Title nvarchar(30),
    Author nvarchar(30)
)

I used artillery to hit the API method with a bunch of requests simultaneously. npm install -g artillery artillery quick -n 5 --count 10 http://localhost:5000/Book/InsertAndReturn

Please let me know if you need any further information to reproduce the issue. Fairly confident at this point that Lamar is introducing a similar bug in one of our applications. There very well could be something wrong with our Lamar configuration as well, but I am not sure what that exactly is.

Thanks for all the hard work making this project. Looking forward to make the switch from StructureMap to Lamar ๐Ÿ˜ƒ

Edit: I am not sure if this is related to https://github.com/JasperFx/lamar/issues/173, but that is what initially prompted me to dig into our issue to see if it was something similar.

Package Information ASP.NET Core 3.0 Lamar 3.2.0 Lamar.Microsoft.DependencyInjection 4.0.0

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:2
  • Comments:13 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
jeremydmillercommented, Jan 2, 2020

FINALLY, thereโ€™s some optimism on this one. Maybe, maybe the fix for #215 knocks this one out as well, but weโ€™ll see.

1reaction
jasselincommented, Nov 25, 2019

@rofr I did try enabling MARS which fixed some problems with read operations. I was still having some odd behavior when doing writes.

I am pretty sure this is the same issue since the ISession hash code was the same between two threads.

Switching back to StructureMap made the problem disappear completely, even with MARS disabled.

Read more comments on GitHub >

github_iconTop Results From Across the Web

๐‡๐จ๐ฐ ๐ญ๐จ ๐š๐ฏ๐จ๐ข๐ ๐ƒ๐›๐‚๐จ๐ง๐ญ๐ž๐ฑ๐ญ ๐“๐ก๐ซ๐ž๐š๐๐ข๐ง๐  ๐ˆ๐ฌ๐ฌ๐ฎ๐ž๐ฌ ...
There are few ways that can help you to avoid threading issues in EF. Separate DbContext instance; Thread-safe DbContext wrapper; Use ofย ...
Read more >
Entity Framework Core: A second operation started on this ...
This is usually caused by different threads using the same instance of DbContext. For more information on how to avoid threading issues with...
Read more >
[BUG] A second operation started on this context before ...
This is usually caused by different threads using the same instance of DbContext. For more information on how to avoid threading issues with...
Read more >
Remember kids: DbContext is not threadsafe
Bad things can happen when using EntityFramework Core in a multithreading environment. Special care is needed. Don't be foolish,ย ...
Read more >
How to Solve Threading Issues with Entity Framework and ...
This is usually caused by different threads concurrently using the same instance of DbContext." This error typically occurs when multipleย ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found