-
Notifications
You must be signed in to change notification settings - Fork 873
Deadlock while cancelling NpgsqlConnection.WaitAsync #5975
Description
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 == truehowever,
notificationConnection.Connector.ReadBuffer.Cts.IsCancellationRequested == falseI 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