Skip to content

Command.Cancel() is not honored if invoked immediately after execution #5191

Closed

Description

@dougkpowers

The issue

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.

Activity

  1. dougkpowers commented on Aug 5, 2023

    @dougkpowers
    Author

    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();
    
  2. NinoFloris commented on Jun 27, 2024

    @NinoFloris
    Member

    @vonzshik Is there something we can improve here?

  3. self-assigned this
    on Jul 1, 2024
  4. added this to the 8.0.4 milestone on Jul 1, 2024
  5. vonzshik commented on Jul 1, 2024

    @vonzshik
    Contributor

    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.

  6. added 3 commits that reference this issue on Sep 10, 2024
    6990cce
    cd786a5
    310515c
  7. added a commit that references this issue on Sep 10, 2024
    8473d72
  8. 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