Repository navigation
Milestone
Description
Steps to reproduce
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Authors>kae</Authors>
<TargetFramework>net5.0</TargetFramework>
<TreatWarningsAsErrors>true</TreatWarningsAsErrors>
<OutputType>Exe</OutputType>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Npgsql" Version="5.0.0-preview1" />
<PackageReference Include="Dapper" Version="2.0.35" />
</ItemGroup>
</Project>
using System;
using System.Linq;
using System.Threading.Tasks;
using Dapper;
namespace pgTest {
public class Program {
private static readonly string connStr =
"Server=db-host;Port=5432;Database=test_db;User id=test;Password=test";
public static async Task Test(int i) {
try {
using var conn = new Npgsql.NpgsqlConnection(connStr);
await conn.OpenAsync();
using var tx = conn.BeginTransaction(System.Data.IsolationLevel.ReadCommitted);
var rs = await conn.QueryFirstOrDefaultAsync<int>("select 1", transaction: tx);
tx.Commit();
}
catch (Exception e) {
Console.WriteLine(e);
}
}
public static async Task Main() {
try {
await Task.WhenAll(Enumerable.Range(0, 1000).Select(i => Test(i)));
}
catch (Exception e) {
Console.WriteLine(e);
}
}
}
}The issue
System.NotSupportedException: In multiplexing mode, transactions must be started with BeginTransaction
at Npgsql.NpgsqlConnector.ProcessNewTransactionStatus(TransactionStatus newStatus)
at Npgsql.NpgsqlConnector.ParseServerMessage(NpgsqlReadBuffer buf, BackendMessageCode code, Int32 len, Boolean isPrependedMessage)
at Npgsql.NpgsqlConnector.DoReadMessage(Boolean async, DataRowLoadingMode dataRowLoadingMode, Boolean readingNotifications, CancellationToken cancellationToken)
at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming, CancellationToken cancellationToken)
at Npgsql.NpgsqlDataReader.Consume(Boolean async, CancellationToken cancellationToken)
at Npgsql.NpgsqlDataReader.Read(Boolean async, CancellationToken cancellationToken)
at Dapper.SqlMapper.QueryRowAsync[T](IDbConnection cnn, Row row, Type effectiveType, CommandDefinition command) in /_/Dapper/SqlMapper.Async.cs:line 511
at pgTest.Program.Test(Int32 i) in C:\Users\kae\Dump\dnx\pgTest\Program.cs:line 16
Count of exception is floating, so it looks like some race condition bug.
Further technical details
Npgsql version: 5.0.0-preview1
PostgreSQL version: 12.4 on x86_64-pc-linux-gnu
Operating system: Windows 10 (Build 19041.572)
There is another exception at actual system that wasn't reproduced still.
But it was mixed with reproduced. Not sure it has similar nature.
{
"Message": "Exception while reading from stream",
"InnerError": {
"Message": "Timeout during reading attempt"
},
"StackTrace": [
"at Npgsql.NpgsqlConnector.<>c__DisplayClass170_0.<<DoReadMessage>g__ReadMessageLong|0>d.MoveNext()",
"--- End of stack trace from previous location ---",
"at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming, CancellationToken cancellationToken)",
"at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)",
"at Npgsql.NpgsqlCommand.ExecuteNonQuery(Boolean async, CancellationToken cancellationToken)",
"at Dapper.SqlMapper.ExecuteImplAsync(IDbConnection cnn, CommandDefinition command, Object param) in /_/Dapper/SqlMapper.Async.cs:line 687",
"at Service.MessageQueueProcessor.ReportHandler.Execute(DeliveredMessage dm) in C:\\Development\\RabbitMQ\\src\\Cron.Service\\MessageQueueProcessor.cs:line 169"
]
}
Activity
[-]Exception "In multiplexing mode, transactions must be started with BeginTransaction" without Multiplexing[/-][+]Exception "In multiplexing mode, transactions must be started with BeginTransaction" without Multiplexing with 5.0.0-preview1[/+]kae commentedon Oct 20, 2020
Interestingly, when adding "Multiplexing=true" to connection string then for Enumerable.Range(0, 100) it's throwing in 1 out of 5-6 runs
And for Enumerable.Range(0, 1000) it hangs for a minutes and than gets multiple pool exhaustion exceptions like
followed with
and
and
vonzshik commentedon Oct 20, 2020
Hello!
I was able to reproduce the first error. It seems to be because the connector is returned to the pool, and only after that we clear the reference to the connection.
npgsql/src/Npgsql/NpgsqlConnection.cs
Lines 784 to 795 in ec6746f
The second error (Multiplexing with
Enumerable.Range(0, 100)), I was unable to reproduce.The third error seems to be from the multiplex bootstrap, which is in no way, shape or form is threadsafe.
npgsql/src/Npgsql/NpgsqlConnection.cs
Line 318 in ec6746f
What do you say, @roji?
roji commentedon Oct 21, 2020
For the first error, good analysis @vonzshik. I actually missed your comment and just tracked it down myself... 🤦 Should learn to read all the way to the bottom...
So #3247 fixes this, adds various assertions to tighten things and simplifies a bit too. The PR has a test for reproducing the bug too. @vonzshik do you want to take a look to see if this bug also exists in 4.1?
roji commentedon Oct 21, 2020
The timeout exception is likely to be a totally unrelated environmental thing. @kae if you manage to put some sort of repro together, can you open another issue and we'll take a look?
@vonzshik why do you say that multiplexing bootstrap isn't threadsafe? I agree it doesn't make sense to run it concurrently (we should put a simple lock there), but where do you see errors coming from it? Either way, am happy if you want to work on it and propose a fix :)
... and thanks for the bug report and the repro @kae!
roji commentedon Oct 21, 2020
One last thing... @kae can you please open a separate issue for the error when Multiplexing=true? We'll use this issue to track the original problem.
vonzshik commentedon Oct 21, 2020
Just checked - nope, it should be fine. It only happens if there is an open transaction.
npgsql/src/Npgsql/NpgsqlConnection.cs
Lines 669 to 681 in b149e1e
kae commentedon Oct 21, 2020
@roji #3248
vonzshik commentedon Oct 21, 2020
This is a tricky one. I'm still investigating the exact problem, but I do have a fix for the bootstrap. Also found a few other problems.
roji commentedon Oct 21, 2020
I'd still fix that in 4.1. It's definitely much more of a corner case than the 5.0 bug discussed here, but we still should never be touching a connector after returning it to the pool in any way - it's very likely possible to trigger a similar bug with ambient transactions. Let me know if you want to submit a PR.
vonzshik commentedon Oct 21, 2020
Sure, why not.
Fix race condition between opening and closing
Fix race condition between opening and closing (#3247)