Repository navigation
Always throw OperationCancelledException, while cancelling a query - #3191
Conversation
|
Just a note to say I'll be reviewing this soon... Am dealing with personal issues at the moment but should be fully back in a few days. |
|
@vonzshik let me know if/when you're still working on it, and when it's OK for me to review this. |
It's fine @roji - it seems, every other day I find something, where things might go wrong 😁 |
roji
left a comment
There was a problem hiding this comment.
Finally got around to reviewing this @vonzshik, thanks as usual for all the great work. This will probably take a few back-and-forths, so here's a first review.
One general note: it looks like the moment Cancel is called, many methods on NpgsqlDataReader will start throwing OperationCanceledException - I'm not sure this is the behavior we want. Cancellation is a best-effort thing, that may or may not terminate the query early; it's described that way both in the PG docs and in the ADO.NET docs. The behavior you're proposing would needlessly prevent some commands from completing successfully, although they could.
For example, say I send a query, PG completes it and has sent the results my way (maybe they're even buffered in NpgsqlReadBuffer). The fact that someone has triggered a cancellation token shouldn't mean the application shouldn't be able to access the data successfully.
I think, this is actually a very interesting question: how exactly do we view a cancellation token? As both approaches in my mind are more or less equal, I'll trust your experience. |
4136691 to
4e3c264
Compare
|
(note: I haven't re-reviewed yet, will wait for you to ask first)
I understand your reasoning, and you're right that there are two different philosophies here. However, note that we don't (by default!) implement immediate "emergency exit" behavior for the cancellation token passed to ExecuteReader: once the token is triggered, the user still needs to wait 2 seconds (Cancellation Timeout) before the operation is guaranteed to terminate. Users will be able to opt into immediate termination with #3208; but the point is that our default behavior already isn't to fail immediately ("emergency exit button"). But I think there's an even more interesting problem here: usually a cancellation token simply cancels the async method to which it was passed; but with ADO.NET, the server query is running across several async method calls (ExecuteReaderAsync, then ReadAsync/NextResultAsync), and the exact meaning of the token isn't completely clear. One thing I realized, is that it must be possible for users to cancel a running query at the server after ExecuteReaderAsync has completed. I think this means that passing a cancelled token to any async operation on NpgsqlDataReader should trigger the normal cancellation flow (PG cancellation followed by the cancellation timeout and a socket break).
Let me know what you think. |
|
I think, the behavior you describe does make sense. And as you said, it's possible to set the cancellation timeout to 0 (or -1 per #3208) to exit immediatelly. |
roji
left a comment
There was a problem hiding this comment.
Some more comments...
I think we need a bit of testing around cancelling of ReadAsync/NextResultAsync specifically too.
d5bf262 to
e08b460
Compare
There was a problem hiding this comment.
Finally got around to reviewing this... It's a bit unfortunate we have @Brar's #3222 going on in parallel (I'm a bit confused to track what's where).
I wrote a few comments, but I (unfortunately) had some general thoughts on our design... @vonzshik, I'm sorry that this puts into question a lot of the work we've already done, I hope it isn't too frustrating. If needed, I can do some of the work below.
Basically, It's weird that we now have 3 CTS wrappers per connection (NpgsqlConnector.CommandCts, plus one per buffer) - things seem more complicated than they need to be. It seems like we could simplify, and just have a single CTS wrapper on the connector, and have the buffers accept that in the ctor. This same wrapper can be used for both soft and hard cancellations (we just change the timeout), and the user-provided token just triggers Cancel (like currently in this PR), which starts the CTS wrapper countdown for hard cancellation.
Note that if we do this, the buffer will always pass have only one wrapper, and its token will always be passed to I/O operation (the token only ever changes if the timeout actually expires). If that's so, there's no longer any need to pass cancellation tokens around any more - each buffer simply requests the token from its wrapper (which is the connector's wrapper), and uses that. I think this is pretty much what @YohDeadfall wanted at some point, but I resisted 🤣
Am I missing something, and is there some reason why the above can't be done?
One more thing - I'll really do my best to continue working on this PR with you as quickly as possible - every time we stop for a few days I lose context :) Hope you'll be able to do the same.
There was a problem hiding this comment.
A possible idea for simplification...
We currently do different things for user-triggered cancellation and timeout-triggered cancellation - in the former we trigger CommandCts (which will cancel any ongoing I/O), in the latter we don't do that, and instead break up the stack in NpgsqlConnector.ReadMessageLong.
As an alternative, we could simply always trigger CommandCts here, and instead of catching and throwing in ReadMessageLong, we could just continue to the next loop, which would throw naturally as we'd be passing in a cancelled token.
There was a problem hiding this comment.
An interesting idea.
Not sure if it would work now, with removal of the CommandCts. I have to think about that.
There was a problem hiding this comment.
I don't think this changes anything - if CancelRequest always triggers cancellation of the buffer's cancellation token, the next read will always fail and we end up in ReadMessageLong again, breaking the connector. We can also look at this idea after this PR.
There was a problem hiding this comment.
In fact, something similar may even be possible for the other path (where cancel doesn't fail). For user cancellation we do ReadBuffer.TimeoutCts.CancelAfter (because the operation is still ongoing in parallel), whereas for timeout cancellation we set ReadBuffer.Timeout up the stack (because the operation isn't ongoing). It's probably possible to refactor things to just make a single call on ReadBuffer that handles both these cases.
But this is really just a note for something to explore after this PR.
There was a problem hiding this comment.
Am unresolving just to keep this idea "open" for afterwards.
I can trim down my PR to just add the additional functionality to the wrapper but not actually use it. |
Well, we do both work on more or less the same thing 😃.
I'm all for redesign. And yet, I do no think it's time yet - next release is pretty close, and redesign might take a while.
It is a cool looking idea. But...
Yeah, the context is everything 😄. |
It's a valid point, but the whole feature has only gone in very recently, and has not yet been released in any case. We're also still evolving it in any case (this PR already introduces quite significant changes).
This is true - we indeed need separate timeouts for read and write. However, I still think it's possible to do only with those two, and not need a 3rd (NpgsqlConnector.CommandCts). As far as I can tell, CommandCts is used (in this PR) to trigger hard cancellation when CancelRequest is called; this could be done simply by shortening the timeout on the buffers' wrappers instead (CancellationTokenSource.CancelAfter). I think that reducing the number of wrappers simplifies the code, and would also be a bit better for perf. For example, CommandCts's token is currently linked with the buffer's wrapper, and that would no longer be necessary. One consequence of this, is that it would be impossible to distinguish between user cancellation and timeout at the buffer level (this is currently done by looking at the incoming cancellation token, which is the CommandCts token. So we'd have to make that distinction at a higher layer (which may be a slightly better design, the buffers are intended to be as dumb as possible). @vonzshik if you think the current design is better (or that my proposal above doesn't have enough value), I'd be OK with going ahead with your work - we can always revisit later, or I could give my ideas a stab later. Tell me what you think and we can decide what to do.
My comment was more about myself not giving this PR enough attention 🤣 |
This should work. The only problem I see, is I'll take a look later today.
I say, let's go with your idea. This pr (and pretty much every other on the cancellation theme) is more about having a concrete example to discuss, rather than a complete proposal. Maybe, I should set a draft tag for them in the future...
May I also join and complain for a little bit about my own small brain? 😂 |
I'm ok with rebasing. |
OK, thanks for being open to all my thoughts. We probably want to keep #3213 in mind as well - if we end up pushing the cancellation/timeout logic into the buffer that's going to have some impact here for sure. |
|
Well, @roji, here you go - just as requested 😀. |
3e43c76 to
3d61ef7
Compare
3d61ef7 to
747c190
Compare
roji
left a comment
There was a problem hiding this comment.
Looking good @vonzshik, see the comments.
I'm a bit worried that we're doing this work independently from #3213, i.e. pushing down the cancellation/timeout logic into the buffer. The two are highly related, so there's a good chance some of the effort here would be lost. It may be better to try to do both in one PR, I don't know - what do you think?
| // The WriteBuffer notifies this command when the first buffer flush occurs, so that the | ||
| // send functions can switch to the special async mode when needed. | ||
| var sendTask = NonMultiplexingWriteWrapper(connector, async, finalCt); | ||
| var sendTask = NonMultiplexingWriteWrapper(connector, async, CancellationToken.None); |
There was a problem hiding this comment.
If we really end up going down this path, where buffers exclusively use their own CTS wrappers and never receive a token from the outside, we'd remove all this again :)
There was a problem hiding this comment.
If we really end up going down this path, where buffers exclusively use their own CTS wrappers and never receive a token from the outside, we'd remove all this again :)
We're definitely not removing anything. NpgsqlConnection.WaitAsync passes the cancellation token to the buffer.
There was a problem hiding this comment.
Why not have the WaitAsync token just trigger the buffer's CTS, more or less like how we do elsewhere (but without triggering PostgreSQL cancellation of course)?
There was a problem hiding this comment.
Because then we have to catch the original exception and throw the OperationCancelledException.
There was a problem hiding this comment.
We can definitely leave this for later, but it would be really weird if all our types (and all type handlers) accept a cancellation token that's almost always None just in order to support WaitAsync... Let's revisit after the rest is done.
There was a problem hiding this comment.
While I'm definitely not against this idea, I do think it requires a thorough evaluation. A lot of work has been done, and it would be a shame to just throw it away.
There was a problem hiding this comment.
While I'm definitely not against this idea, I do think it requires a thorough evaluation. A lot of work has been done, and it would be a shame to just throw it away.
There was a problem hiding this comment.
I know, and I don't underestimate how much effort went into adding all those cancellation tokens. But at the end of the day, the question is whether the design we end up with needs them or not...
Let's defer the question until we're happy with everything else, then we can take a look and make a decision.
There was a problem hiding this comment.
I don't think this changes anything - if CancelRequest always triggers cancellation of the buffer's cancellation token, the next read will always fail and we end up in ReadMessageLong again, breaking the connector. We can also look at this idea after this PR.
There was a problem hiding this comment.
In fact, something similar may even be possible for the other path (where cancel doesn't fail). For user cancellation we do ReadBuffer.TimeoutCts.CancelAfter (because the operation is still ongoing in parallel), whereas for timeout cancellation we set ReadBuffer.Timeout up the stack (because the operation isn't ongoing). It's probably possible to refactor things to just make a single call on ReadBuffer that handles both these cases.
But this is really just a note for something to explore after this PR.
You know, the more we're discussing this pr, the more i'm begining to think that you're right. I'll take a look later today. |
|
I'm finally back @roji, was dealing with some Linux problems (I mean, whose idea it was to remove every ipc object on a logout by default 😖). |
|
Added a granularity of ms for the cancellation timeout, per our discussion. |
roji
left a comment
There was a problem hiding this comment.
Thanks @vonzshik. The code as it is now looks pretty... perfect :) Concise, clear and simple - this is exactly the kind of stuff I hoped we'd end up with. I hope you're also good with the choices made (e.g. pushing the cancellation/timeout logic down into the read buffer).
See some nits but nothing substantial - I think we can merge very soon. I have some additional simplifications/improvements in mind but let's do that afterwards.
There was a problem hiding this comment.
Am unresolving just to keep this idea "open" for afterwards.
| try | ||
| if (async) | ||
| finalCt = Timeout > TimeSpan.Zero | ||
| ? Cts.Start(cancellationToken) |
There was a problem hiding this comment.
So cancellationToken is only interesting when we're called from WaitAsync, right? If so I'll maybe think about another way to handle it (but separately, later).
There was a problem hiding this comment.
@vonzshik can you confirm that this is your understanding as well?
There was a problem hiding this comment.
Yep, it is so. Can confirm 😄
There was a problem hiding this comment.
We've completely forgotten about copy operations, haven't we...
There was a problem hiding this comment.
I did have them in the back of my mind, though yeah 🤣
I've added some thoughts in #1328 (comment), which we can use to track all the COPY work. I still don't see a reason for us to pass the user cancellation token all the way down to the buffer - but I'll hold off on doing any refactoring until we discuss this between us and agree.
There was a problem hiding this comment.
Also, NpgsqlTransaction ![]()
roji
left a comment
There was a problem hiding this comment.
Looks great, thanks. I'll probably do a few more nits in a separate PR myself (will ping you).
I'm really happy where this ended up.
|
Merged via ec6746f |
Closes #1146
The main changes here are: