Skip to content

Received backend message ReadyForQuery while expecting ParseCompleteMessage. #1826

Closed

Description

@MirreJensen

Steps to reproduce

not deterministic.

The issue

On reading and writing data, following exception is thrown:
Npgsql.NpgsqlException (0x80004005): Received backend message ReadyForQuery while expecting ParseCompleteMessage. Please file a bug.

Exception Message:

Microsoft.EntityFrameworkCore.DbUpdateException: An error occurred while updating the entries. See the inner exception for details.
 ---> Npgsql.NpgsqlException (0x80004005): Received backend message ReadyForQuery while expecting ParseCompleteMessage. Please file a bug.

Stacktrace:

at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming, CancellationToken cancellationToken)
 at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
 at Npgsql.NpgsqlCommand.ExecuteReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
 at Npgsql.NpgsqlCommand.ExecuteDbDataReaderAsync(CommandBehavior behavior, CancellationToken cancellationToken)
 at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
 at Microsoft.EntityFrameworkCore.Storage.RelationalCommand.ExecuteReaderAsync(RelationalCommandParameterObject parameterObject, CancellationToken cancellationToken)
 at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)
 --- End of inner exception stack trace ---
 at Microsoft.EntityFrameworkCore.Update.ReaderModificationCommandBatch.ExecuteAsync(IRelationalConnection connection, CancellationToken cancellationToken)
 at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(IEnumerable`1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken)
 at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(IEnumerable`1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken)
 at Microsoft.EntityFrameworkCore.Update.Internal.BatchExecutor.ExecuteAsync(IEnumerable`1 commandBatches, IRelationalConnection connection, CancellationToken cancellationToken)
 at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(IList`1 entriesToSave, CancellationToken cancellationToken)
 at Microsoft.EntityFrameworkCore.ChangeTracking.Internal.StateManager.SaveChangesAsync(DbContext _, Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)
 at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementationAsync[TState,TResult](Func`4 operation, Func`4 verifySucceeded, TState state, CancellationToken cancellationToken)
 at Microsoft.EntityFrameworkCore.Storage.ExecutionStrategy.ExecuteImplementationAsync[TState,TResult](Func`4 operation, Func`4 verifySucceeded, TState state, CancellationToken cancellationToken)
 at Microsoft.EntityFrameworkCore.DbContext.SaveChangesAsync(Boolean acceptAllChangesOnSuccess, CancellationToken cancellationToken)

Npgsql version: Npgsql.EntityFrameworkCore.PostgreSQL v 5.0.5.1
Postgres: AWS Aurora 9.6.16
Operating system: (Debian 10) k8s -> docker-image -> mcr.microsoft.com/dotnet/aspnet:5.0 -> buster-slim-amd64

