Closed Shtong closed 9 months ago
Entity Framework Core does not support multiple parallel operations being run on the same DbContext instance
ABP will create a new database context instead of using the same one
The unit tests fails because the SQLite drivers used by the tests are very sensitive to concurrency problems
Maybe related to : https://github.com/abpframework/abp/issues/19065
Yes, this seems to be the same issue. Closing as duplicate
Is there an existing issue for this?
Description
There is a concurrency problem in the code used to initialize the database for some of the official ABP modules. This problem essentially creates concurrent database operations on the same DbContext instance, which is not a scenario officially supported by EF Core, as the DbContext class is not thread safe.
In practice, I haven't seen any side effect of this problem in production scenarios. However, we have been encountering issues caused by this problem on several projects when running unit tests containing large amounts of data seeding, or when the offending modules have a lot of data to initialize. The unit tests fails because the SQLite drivers used by the tests are very sensitive to concurrency problems, more so than other drivers used in production environments.
The offending modules are :
The root of the issue is the use of
Thread.Run(...)
to start the database initialization sequences. BecauseThread.Run
will queue the provided method on the thread pool, the thread pool may decide to execute them all at once on different threads, or during some other data operation taking place on the main thread.Reproduction Steps
Since this is a race condition problem, it cannot be reprodured reliably. To improve your chances of reproducing it, create a project that inserts a lot of data during the data seeding (executed on the main thread), and/or during the database initialization of the Setting, Permission or Features modules. The objective is to spend more time doing database work in the initialization phase of the tests.
After that, just running any unit test is enough to try and replicate, as the crash will happen during the initialization phase of ABP.
Expected behavior
Unit tests starts every time without errors
Actual behavior
Unit tests will randomly throw exceptions during initialization (I am currently working on a project where an exception happens once every 10-15 runs). Here is an example of such an exception:
Regression?
No response
Known Workarounds
If you do not need the data initialization done by the Settings/Features/Permissions modules during testing, you can disable their data initialization, which will prevent the separate threads from being created in the impacted test module (usually any module referencing your Domain module):
Version
8.0.4
User Interface
MVC
Database Provider
EF Core (Default)
Tiered or separate authentication server
Tiered
Operation System
Windows (Default)
Other information
No response