Repository navigation
Passing the cancellation token from the command to the read and write buffers - #3173
Conversation
roji
left a comment
There was a problem hiding this comment.
I think that HardCommandTimeout could still be the right parameter for both scenarios (though we may want to rename it). It means the same thing: how much do we wait, after sending a PG cancellation, before declaring that the network (or PG) is totally dead and break the connection. The critical part is that PG cancellation isn't supposed to depend on the query being cancelled in any way - it's supposed to be "instantaneous". So in my mind it doesn't really matter if the PG cancellation was sent after a regular CommandTimeout, or because the user explicitly triggered it.
Maybe we should just call the parameter CancellationTimeout?
| try | ||
| { | ||
| ((NpgsqlCommand)cmd!).Cancel(true); | ||
| if (cancellationTimeout > 0) |
There was a problem hiding this comment.
Just a thought (not for this PR)... It's true that 0 usually means "no timeout" (e.g. with CommandTimeout). However, CancellationTimeout is a bit special, in that a zero timeout could actually make sense - it would mean "don't attempt to send a PG cancellation and just break the connection right away.
I'm not sure if disabling PG cancellation could actually be useful to anyone - maybe some pseudo-PostgreSQL variant that doesn't support cancellation. It's also a bit confusing to give 0 a different meaning for CancellationTimeout than what it means for CommandTimeout. But something makes me want to support this, for completeness (and possibly use -1 to mean no timeout).
Any thoughts @vonzshik @YohDeadfall @Brar?
(no need to block this PRs on this question, we can change this later).
There was a problem hiding this comment.
An interesting idea. And I don't see a reason why not - although I highly doubt anyone would really use it. Maybe only if you want the fastest shutdown possible.
roji
left a comment
There was a problem hiding this comment.
Apart from that last allocation thing, looks great.
Re test coverage... We have a flaky (and somewhat badly-written) CancelAsync that's being ignored. Do you want to take a look at it and update it to the new behavior? I think it should be possible for it not to be flaky.
Unfortunately we cannot easily test scenarios of cancellation not working (i.e. network partition). One day we'll mock PostgreSQL :)
| var cancellationSource = new CancellationTokenSource(300); | ||
| var t = cmd.ExecuteNonQueryAsync(cancellationSource.Token); | ||
| Assert.That(async () => await t.ConfigureAwait(false), Throws.Exception.TypeOf<NpgsqlException>()); | ||
| Assert.That(async () => await t.ConfigureAwait(false), Throws.Exception.TypeOf<PostgresException>()); |
There was a problem hiding this comment.
Thanks to the test I suddenly realized we we're throwing PostgresException when the cancellation token is triggered - shouldn't we be throwing the standard OperationCanceledException instead? This is important not just because it's the standard exception for cancellation, but also because it affects the status of Tasks (canceled instead of faulted). This would mean translating the PG cancellation messages to OperationCanceledException when we received them (a bit like how we translated socket timeout exceptions to TimeoutException).
If we do this, we'd probably make the same change for sync cancellation via Cancel - a breaking change but I think we should do it.
Thoughts?
There was a problem hiding this comment.
There is actually an issue about this #1146.
Not sure, if we should change anything for sync cancellation - the result of the cancellation via Cancel should always be a PostgresException. It's just for the cancellation via the cancellation token, we would hide the original exception behind the OperationCanceledException.
Are we doing this as a part of this pr, or a separate one?
There was a problem hiding this comment.
There is actually an issue about this #1146.
Of course, forgot about that.
Not sure, if we should change anything for sync cancellation - the result of the cancellation via Cancel should always be a PostgresException
I'm not sure... I think making (sync) cancellation throw OperationCanceledException is similar to making the timeout throw TimeoutException - in both cases we're translating a lower-level exception type to a standard, higher-level .NET exception that conceptually means what we want (cancellation, timeout). Do you have any specific argument on why this should continue throwing PostgresException, apart from it being a breaking change (which is definitely an argument)?
It's just for the cancellation via the cancellation token, we would hide the original exception behind the OperationCanceledException.
It's definitely possible, though I don't think I've ever seen an inner exception on a OperationCanceledException. It could provide the extra information on where exactly the operation was cancelled (assuming a big stack with different components, and a cancellation token being passed around to everyone).
Are we doing this as a part of this pr, or a separate one?
As you're already working right in the area, it could make sense to just do it - but it's totally up to you. I'm fine with merging without this and fixing later.
There was a problem hiding this comment.
BTW another small argument for throwing OperationCanceledException for sync cancellation, is that it would make sync and async more consistent.
|
Sounds good, thanks as always @vonzshik! |
Closes #2437.
The final part for the cancellation story. The only thing missing here is the timeout, after which the token would be cancelled. It seems, I would have to add another parameter to the connection string (as
HardCommandTimeoutis only until the next read, not for the whole cancellation request). @roji objections?