Activity

  1. roji commented on Apr 27, 2021

    @roji
    Member

    @MirreJensen there's not much we can do with an error message... I understand that it's not deterministic, but it's usually possible do write a loop that runs until the error is triggered (sometimes executed across multiple threads). This is something you think you can do?

  2. MirreJensen commented on Apr 27, 2021

    @MirreJensen
    Author

    @roji we'll try to reproduce the error. Currently we are busy with rolling back to version 5.0.2.

  3. roji commented on Apr 27, 2021

    @roji
    Member

    @MirreJensen are you saying that 5.0.2 works perfectly without ever triggering this bug, while 5.0.5.1 consistently does trigger it (even if non-deterministically)?

  4. efmk commented on May 21, 2021

    @efmk

    We've faced with similar issue.
    After analysis we found that the reason is in concurrent keepalive. I believe it will be fixed in #Npgsql 3701

  5. vonzshik commented on May 21, 2021

    @vonzshik
    Contributor

    We've faced with similar issue.
    After analysis we found that the reason is in concurrent keepalive. I believe it will be fixed in #Npgsql 3701

    @efmk is there a specific reason why you think it's the keepalive? On what version of npgsql did you test?

    npgsql/npgsql#3701 is about making sure a user is not accidentally using the same connection from multiple threads.

  6. efmk commented on May 21, 2021

    @efmk

    We've faced with similar issue.
    After analysis we found that the reason is in concurrent keepalive. I believe it will be fixed in #Npgsql 3701

    @efmk is there a specific reason why you think it's the keepalive? On what version of npgsql did you test?

    npgsql/npgsql#3701 is about making sure a user is not accidentally using the same connection from multiple threads.

    We were able to reproduce the issue (the same error message) by test running until failure. With specified keepalive (=1) in connection string, the test fails after 10 minutes. Without keepalive, the test does not fail after 3 hours of running. We use npgsql of version 5.0.5.

    Also, in npgsql/npgsql#3701 said that keepalive is not 100% concurrent free.

  7. vonzshik commented on May 21, 2021

    @vonzshik
    Contributor

    We were able to reproduce the issue (the same error message) by test running until failure. With specified keepalive (=1) in connection string, the test fails after 10 minutes. Without keepalive, the test does not fail after 3 hours of running. We use npgsql of version 5.0.5.

    We're talking about just sending queries in a loop, nothing too fancy? I'll try to take a look today, but it will be great if you're able to provide a small repro.

    Also, in npgsql/npgsql#3701 said that keepalive is not 100% concurrent free.

    That's true, but I would rather treat it as another keepalive bug, which we can fix independently of npgsql/npgsql#3701.

  8. efmk commented on May 21, 2021

    @efmk

    We're talking about just sending queries in a loop, nothing too fancy? I'll try to take a look today, but it will be great if you're able to provide a small repro.

    In fact, the test we used is emulated a real application scenario, not just sending queries in a loop.
    I tried to write a small repro test but can't implement the same behavior and reproduce the issue. I suppose there are specific conditions in the real app that can't be easily met.
    But I can show application log with TRACE level for the broken connection. 3D26 it is BackendProcessId in HEX format.
    Logger App.DataAccess.Storage.PostgreSql.NpgsqlConnectionProvider - is the application's logger. New NpgsqlConnection is created here.

    { "@timestamp": "2021-05-21T16:08:06.1025250+07:00", "Level": "TRACE", "Logger": "Npgsql.NpgsqlConnector", "Message": "[3D26] Performing keepalive" }
    
    { "@timestamp": "2021-05-21T16:08:06.1025250+07:00", "Level": "TRACE", "Logger": "Npgsql.NpgsqlConnection", "Message": "[3D26] Closing connection..." }
    
    { "@timestamp": "2021-05-21T16:08:06.1121509+07:00", "Level": "DEBUG", "Logger": "Npgsql.NpgsqlConnection", "Message": "[3D26] Connection closed" }
    
    { "@timestamp": "2021-05-21T16:08:06.1472153+07:00", "Level": "TRACE", "Logger": "Npgsql.NpgsqlConnector", "Message": "[3D26] Performed keepalive" }
    
    { "@timestamp": "2021-05-21T16:08:06.5723549+07:00", "Level": "TRACE", "Logger": "Npgsql.NpgsqlConnector", "Message": "[3D26] Start user action" }
    
    { "@timestamp": "2021-05-21T16:08:06.5723549+07:00", "Level": "DEBUG", "Logger": "Npgsql.NpgsqlTransaction", "Message": "[3D26] Beginning transaction with isolation level ReadCommitted" }
    
    { "@timestamp": "2021-05-21T16:08:06.5723549+07:00", "Level": "TRACE", "Logger": "Npgsql.NpgsqlConnector", "Message": "[3D26] End user action", "PolicyId": "7e7dd59a-454a-4277-b10f-5bfb5a2509db" }
    
    { "@timestamp": "2021-05-21T16:08:06.5723549+07:00", "Level": "DEBUG", "Logger": "Npgsql.NpgsqlConnection", "Message": "[3D26] Enlisted volatile resource manager (localid=2df616e4-39f0-42e8-a01a-82250eb2164d:1948)" }
    
    { "@timestamp": "2021-05-21T16:08:06.6749201+07:00", "Level": "TRACE", "Logger": "App.DataAccess.Storage.PostgreSql.NpgsqlConnectionProvider", "Message": "Npgsql connection created for '\"Database\"'. ProcessId: \"3D26\"" }
    
    { "@timestamp": "2021-05-21T16:08:06.6749201+07:00", "Level": "TRACE", "Logger": "Npgsql.NpgsqlConnector", "Message": "[3D26] Start user action" }
    
    { "@timestamp": "2021-05-21T16:08:06.6749201+07:00", "Level": "DEBUG", "Logger": "Npgsql.NpgsqlCommand", "Message": "[3D26] Executing statement(s):\r\n\t**Application query**\r\n" }
    
    { "@timestamp": "2021-05-21T16:08:06.7074446+07:00", "Level": "ERROR", "Logger": "Npgsql.NpgsqlConnector", "Message": "[3D26] Breaking connector", "Exception": "Npgsql.NpgsqlException (0x80004005): Received backend message CommandComplete while expecting ParseCompleteMessage. Please file a bug." }
    
    { "@timestamp": "2021-05-21T16:08:06.7074446+07:00", "Level": "TRACE", "Logger": "Npgsql.NpgsqlConnector", "Message": "[3D26] Cleaning up connector" }
    
    { "@timestamp": "2021-05-21T16:08:06.7074446+07:00", "Level": "TRACE", "Logger": "Npgsql.NpgsqlDataReader", "Message": "[3D26] Cleaning up reader" }
    
    { "@timestamp": "2021-05-21T16:08:06.7074446+07:00", "Level": "DEBUG", "Logger": "Npgsql.NpgsqlDataReader", "Message": "[3D26] Query duration time: 19ms" }
    
    { "@timestamp": "2021-05-21T16:08:09.6736144+07:00", "Level": "DEBUG", "Logger": "Npgsql.VolatileResourceManager", "Message": "[3D26] Single-phase transaction rollback (localid=2df616e4-39f0-42e8-a01a-82250eb2164d:1948)" }
    
    { "@timestamp": "2021-05-21T16:08:09.6755502+07:00", "Level": "ERROR", "Logger": "Npgsql.VolatileResourceManager", "Message": "[3D26] Exception during transaction rollback (localid=2df616e4-39f0-42e8-a01a-82250eb2164d:1948)", "Exception": "System.ObjectDisposedException: Cannot access a disposed object.\r\nObject name: 'NpgsqlTransaction'.\r\n   at Npgsql.NpgsqlTransaction.CheckDisposed()\r\n   at Npgsql.NpgsqlTransaction.CheckReady()\r\n   at Npgsql.NpgsqlTransaction.Rollback(Boolean async, CancellationToken cancellationToken)\r\n   at Npgsql.NpgsqlTransaction.Rollback()\r\n   at Npgsql.VolatileResourceManager.RollbackLocal()\r\n   at Npgsql.VolatileResourceManager.Rollback(Enlistment enlistment)" }
    
    { "@timestamp": "2021-05-21T16:08:09.6755502+07:00", "Level": "TRACE", "Logger": "Npgsql.VolatileResourceManager", "Message": "[3D26] Cleaning up resource manager (localid=2df616e4-39f0-42e8-a01a-82250eb2164d:1948" }
    
    { "@timestamp": "2021-05-21T16:08:09.6755502+07:00", "Level": "TRACE", "Logger": "Npgsql.NpgsqlConnector", "Message": "[3D26] Closing connector" }
    
    { "@timestamp": "2021-05-21T16:08:09.7867676+07:00", "Level": "TRACE", "Logger": "Npgsql.NpgsqlConnection", "Message": "[3D26] Closing connection..." }
    

    I hope these logs could be useful for you. As I see in logs, connection has been closed during keepalive performing, maybe this somehow relates to the issue.

  9. vonzshik commented on May 21, 2021

    @vonzshik
    Contributor

    @efmk it actually did help. I took a look not so long ago and found a potential bug with keepalive (npgsql/npgsql#3765). And your logs do confirm that it's possible that's the reason for your specific issue (as there is a race condition between connection closing and keepalive performing).

  10. vonzshik commented on Jul 26, 2021

    @vonzshik
    Contributor

    Closing, since the issue should have been fixed with npgsql 5.0.7.

  11. roji commented on Jul 26, 2021

    @roji
    Member

    Duplicate of npgsql/npgsql#3765

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions