Skip to content

Throw OperationCanceledException for cancellation instead of PostgresException #1146

Closed

Description

@mikkeljohnsen

610e319
190eb8555

Should catch, but only invoke Connector.Break() and not throw the exception on an OperationCanceledException (which also catches TaskCanceledException)?

Npgsql.NpgsqlException: Exception while reading from stream --->
System.Threading.Tasks.TaskCanceledException: A task was canceled.
  --- End of inner exception stack trace ---
  at Npgsql.ReadBuffer+<EnsureAsync>c__async0.MoveNext () [0x00266] in
/home/mkj/rpm/BUILD/npgsql-3.1.2/src/Npgsql/GeneratedAsync.cs:1494

Activity

  1. roji commented on Jun 6, 2016

    @roji
    Member

    @mikkeljohnsen can you give a bit more detail about what you're doing (a code sample)?

    Unless I'm wrong, if you're using NpgsqlCommand.Cancel() then no exception should ever get thrown. But if you're using an async API and cancelling via a cancellation token then you should get an exception, like any other async API. Are you seeing something different?

  2. mikkeljohnsen commented on Jun 6, 2016

    @mikkeljohnsen
    Author

    We are using a token to cancel the request.

    The exception we get is NpgsqlException but the OperationCanceledException/TaskCanceledException is an InnerException.

    I think you should catch the TaskCanceledException and do "Connector.Break(); throw" so the exception is passed to our application as is.

  3. roji commented on Jun 6, 2016

    @roji
    Member

    Ah OK, I'll check it out.

    BTW I don't think a cancellation is supposed to break the connector.

  4. added this to the 3.1.4 milestone on Jun 6, 2016
  5. self-assigned this
    on Jun 6, 2016
  6. roji commented on Jun 7, 2016

    @roji
    Member

    @mikkeljohnsen, I just checked the behavior for this in SqlClient and Npgsql seems to be doing more or less the same thing. When cancelling an async query with a cancellation token in SqlClient, an SqlException is thrown with "Operation cancelled by user" in the error message (no inner exception) - not a TaskCanceledException exception.

    This is part of a general decision in SqlClient where all the server/network-related errors are thrown as SqlException (and not, say, IOException). In 3.1 Npgsql switched to do the same - I/O exceptions are wrapped in an NpgsqlException (see #897 for a discussion on all this).

    With cancellation I admit this is a little strange - when activating a cancellation tokens users expect a TaskCanceledException, not an NpgsqlException. Even worse, the fact that a TaskCanceledException isn't thrown (directly) means that the Task's status isn't Canceled - it's Faulted (since Tasks only get to state Canceled if a TaskCanceledException is thrown). So it's a matter of deciding between:

    1. Standard cancellation behavior (throw TaskCanceledException directly), or
    2. Throw all server/network-related exceptions as NpgsqlException

    Because SqlClient does the second, I think we should probably do the same (but am open to hear arguments).

    Regardless of the above, in Npgsql activating the cancellation token does break the connection, unlike with SqlClient and unlike with the NpgsqlCommand.Cancel() method (which triggers the server cancellation flow). See #633 for a discussion.

    Am going to close this as I think we're doing the right thing, but feel free to continue posting.

  7. removed this from the 3.1.4 milestone on Jun 7, 2016
  8. added and removed on Jun 7, 2016
  9. added a commit that references this issue on Jun 7, 2016
  10. mikkeljohnsen commented on Jun 8, 2016

    @mikkeljohnsen
    Author

    @roji I see the point in doing the same as SqlClient and also the point of being able to catch all exceptions as NpgsqlExceptions.

    But a TaskCanceledException is not really an exception in the sense that it is unexpected, it is expected behavior when you cancel a token. So I would prefer to get a TaskCanceledException, instead of having to catch a NpgsqlException and check the InnerException. (Not sure how to do this in SqlClient, do they want people to match on the string).

    I would prefer option 1.

  11. 16 remaining items

  12. roji commented on Jun 29, 2020

    @roji
    Member

    @KrzysztofBranicki sorry, I was missing the context of this issue when I wrote my previous comment - please ignore.

    I do absolutely believe we should be throwing OperationCanceledException/TaskCanceledException, and will do my best to have that done for 5.0. The only reason it hasn't been done yet has been priorities and too much work generally.

  13. assigned and unassigned on Sep 17, 2020
  14. roji commented on Sep 17, 2020

    @roji
    Member

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

  15. roji commented on Sep 25, 2020

    @roji
    Member

    Continuing the discussion in #3173 (comment), it actually does make sense to me to have PostgresException as an inner exception on the OperationCancelledException - it would allow users to tell which kind of cancellation actually occurred. A soft, server-side cancellation would bubble up as OperationCancelledException with an inner PostgresException, while in cases of network partition, we'd just bubble up the original OperationCancelledException as thrown by the socket.

    The difference between the two cases is important, because when the first occurs the connection is still open/usable, but not when the second occurs.

  16. vonzshik commented on Sep 25, 2020

    @vonzshik
    Contributor

    It actually does make sense. Although, 4 things I don't like:

    1. If the cancellation is not successful, the connection would be broken. And in this case, as a user I don't really care if the cancellation was successful or not.
    2. Yes, when the cancellation token is cancelled, we will attempt to cancel the query on PG side. But it's the details of the implementation, it's not something user should be aware of.
    3. In case of the cancellation token, we should only check for the cancellation tokes itself. If it was cancelled - we hide the original exception behind the OperationCancelledException. For the Cancel, it would only possible while using some other variable, which we will have to check. Or, maybe we could use the cancellation token source from the connector.
    4. What about cancelling a sync query?
  17. roji commented on Sep 26, 2020

    @roji
    Member

    If the cancellation is not successful, the connection would be broken. And in this case, as a user I don't really care if the cancellation was successful or not.

    I assume that by "cancellation is not successful", you mean that PG cancellation timed out and we do a socket cancellation, right? If so, the point is how the user is supposed to know which of these happened. Sure - they can check the state of the connection after each OperationCanceledException (is it broken?), but it seems better to expose that information via the exception (for example, it would allow using exception filters).

    Yes, when the cancellation token is cancelled, we will attempt to cancel the query on PG side. But it's the details of the implementation, it's not something user should be aware of.

    Maybe. On the other hand we're not forcing them to be aware of it - it's just an inner exception inside the OperationCanceledException, providing extra details which they're free to ignore.

    In case of the cancellation token, we should only check for the cancellation tokes itself. If it was cancelled - we hide the original exception behind the OperationCancelledException. For the Cancel, it would only possible while using some other variable, which we will have to check. Or, maybe we could use the cancellation token source from the connector.

    If I'm understanding you correctly, you're talking about async vs. sync here. This is one of the reasons I believe that sync Cancel should also throw OperationCanceledException (like async), and that if, in async, we put the PostgresException as the inner exception of OperationCanceledException, we can do the same for sync. This would completely align the two.

    What about cancelling a sync query?

    You mean, how to force-cancel the socket after the PG cancellation fails (times out)? This isn't as trivial as using a cancellation token for async - I wrote a bit in #3187 about it. It should be possible to simply close the socket, immediately causing some exception (which?) to be raised on the blocked read operation. We could check how that works, identify it etc.

  18. vonzshik commented on Sep 27, 2020

    @vonzshik
    Contributor

    You mean, how to force-cancel the socket after the PG cancellation fails (times out)? This isn't as trivial as using a cancellation token for async - I wrote a bit in #3187 about it. It should be possible to simply close the socket, immediately causing some exception (which?) to be raised on the blocked read operation. We could check how that works, identify it etc.

    I'm talking about a case, when we send a sync query, and cancel it with NpgsqlCommand.Cancel. Essentially, we have 3 different cancellation cases:

    • Sync query, cancel through NpgsqlCommand.Cancel.
    • Async query, cancel through NpgsqlCommand.Cancel.
    • Async query, cancel through the cancellation token.

    While I'm still not sure that we should always throw an OperationCancelledException for all of them, I've gone ahead and did just that. A pr should be ready in an hour.

  19. roji commented on Sep 27, 2020

    @roji
    Member

    While I'm still not sure that we should always throw an OperationCancelledException for all of them

    I'm definitely interested in hearing other opinions on this! I think it's good for sync and async to be consistent on this, and it would allow the sync OCE to contain the same triggering inner exception as the async OCE (assuming we agree to do that). The only disadvantage I can see, is that it would be a breaking change since today a PostgresException is thrown.

    Do you have other arguments against throwing OCE for sync cancellation?

  20. vonzshik commented on Sep 27, 2020

    @vonzshik
    Contributor

    Do you have other arguments against throwing OCE for sync cancellation?

    Nope, not really.

  21. roji commented on Sep 27, 2020

    @roji
    Member

    I guess one last thought, is that I've always disliked cancellation being represented in Npgsql by just another PG error code. After all, cancellation isn't an error - it's a user-triggered, desireable outcome, that just happens to be represented by an exception, because that's the mechanism for signalling that an operation could be completed. Just like in .NET, failed Tasks have a different state code from cancelled Tasks, it makes sense to me to throw a very clearly different exception.

    I'm still a little worried about the impact of this breaking change, as code currently catching PostgresException for cancellation will no longer work. But it seems like the right thing to do if well-documented.

  22. changed the title [-]Connector.Break() and not throw the exception on an OperationCanceledException[/-] [+]Throw OperationCanceledException for cancellation instead of PostgresException[/+] on Oct 20, 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

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions