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
Throw OperationCanceledException for cancellation instead of PostgresException #1146
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
@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?
@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:
Standard cancellation behavior (throw TaskCanceledException directly), or
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.
@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).
@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.
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.
It actually does make sense. Although, 4 things I don't like:
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.
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.
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 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.
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.
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?
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.
changed the title [-]Connector.Break() and not throw the exception on an OperationCanceledException[/-][+]Throw OperationCanceledException for cancellation instead of PostgresException[/+]on Oct 20, 2020
Activity
roji commentedon Jun 6, 2016
@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?mikkeljohnsen commentedon Jun 6, 2016
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.
roji commentedon Jun 6, 2016
Ah OK, I'll check it out.
BTW I don't think a cancellation is supposed to break the connector.
roji commentedon Jun 7, 2016
@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:
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.
Some test work around async cancellation
mikkeljohnsen commentedon Jun 8, 2016
@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.
16 remaining items
roji commentedon Jun 29, 2020
@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.
roji commentedon Sep 17, 2020
@vonzshik assigning to you as you expressed an interest in this (#3142 (comment))
roji commentedon Sep 25, 2020
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.
vonzshik commentedon Sep 25, 2020
It actually does make sense. Although, 4 things I don't like:
roji commentedon Sep 26, 2020
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).
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.
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.
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.
vonzshik commentedon Sep 27, 2020
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:NpgsqlCommand.Cancel.NpgsqlCommand.Cancel.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.
roji commentedon Sep 27, 2020
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?
vonzshik commentedon Sep 27, 2020
Nope, not really.
roji commentedon Sep 27, 2020
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.
[-]Connector.Break() and not throw the exception on an OperationCanceledException[/-][+]Throw OperationCanceledException for cancellation instead of PostgresException[/+]