Skip to content

Implement async cancellation #2437

Closed

Description

@roji

It seems that .NET Core 3.0 finally honors cancellation tokens at the socket layer - https://github.com/dotnet/corefx/issues/24430. We should review what we currently do with cancellation (token vs. DbCommand.Cancel()) and consider what to do. One possibility would be to simply pass the cancellation token to the socket layer (but we need to understand the state of things after such a cancellation occurs), and to keep DbCommand.Cancel() for the PostgreSQL-specific cancellation mechanism (via a second connection).

  • We should probably keep the current behavior or doing PG cancellation, but consider adding a frontend cancellation (i.e. close the socket) if nothing happens for a period of time. This would solve the case of a network partition, and would be similar to what Trigger backend cancellation when a (frontend) timeout occurs #1567 proposes for timeouts.

Activity

  1. added this to the 4.1 milestone on Apr 16, 2019
  2. YohDeadfall commented on Apr 17, 2019

    @YohDeadfall
    Contributor

    With that feature cancellation can be done on the same connection after closing the socket.

  3. roji commented on Apr 17, 2019

    @roji
    MemberAuthor

    @YohDeadfall I don't understand what you mean. One point of PostgreSQL cancellation is to be able to continue using the socket (no closing), and to terminate the query on the server side (which just closing the socket would not achieve). Also, unless I'm mistaken any sort of socket-level cancellation (i.e. https://github.com/dotnet/corefx/issues/35012#issuecomment-483779404) would probably leave the connection in an unusable state, because some data has been written/read and we probably lose protocol sync - although I'm not sure about this, we need to investigate.

    The more I think about it, the more I think there really are two types of cancellations:

    1. Server-side PostgreSQL cancellation, via a second physical connection - like today. This is already triggered via NpgsqlCommand.Cancel(), or by triggering the async cancellation token. The disadvantage is that this isn't immediate, and there's no guarantee it would actually work (e.g. network partition).
    2. Client-side "cancellation" , at the socket level - this always works, immediately. If what I wrote above is correct, then cancelling the socket operation wouldn't really have any advantage over... just closing the connection.
  4. YohDeadfall commented on Apr 17, 2019

    @YohDeadfall
    Contributor

    Client-side "cancellation" , at the socket level - this always works, immediately.

    Would it immediately kill the corresponding backend? I saw situations when a fronted died/crashed, but the backend kept working.

  5. roji commented on Apr 17, 2019

    @roji
    MemberAuthor

    If the socket is killed client-side, then the PostgreSQL backend process will die as soon as it reads from its own side - which would be after the current query completed. If you want to kill the server process immediately, the 1st type of cancellation is required above.

    Basically the 1st kind of cancellation is almost always better - unless there's a network partition, in which case the 2nd kind (which isn't exactly a cancellation) is the only way which would release things. I'm not sure whether we should be providing an API to do automatically try the 1st, and forcibly close the connection after a certain amount of time (or whether this should be implemented by the user).

    Also, not that we currently don't support forcible client-side socket closing well - there are some race conditions around that...

  6. YohDeadfall commented on Apr 17, 2019

    @YohDeadfall
    Contributor

    Thanks for the explanation. I meant that we can use the same connector for which the socket was canceled to ensure that the backend was stopped and help it if it doesn't.

  7. modified the milestones: 4.1, 5.0 on Aug 26, 2019
  8. Brar commented on Jul 9, 2020

    @Brar
    Member

    I'm currently thinking about implementing a client side timeout in streaming replication via CancellationTokenSource.CancelAfter which would obviously depend on passing an optional CancellationToken to asynchronous read operations.
    This is a kind of special case because a streaming replication connection is a really long-lived connection where recycling of the socket or connector is pretty much a non-issue.
    @roji and @YohDeadfall, are there any objections in pursuing this or would you rather direct me in another direction?

  9. YohDeadfall commented on Jul 9, 2020

    @YohDeadfall
    Contributor

    As described in #3052 (comment) we have a problem with cancellation. Even if there is a pooled connection it's better to close the socket and lost the connection than terminate another command.

  10. Brar commented on Jul 10, 2020

    @Brar
    Member

    So @YohDeadfall, if I understand this correctly, you are for pursuing cancellation of async read operations via CancellationToken (and by that killing the socket/connector), because the PostrgeSQL way of cancelling commands via a separate connection leads to issues in our code base.

    Right?

  11. roji commented on Jul 10, 2020

    @roji
    MemberAuthor

    @Brar this sounds right to me - we indeed want an exception to be automatically raised if the network is cut, otherwise read operations (i.e. IAsyncEnumerator.MoveNextAsync) will block forever).

    @YohDeadfall this isn't about user-triggered cancellation, but about (socket) timeout, so it's more similar to #607 (definitely not about PG cancellation via a separate connector etc.). Note also that we're talking about replication connections, which are special - these aren't normal connections where the user is executing commands, and where you can accidentally cancel the wrong command etc.

    Note that the user will still have the option of passing a cancellation token to the entire replication enumeration process, so it should be possible for them to stop the replication at any point as well.

  12. Brar commented on Jul 10, 2020

    @Brar
    Member

    After reading a bit in #2911 I came to the conclusion that I should probably wait for this PR to get merged before going on with the client side timeout in streaming replication.
    Although #2911 will not provide the functionality really needed for this (see #2911 (comment)), it lays some ground and is probably too closely related to conveniently merge changes after it was merged into dev.
    In the end we'll probably need to take an additional (optional) CancellationToken as an argument to our internal async methods and link it with the one introduced by #2911

  13. 6 remaining items

  14. roji commented on Aug 18, 2020

    @roji
    MemberAuthor

    If I'm understanding correctly, plumbing the cancellation token into NpgsqlReadBuffer.Ensure would (for now) only be for the purposes of replication cancellation, right? Regular command execution would continue as today, wiring up the cancellation token to trigger a PostgreSQL server cancellation instead?

    @YohDeadfall I'm not sure about saving the token as state variable (on NpgsqlReadBuffer?)... the standard is to pass it down the stack. Handlers can simply pass the default token when doing Ensures.

  15. YohDeadfall commented on Aug 18, 2020

    @YohDeadfall
    Contributor

    Async enumerators is a good example of storing the passed token as a part of a state machine.

  16. roji commented on Aug 18, 2020

    @roji
    MemberAuthor

    @YohDeadfall the point with async enumerators is that the same cancellation token applies to the entire enumeration (or enumerator). When you do ExecuteReader, the cancellation token is only valid for that call - once it completes it isn't valid (other async calls on the returned reader accept other tokens).

  17. Brar commented on Aug 18, 2020

    @Brar
    Member

    Instead of passing it through all methods, just save it as a part of the current state and use it internally. The token is used only by IO and not by handlers or other stuff to read data.

    I'd prefer to stick with the common CancellationToken cancellationToken = default method argument.
    It is pretty much standard across .NET and as a simple-minded person I hate global state.

    If I'm understanding correctly, plumbing the cancellation token into NpgsqlReadBuffer.Ensure would (for now) only be for the purposes of replication cancellation, right? Regular command execution would continue as today, wiring up the cancellation token to trigger a PostgreSQL server cancellation instead?

    Not necessarily. It depends, whether we already agree on the way forward.

  18. roji commented on Sep 6, 2020

    @roji
    MemberAuthor

    See overall discussion on cancellation and timeout: #1567 (comment)

  19. roji commented on Sep 6, 2020

    @roji
    MemberAuthor

    I'd prefer to stick with the common CancellationToken cancellationToken = default method argument.
    It is pretty much standard across .NET and as a simple-minded person I hate global state.

    I also agree with this because I'm also a simple-minded person 😄

  20. YohDeadfall commented on Sep 6, 2020

    @YohDeadfall
    Contributor

    FYI, already supported the idea in the new type handlers which hope will publish soon.

  21. roji commented on Sep 17, 2020

    @roji
    MemberAuthor

    @vonzshik assigning to you as you expressed an interest in this (#3142 (comment))

  22. changed the title [-]Reexamine cancellation[/-] [+]Implement async cancellation[/+] on Sep 21, 2020
  23. 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

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions