Improve connector's cancellation request · Issue #3277 · npgsql/npgsql · GitHub
Skip to content

Improve connector's cancellation request #3277

Description

@vonzshik
  • Make an asynchronous option
  • Add a timeout for the whole request

Activity

  1. self-assigned this
    on Oct 27, 2020
  2. added this to the 5.0.0 milestone on Oct 27, 2020
  3. roji commented on Nov 11, 2020

    @roji
    Member

    Thinking about this again after refactoring cancellation...

    PerformPostgresCancellation should probably always return immediately. Since PG cancellation isn't guaranteed to work, is completely out-of-band (on a different connection), and isn't support to throw any exceptions, there is no reason to wait for the send to complete. We can simply perform the operation asynchronously and not await it.

    This is important because PerformPostgresCancellation is called when the user's cancellation token is triggered, which means that when we dispose CancellationTokenRegistration, we have to wait (synchronously or asynchronously) until the callback completes. It's difficult to do this - CancellationTokenRegistration isn't IAsyncDisposable in netstandard2.0, and even when it is we have an async flag to check. We could do all this, but there really isn't any reason.

  4. roji commented on Nov 11, 2020

    @roji
    Member

    One note on the above: not waiting for the cancellation attempt to complete could mean that we miss a write exception, which does affect our flow a bit. However, that doesn't seem important - in the worse case, we'll think a cancellation attempt succeeded when it hasn't, and wait two seconds before doing hard cancellation; that seems reasonable.

  5. vonzshik commented on Nov 11, 2020

    @vonzshik
    ContributorAuthor

    Wouldn't it lead to possible concurency issue, when the request is send at the end of a query, and it cancels something else?

  6. roji commented on Nov 11, 2020

    @roji
    Member

    This can always happen regardless of this, no?

  7. vonzshik commented on Nov 11, 2020

    @vonzshik
    ContributorAuthor

    https://www.postgresql.org/message-id/CADT4RqD2h199isLd7wDVm_ogBnWn0FgKtYBqLEdn-QZh3bGP6A%40mail.gmail.com

    Yeah, I saw this.

    This can always happen regardless of this, no?

    Not really - we wait, until pg closes the cancellation connection.

  8. vonzshik commented on Nov 11, 2020

    @vonzshik
    ContributorAuthor

    But it can happen for the NpgsqlCommand.Cancel.

  9. roji commented on Nov 11, 2020

    @roji
    Member

    Not really - we wait, until pg closes the cancellation connection.

    But how does that prevent race conditions? The cancellation token (or NpgsqlCommand.Cancel) can be called at any time, from any thread, and nothing guarantees when the cancel packet arrives at PG or when it applies it... Also, it's the user's responsibility to properly wait for the 1st command to finish (either normally or with a cancellation exception), before a second command is sent, no?

    (I know that there's a comment about this in DoCancelRequest - but I'm not sure it's good behavior)

  10. roji commented on Nov 11, 2020

    @roji
    Member

    OK, yeah - I forgot that we wait for PG to actually close the connection, so it's reasonable to expect that the cancellation has already been applied. I will scope this out of my refactor for now.

  11. roji commented on Nov 13, 2020

    @roji
    Member

    @vonzshik I propose we leave this for 6.0 - we will have cancellation-related leftovers in any way, does that sound OK?

  12. vonzshik commented on Nov 13, 2020

    @vonzshik
    ContributorAuthor

    ok

  13. modified the milestones: 5.0.0, vNext on Nov 13, 2020
  14. modified the milestones: 6.0.0, 5.0.11, 7.0.0 on Oct 14, 2021
  15. modified the milestones: 7.0.0, Backlog on Sep 11, 2022
  16. added theissue type on Jun 14, 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

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions