Skip to content

Deadlock while cancelling NpgsqlConnection.WaitAsync #5975

Description

@WizardBrony

Steps to reproduce

The exact code that is encountering the issue cannot be shared. However, the relevant part of the app that results in the deadlock is very similar to the following:

await using var dataSource = NpgsqlDataSource.Create(ConnectionString);
while (true)
{
    await using var jobConnection = await dataSource.OpenConnectionAsync(stoppingToken);
    await using var notificationConnection = await dataSource.OpenConnectionAsync(stoppingToken);

    var executeJobTask = ExecuteJobAsync(jobConnection, stoppingToken);

    using var source = CancellationTokenSource.CreateLinkedTokenSource(stoppingToken);
    var processNotificationsTask = ProcessNotificationsAsync(notificationConnection, source.Token);

    await executeJobTask;

    source.Cancel();
    try
    {
        await processNotificationsTask;
    }
    catch (OperationCanceledException)
    {
    }
}

async Task ProcessNotificationsAsync(NpgsqlConnection notificationConnection, CancellationToken cancellationToken)
{
    await ListenAsync(notificationConnection, cancellationToken);
    while (true)
    {
        // ...

        await notificationConnection.WaitAsync(TimeSpan.Zero, cancellationToken);
    }
}

The issue

I am stress testing my app by effectively having ExecuteJobAsync do very little work, which allows the main while loop to repeat hundreds of times per second. The app will very consistently lock up after about an hour of running. While it's locked up, if I attach a debugger, I can see that WaitAsync is stuck waiting for NpgsqlReadBuffer.EnsureLong to complete. Additionally, I can see that

notificationConnection.Connector.UserCancellationRequested == true

however,

notificationConnection.Connector.ReadBuffer.Cts.IsCancellationRequested == false

I do not expect anyone to try to reproduce this, so if there is any additional state or diagnostic information I can provide that would help find the issue, please let me know.

Further technical details

Npgsql version: 8.0.11
PostgreSQL version: 14
Operating system: Windows

Activity

  1. self-assigned this
    on Dec 17, 2024
  2. vonzshik commented on Dec 17, 2024

    @vonzshik
    Contributor

    This is most likely a race condition between us setting a timeout here

    ReadBuffer.Timeout = TimeSpan.FromMilliseconds(timeoutForKeepalive ? keepaliveMs : timeout);

    And here, whenever a cancellation token is cancelled

    ReadBuffer.Timeout = _cancelImmediatelyTimeout;

    I'm going to investigate further later today, but as a workaround enabling keepalive should help make things much more stable.

  3. modified the milestones: 8.0.7, 9.0.3 on Dec 17, 2024
  4. modified the milestones: 9.0.3, 9.0.4 on Feb 24, 2025
  5. added theissue type on Jun 14, 2025
  6. modified the milestones: 9.0.4, 9.0.5 on Oct 1, 2025
  7. modified the milestones: 9.0.5, 10.0.3 on Mar 12, 2026
  8. modified the milestones: 10.0.3, 10.0.4 on May 27, 2026
  9. modified the milestones: 10.0.4, 10.0.5 on Nov 20, 2026
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

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions