You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Command.Cancel() is not honored if invoked immediately after execution #5191
Command.Cancel() is not honored if the cancel is requested almost immediately after the command is executed. The same issue occurs if a command cancellation is invoked via a cancellation token.
Steps to reproduce
Here is an Xunit test to reproduce the issue:
[Fact]
public async Task PostgresDriver_QueryCancelled_ThrowsOperationCancelledException()
{
using CancellationTokenSource cts = new();
await using DbConnection connection = <CREATE YOUR CONNECTION HERE>
await connection.OpenAsync(cts.Token);
await using DbCommand command = connection.CreateCommand();
command.CommandText = "DO $$ BEGIN PERFORM pg_sleep(10); END $$;";
Task t = command.ExecuteReaderAsync(cts.Token);
cts.Cancel();
await Assert.ThrowsAsync<OperationCanceledException>(async () => await t);
}
An OperationCanceledException should be thrown. Instead, the command runs for 10s.
It appears the flow control for the Cancel() call aborts here:
// We don't wait indefinitely to avoid deadlocks from synchronous CancellationToken.Register
// See #5032
if (!ReadingPrependedMessagesMRE.Wait(0))
return;
Once we are done reading prepended messages, is it possible to see if a cancellation was attempted and finish things up?
Npgsql version: 7.0.4
PostgreSQL version: Any
Operating system: Any
Other details about my project setup:
The cancellation token in our actual application is invoked by a cancelled inbound web request so we don't have control over the timing of cancellation.
Once we are done reading prepended messages, is it possible to see if a cancellation was attempted and finish things up?
// We don't wait indefinitely to avoid deadlocks from synchronous CancellationToken.Register
// See #5032
if (!ReadingPrependedMessagesMRE.Wait(0)) {
cancelAfterReadingPrependedMessages = true;
return;
}
// We've read all the prepended response.
// Allow cancellation to proceed.
connector.ReadingPrependedMessagesMRE.Set();
if (cancelAfterReadingPrependedMessages)
PerformUserCancellation();
Activity
dougkpowers commentedon Aug 5, 2023
Once we are done reading prepended messages, is it possible to see if a cancellation was attempted and finish things up?
NinoFloris commentedon Jun 27, 2024
@vonzshik Is there something we can improve here?
vonzshik commentedon Jul 1, 2024
We actually have #5070 which is about the similar issue (though there I was thinking about making cancellation request completely asynchronous).
Looking at the code I think we indeed can try and execute cancellation request just after we set
ReadingPrependedMessagesMRE.Fix sending cancellation request if it's requested while reading prep…
Fix sending cancellation request if it's requested while reading prep…
Fix sending cancellation request if it's requested while reading prep…
Fix sending cancellation request if it's requested while reading prep…