Repository navigation
Milestone
Description
Steps to reproduce
class Program
{
const string connectionString = "Server=127.0.0.1;Port=5432;Database=postgres;User Id=postgres;Password=postgres;";
const int numThreads = 5;
static SemaphoreSlim s = new SemaphoreSlim(0);
static CountdownEvent c = new CountdownEvent(numThreads);
static void Main(string[] args)
{
try
{
CreateTable();
var tasks = new List<Task>();
var synchronizer = Task.Run(() =>
{
while (true)
{
s.Release(numThreads);
while (c.CurrentCount > 0)
{
c.Wait();
}
c.Reset();
}
});
tasks.Add(synchronizer);
for (int i = 0; i < numThreads; i++)
{
tasks.Add(Task.Run(RunQueries));
}
Task.WaitAll(tasks.ToArray());
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
static IDbConnection GetDbConnection()
{
return new NpgsqlConnection(connectionString);
}
static void CreateTable()
{
using (var db = GetDbConnection())
{
string script = "CREATE TABLE IF NOT EXISTS testbug(id TEXT PRIMARY KEY, value int)";
db.Execute(script);
}
}
static void RunQueries()
{
string script1 = @"
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
INSERT INTO testbug (id, value)
VALUES ('abc', 1)
ON CONFLICT (id) DO UPDATE
SET value = testbug.value + 1
RETURNING value;";
while (true)
{
s.Wait();
using (var db = GetDbConnection())
{
try
{
var result = db.QuerySingle<int>(script1);
Console.WriteLine($"Got result {result} inside try");
}
catch (Exception e) when (e.Message.Contains("Please file a bug"))
{
Console.WriteLine("Got the bug in the try");
Console.WriteLine(e);
Environment.Exit(1);
}
catch (Exception e) when (e.Message.Contains("could not serialize access"))
{
try
{
var result = db.QuerySingle<int>("SELECT 1234");
Console.WriteLine($"Got result {result} inside catch");
}
catch (Exception ex) when (ex.Message.Contains("Please file a bug"))
{
Console.WriteLine("Got the bug in the catch");
Console.WriteLine(ex);
Environment.Exit(1);
}
}
catch (Exception e)
{
Console.WriteLine(e);
}
}
c.Signal();
}
}
}
The issue
When running any query shortly after a "could not serialize access" error I sometimes get this exception:
System.Exception: Received unexpected backend message DataRow. Please file a bug.
at Npgsql.NpgsqlDataReader.ProcessDataRowMessage(DataRowMessage msg)
at Npgsql.NpgsqlDataReader.ProcessMessage(IBackendMessage msg)
at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming)
at Npgsql.NpgsqlDataReader.NextResult()
at Npgsql.NpgsqlCommand.ExecuteReaderAsync(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior)
at Npgsql.NpgsqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
at Dapper.SqlMapper.ExecuteReaderWithFlagsFallback(IDbCommand cmd, Boolean wasClosed, CommandBehavior behavior) in C:\projects\dapper\Dapper\SqlMapper.cs:line 1051
at Dapper.SqlMapper.QueryRowImpl[T](IDbConnection cnn, Row row, CommandDefinition& command, Type effectiveType) in C:\projects\dapper\Dapper\SqlMapper.cs:line 1177
at Dapper.SqlMapper.QuerySingle[T](IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Nullable`1 commandTimeout, Nullable`1 commandType) in C:\projects\dapper\Dapper\SqlMapper.cs:line 781
at NpgsqlBug.Program.RunQueries() in C:\Work\Code\NpgsqlBug\Program.cs:line 98
Further technical details
Npgsql version: 4.1.3.1
PostgreSQL version: 11.2
Operating system: Windows 10
Other details about my project setup:
Dapper version: 2.0.30
netcoreapp3.1
Activity
roji commentedon Apr 3, 2020
Where is the "could not serialize access" coming from? Could you please post a stack trace for that exception as well?
meum commentedon Apr 3, 2020
It comes from doing concurrent upserts on the same row with isolation level REPEATABLE READ, so that is expected I think.
Here is the stack trace:
roji commentedon Apr 3, 2020
Thanks for the additional info, I'll take a look at this soon.
roji commentedon Apr 3, 2020
(@YohDeadfall are you interested in taking a look?)
roji commentedon Apr 10, 2020
@YohDeadfall ping
phillip-haydon commentedon May 2, 2020
I just upgraded from 4.1.2 to 4.1.3.1 and started getting this error too :(
vonzshik commentedon Sep 2, 2020
Hello @YohDeadfall, I've managed to identify the issue. It's kind of similar to the one I've described here #2964.
There are 2 requirements:
Reader, useCommandBehavior.CloseConnection.So, it follows like this:
npgsql/src/Npgsql/NpgsqlConnector.cs
Lines 1084 to 1086 in c6c0aae
npgsql/src/Npgsql/NpgsqlConnector.cs
Lines 1123 to 1127 in c6c0aae
npgsql/src/Npgsql/NpgsqlDataReader.cs
Lines 913 to 914 in c6c0aae
StatementIndex = -1.npgsql/src/Npgsql/NpgsqlCommand.cs
Line 1239 in c6c0aae
StatementIndex.npgsql/src/Npgsql/NpgsqlDataReader.cs
Lines 488 to 513 in c6c0aae
StatementIndexand the same time. Because of this, there is a chance we can skip reading messages for some statements, so they are still in the buffer after everything is done.I've managed to write a test for this issue (although, it's not ideal and can be finicky).
roji commentedon Sep 6, 2020
@vonzshik thanks for looking into this! That's very helpful.
YohDeadfall commentedon Nov 14, 2020
Was it fixed, @vonzshik? If so, is it possible to backport? If not, it would change the milestone and just close the issue.
vonzshik commentedon Nov 14, 2020
@YohDeadfall was not yet fixed - hopefully, #3126 should be merged today.
vonzshik commentedon Nov 14, 2020
And yes, it should be possible to backport.
roji commentedon Nov 14, 2020
The way #3126 is looking, I do not think this is a good candidate for backporting... The fix is quite complex and changes a lot of state, and while this is indeed a bug, not a lot of people seem to be running into it (plus 5.0 is right around the corner).
Fixed reading by reader previous query messages