Skip to content

NpgsqlConnection.WaitAsync(): Npgsql.NpgsqlException (0x80004005): Received backend message RowDescription while expecting ParseCompleteMessage. Please file a bug. #2506

Closed

Description

@worldleaderpretend

I ran into this a few times while using a listener. I finally traced it back to a race condition with the cancellation token. Seems if a cancelled token gets sent to WaitAsync(), then the next command fails with this error. Here's some sample code (TableA is a non-existent). I tested with 4.0.7:

using (NpgsqlConnection connection = new NpgsqlConnection(connectionString))
{
    connection.Open();

    using (NpgsqlCommand command = new NpgsqlCommand("LISTEN TableA", connection))
    {
        command.ExecuteNonQuery();
    }

    using (CancellationTokenSource tokenSource = new CancellationTokenSource())
    {
        try
        {
            tokenSource.Cancel();

            connection.WaitAsync(tokenSource.Token).Wait();
        }
        catch (AggregateException ex) when (ex.InnerExceptions.Any(x => (x is TaskCanceledException)))
        {
        }
    }

    using (NpgsqlCommand command = new NpgsqlCommand("SELECT NULL", connection))
    {
        command.ExecuteNonQuery();
    }
}

I found that I can workaround this by locking the call to WaitAsync to prevent the cancel occurring before the async returns. So it appears everything is good to go if it makes it to the first await, It basically turns it into the following code, which works fine:

using (NpgsqlConnection connection = new NpgsqlConnection(connectionString))
{
    connection.Open();

    using (NpgsqlCommand command = new NpgsqlCommand("LISTEN TableA", connection))
    {
        command.ExecuteNonQuery();
    }

    using (CancellationTokenSource tokenSource = new CancellationTokenSource())
    {
        try
        {
            Task connectionWait = connection.WaitAsync(tokenSource.Token);

            tokenSource.Cancel();

            connectionWait.Wait();
        }
        catch (AggregateException ex) when (ex.InnerExceptions.Any(x => (x is TaskCanceledException)))
        {
        }
    }

    using (NpgsqlCommand command = new NpgsqlCommand("SELECT NULL", connection))
    {
        command.ExecuteNonQuery();
    }
}


Activity

  1. added this to the 4.0.8 milestone on Jun 26, 2019
  2. self-assigned this
    on Jun 26, 2019
  3. austindrenski commented on Jun 30, 2019

    @austindrenski
    Contributor

    @roji Looks like this was meant to close with #2507, but wasn't because f5bbc74 wasn't merged with our default branch (e.g. dev).

  4. roji commented on Jul 1, 2019

    @roji
    Member

    @austindrenski was keeping this open until #2443 is merged, which fixes this in dev.

  5. reopened this on Jul 1, 2019
  6. modified the milestones: 4.0.8, 4.0.9 on Jul 18, 2019
  7. modified the milestones: 4.0.9, 4.0.10 on Aug 14, 2019
  8. roji commented on Aug 27, 2019

    @roji
    Member

    This was actually merged into 4.0.8 as @austindrenski wrote above.

  9. modified the milestones: 4.0.10, 4.0.8 on Aug 27, 2019
  10. added theissue type on Jun 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions