Repository navigation
Closed
Copy link
Milestone
Description
We have a set of tests that use different contexts with following setup:
- all tests run sequentially (no parallel execution)
- there are several database contexts used by tests
- if context is not initialized yet during current test run, it will ensure test database is ready using
context.Database.EnsureDeleted();
context.Database.EnsureCreated();This works fine for different db providers (we test sqlite, sqlserver, mysql and npgsql) and versions (3.1, 6, 8). But in v9 now it fails to create database for Npgsql on second call to snippet above. Basically only 1 context being initialized per test run, second initialization fails with:
System.InvalidOperationException
HResult=0x80131509
Message=An exception has been raised that is likely due to a transient failure.
Source=Npgsql.EntityFrameworkCore.PostgreSQL
StackTrace:
at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlExecutionStrategy.Execute[TState,TResult](TState state, Func`3 operation, Func`3 verifySucceeded) in Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal\NpgsqlExecutionStrategy.cs:line 28
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQuery(IReadOnlyList`1 migrationCommands, IRelationalConnection connection, MigrationExecutionState executionState, Boolean commitTransaction, Nullable`1 isolationLevel)
at Microsoft.EntityFrameworkCore.Migrations.Internal.MigrationCommandExecutor.ExecuteNonQuery(IEnumerable`1 migrationCommands, IRelationalConnection connection)
at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlDatabaseCreator.CreateTables() in Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal\NpgsqlDatabaseCreator.cs:line 210
at Microsoft.EntityFrameworkCore.Storage.RelationalDatabaseCreator.EnsureCreated()
at Microsoft.EntityFrameworkCore.Infrastructure.DatabaseFacade.EnsureCreated()
at LinqToDB.EntityFrameworkCore.Tests.ContextTestBase`1.InitializeDatabase(TContext context, String provider, String connectionString) in C:\GitHub\linq2db\Tests\EntityFrameworkCore\ContextTestBase.cs:line 119
at LinqToDB.EntityFrameworkCore.Tests.ContextTestBase`1.CreateContext(String provider, Func`2 optionsSetter, Func`2 optionsBuilderSetter) in C:\GitHub\linq2db\Tests\EntityFrameworkCore\ContextTestBase.cs:line 159
at LinqToDB.EntityFrameworkCore.Tests.InterceptorTests.TestEfCoreSideOfComboInterceptor(String provider) in C:\GitHub\linq2db\Tests\EntityFrameworkCore\Tests\InterceptorTests.cs:line 109
at System.Reflection.MethodBaseInvoker.InvokeWithOneArg(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
This exception was originally thrown at this call stack:
System.Net.Sockets.NetworkStream.Read(byte[], int, int)
Inner Exception 1:
NpgsqlException: Exception while reading from stream
Inner Exception 2:
IOException: Unable to read data from the transport connection: An established connection was aborted by the software in your host machine..
Inner Exception 3:
SocketException: An established connection was aborted by the software in your host machine.
Currently looking into providing isolated repro code
Activity
MaceWindu commentedon Oct 23, 2024
Reproduced.
Note use of
UseNodaTimeoption. Without it it works fine.This is actually a workaround for another issue in v8 we hit and I didn't reported yet: only one of test contexts need nodatime integration, but starting from v8 release it
UseNodaTimewas ignored if not called on firstUseNpgsqlcall in tests (3.1 and 6 versions respected this call for specific context)MaceWindu commentedon Oct 24, 2024
BTW, running from linux gives a bit different error:
[-]Migrations behavior change in 9.0.0[/-][+]Pooled connections broken by EnsureDeleted() stay in the pool if using a data source[/+]roji commentedon Oct 27, 2024
@MaceWindu thanks for the repro, I can see the error happening (I wish everyone submitted such minimal repros...).
What's going on is a bit complex...
Starting with EFCore.PG 9, UseNodaTime() (and MapEnum(), and some other things) cause the provider to build and use an NpgsqlDataSource internally; this is done so that EFCore.PG can be in control of the lower-level Npgsql configuration, and so that the EF-level UseNodaTime() can also configure NodaTime at the Npgsql data source level. Solves the bad user experience up to 9.0, where users had to configure NodaTime twice - once in Npgsql, once in EFCore.PG.
Now, when you perform EnsureDeleted, EFCore.PG calls
NpgsqlConnection.ClearAllPools()(code); this immediately clears all "legacy" pools of all idle connections (legacy = without a data source), since they're all broken. However, if a data source is in use, ClearAllPools() does not take effect on it (this is by design). Unfortunately, we're missing an API to clear an NpgsqlDataSource: opened npgsql/npgsql#5902 on the Npgsql side to discuss this.Slightly simplified repro with logging
Call NpgsqlDataSource.Clear when doing EnsureDeleted
Call NpgsqlDataSource.Clear when doing EnsureDeleted (npgsql#3337)
Call NpgsqlDataSource.Clear when doing EnsureDeleted (npgsql#3337)
MaceWindu commentedon Nov 23, 2025
@roji we see this error again with v10. rc2 was fine.
MaceWindu commentedon Nov 23, 2025
Ok, while error is the same, repro case is different. I will create new repro and open new issue