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
Trigger backend cancellation when a (frontend) timeout occurs #1567
Npgsql currently has frontend (or socket) timeouts - if a read is stuck on a socket for too long, an exception is raised and the connection is broken. One disadvantage of this is that the backend is left with the running query, at least until it attempts to write some results back (at which point it would bomb due to the closed connection). For cases of which don't produce any results for a long time, this is problematic.
When a socket timeout occurs, we can trigger a cancellation on the connection to resolve this.
Split off from #1344, and also discussed in #1551.
Reacted by Mauricio Scheffer, Evgeny Morozov, Florian Rainer, Roman Guryanov, Andrey Kudashkin, Rob Emery and Anthony Mastrean
@vonzshik nope - although I think @Brar is doing some timeout-related work in his logical replication branch, so this may not be the perfect time (@Brar can you confirm?).
Otherwise feel free to work on this, but it may be a good idea to discuss exactly what we want to do before starting implementation, and to consider the discussion in #2437 (this issue actually seems to be a dup of that one).
Here are some thoughts on what we should do:
I think the ideal behavior in Npgsql is to first attempt a so-called backend/PostgreSQL timeout - contact PG and trigger cancellation of the currently-running query. This has the advantage of terminating long operations in PG, and of keeping the physical connection alive.
If the PG timeout isn't completed within a certain time window, we then perform a client/socket timeout. This always succeeds, and breaks the physical connection.
Note that this backend-then-frontend behavior would need to be implemented across the board, for both sync and async. This would mean NpgsqlCommand.Cancel and the cancellation token of the DbCommand.Execute* methods, as a minimum.
5 seconds before client timeout, we contact PG to cancel query
If we are successful, we should throw a timeout exception (without breaking connection)
On closing connection, it returns to the pool to be reused
If we are not successful with cancellation within 5 seconds, we should get a timeout exception from socket (cancellation token in case of async). Also, we (maybe?) cancel cancel request.
On closing connection, it returns to the pool to be disposed
@vonzshik yes, that more or less corresponds to what I had in mind. Some open questions:
Should we contact PG to cancel query after 25 seconds (as you suggest), or after 30? I think after 30 is better, with the Command Timeout setting being a lower bound rather than an upper bound. It would be surprising (and probably more ncorrect) if the user specifies 30 seconds, but their query gets cancelled after only 25.
If the PG cancellation succeeds, we should probably continue to throw the PostgresException with SqlState=57014, rather than TimeoutException. Besides allowing the user to distinguish the two cancellation types (and just passing along the PG response), throwing a TimeoutException would be a breaking change - existing code already expects PostgresException as a consequence of cancellation. (it's true that we would start to throw TimeoutException for the client timeout in case the PG one fails, but that seems more acceptable).
Also, we (maybe?) cancel cancel request.
Very good point.
Anyway, I'd like to hear @YohDeadfall's and @Brar's opinions about this plan (and also to know whether @Brar thinks there's going to be a conflict with his own work), but aside from that it sounds like we're aligned.
I consider timeouts and cancellation as related but not identical concepts.
While timeouts are probably the most common reason to trigger cancellation, it might also happen as a reaction to a GUI event (e. g. a user clicking a button to abort a long running query).
So to me #2437 is a more general thing about cancellation whereas this is more about timeouts that cause a cancellation.
While the actual implementation will overlap to a huge amount, I don't think we should mix the two terms or treat them as interchangeable.
I think @Brar is doing some timeout-related work in his logical replication branch, so this may not be the perfect time (@Brar can you confirm?).
Yes I can.
What I'm currently working on (cancellation and timeouts in replication connections) will rely on async cancellation but will not use anything around CommandTimeout so the actual overlap of our work would probably be the cancellation part. Also, since the replication API will be async only, I don't care about sync cancellation.
For this reason I think that we should, at first, talk about what we actually want regarding (async) cancellation (#2437 - e. g. wire the cancellation tokens we already have on some of our public API down to the actual socket + trigger backend cancellation first as an attempt to save the physical connection) and then both of us could probably do their thing without too much overlap.
Should we contact PG to cancel query after 25 seconds (as you suggest), or after 30? I think after 30 is better, with the Command Timeout setting being a lower bound rather than an upper bound. It would be surprising (and probably more ncorrect) if the user specifies 30 seconds, but their query gets cancelled after only 25.
Agree, it should be 30 seconds.
If the PG cancellation succeeds, we should probably continue to throw the PostgresException with SqlState=57014, rather than TimeoutException. Besides allowing the user to distinguish the two cancellation types (and just passing along the PG response), throwing a TimeoutException would be a breaking change - existing code already expects PostgresException as a consequence of cancellation. (it's true that we would start to throw TimeoutException for the client timeout in case the PG one fails, but that seems more acceptable).
Yep, we shouldn't change this behavior (at the very least in this issue).
I've had some time to think it over, and this is how I see it working:
There are no changes in NpgsqlReadBuffer, it should work as it is.
We only change NpgsqlConnector (I'm talking about while (true) reading loop here).
When NpgsqlReadBuffer fails because of CommandTimeout (doesn't matter, sync or async), we should catch it in the loop.
Then, if and only if we determine that exception is defitinelly because of CommandTimeout, we send request to PG to cancel query. Also, this request should have timeout different from CommandTimeout (maybe another param in ConnectionString?).
If request also fails with timeout, then nothing changes - connection is broken etc.
But if it is successful, we return to the read loop and proceed to read everything, until we get PG exception.
After that, also nothing changes - connection is not broken, it returns to the pool to be reused.
Most likely I've missed some cases, where it wouldn't work (multiplexing?).
Sorry for disappearing for a while, had to deal with some personal issues. Although this issue is about timeouts, I'll try to summarize things as I see them for both timeouts and cancellation, as they're somewhat related.
User-triggered cancellation seems to be the simpler case (this is tracked by #2437).
Both DbCommand.Cancel (sync) and the cancellation token (async) trigger PostgreSQL server cancellation.
Good: it actually cancels the query in PG
Bad: if there's a network partition it has no effect, and user code is stuck on read forever.
Bad (minor): PostgreSQL server cancellation is unapplicable for multiplexing (queries are interleaved on the connection, and it's impossible to know which one will be cancelled).
Since Socket now honors cancellation tokens, we can add a socket cancellation at some interval after the backend cancellation times out. This would solve the two bad points above.
User-managed timeout is more complicated:
Our current user timeout story (i.e. CommandTimeout) is pretty much the opposite: CommandTimeout is only implemented via socket timeouts.
Bad: the PG query doesn't get cancelled
Good: it there's a network partition, user code gets the exception and stops hanging.
In a nutshell, statement_timeout fires after X seconds have passed, regardless of the type of time. For example, if the user code takes time in processing some row result, that time is counted in the timeout. This is very different from the socket timeouts we're using, which are only about time spent waiting for responses (on the network).
However, an advantage of statement_timeout compared to PG cancellation, is that if a network partition occurs at some point after the query has started, the former will cancel the query while the latter won't.
As @Brar noted below, it's always possible for users to manually set statement_timeout just like any other PG parameter if that's what they want.
In light of the above, I think we should continue not using statement_timeout, and implement PG cancellation as already discussed in this issue. Basically, the scenario of a network partition after the query starts doesn't seem important enough compared to the quirkiness of how statement_timeout works (and the subtle breakage that starting to use it would introduce).
Also, as we're already considering doing the timeout part, we should consider doing the cancellation part as well for 5.0, to (finally) present a complete and working cancellation & timeout story in Npgsql.
In light of the above, I think we should continue not using statement_timeout, and implement PG cancellation as already discussed in this issue.
Agreed.
Basically, the scenario of a network partition after the query starts doesn't seem important enough compared to the quirkiness of how statement_timeout works (and the subtle breakage that starting to use it would introduce).
Agreed.
Also if somebody really uses very heavy queries that should definitely stop after some time, even in case of a network partition, there's nothing stopping them from issuing a SET statement_timeout TO 123; before the actual query.
Also, as we're already considering doing the timeout part, we should consider doing the cancellation part as well for 5.0, to (finally) present a complete and working cancellation & timeout story in Npgsql.
Yes absolutely. My replication work will build on top of (async) cancellation so it would be good for my work to even do the cancellation part first.
Also if somebody really uses very heavy queries that should definitely stop after some time, even in case of a network partition, there's nothing stopping them from issuing a SET statement_timeout TO 123; before the actual query.
That's a very good point, and another argument why we don't need to do this in the driver. I've added it to the analysis above.
@vonzshik let's see what @YohDeadfall thinks, but I think we have a good consensus - unless something new comes from @YohDeadfall I think you can start implementation. You don't have to do both the timeout and cancellation in the same PR (in fact it's always better to split things to smaller chunks for easier reviewing).
FYI @vonzshik and others, here is the plan for timeouts in MySqlConnector by @bgrainger, which is similar to what we've done: mysql-net/MySqlConnector#455 (comment). It's good to have validation from another database driver expert that most of the decisions/arguments here are valid.
Activity
FlorianRainer commentedon Nov 17, 2017
roji commentedon Nov 18, 2017
FlorianRainer commentedon Nov 20, 2017
roji commentedon Nov 20, 2017
vonzshik commentedon Aug 19, 2020
Hello, @roji, is anyone working on this one?
roji commentedon Aug 20, 2020
@vonzshik nope - although I think @Brar is doing some timeout-related work in his logical replication branch, so this may not be the perfect time (@Brar can you confirm?).
Otherwise feel free to work on this, but it may be a good idea to discuss exactly what we want to do before starting implementation, and to consider the discussion in #2437 (this issue actually seems to be a dup of that one).
Here are some thoughts on what we should do:
/cc @Brar and @YohDeadfall it would be good to get your opinion on the above (though I know we briefly discussed this before).
vonzshik commentedon Aug 20, 2020
So, as an example:
CommandTimeoutroji commentedon Aug 20, 2020
@vonzshik yes, that more or less corresponds to what I had in mind. Some open questions:
Should we contact PG to cancel query after 25 seconds (as you suggest), or after 30? I think after 30 is better, with the Command Timeout setting being a lower bound rather than an upper bound. It would be surprising (and probably more ncorrect) if the user specifies 30 seconds, but their query gets cancelled after only 25.
If the PG cancellation succeeds, we should probably continue to throw the PostgresException with SqlState=57014, rather than TimeoutException. Besides allowing the user to distinguish the two cancellation types (and just passing along the PG response), throwing a TimeoutException would be a breaking change - existing code already expects PostgresException as a consequence of cancellation. (it's true that we would start to throw TimeoutException for the client timeout in case the PG one fails, but that seems more acceptable).
Very good point.
Anyway, I'd like to hear @YohDeadfall's and @Brar's opinions about this plan (and also to know whether @Brar thinks there's going to be a conflict with his own work), but aside from that it sounds like we're aligned.
Brar commentedon Aug 20, 2020
I consider timeouts and cancellation as related but not identical concepts.
While timeouts are probably the most common reason to trigger cancellation, it might also happen as a reaction to a GUI event (e. g. a user clicking a button to abort a long running query).
So to me #2437 is a more general thing about cancellation whereas this is more about timeouts that cause a cancellation.
While the actual implementation will overlap to a huge amount, I don't think we should mix the two terms or treat them as interchangeable.
Yes I can.
What I'm currently working on (cancellation and timeouts in replication connections) will rely on async cancellation but will not use anything around CommandTimeout so the actual overlap of our work would probably be the cancellation part. Also, since the replication API will be async only, I don't care about sync cancellation.
For this reason I think that we should, at first, talk about what we actually want regarding (async) cancellation (#2437 - e. g. wire the cancellation tokens we already have on some of our public API down to the actual socket + trigger backend cancellation first as an attempt to save the physical connection) and then both of us could probably do their thing without too much overlap.
vonzshik commentedon Aug 21, 2020
Agree, it should be 30 seconds.
Yep, we shouldn't change this behavior (at the very least in this issue).
I've had some time to think it over, and this is how I see it working:
NpgsqlReadBuffer, it should work as it is.NpgsqlConnector(I'm talking aboutwhile (true)reading loop here).NpgsqlReadBufferfails because ofCommandTimeout(doesn't matter, sync or async), we should catch it in the loop.CommandTimeout, we send request to PG to cancel query. Also, this request should have timeout different fromCommandTimeout(maybe another param inConnectionString?).Most likely I've missed some cases, where it wouldn't work (multiplexing?).
vonzshik commentedon Sep 6, 2020
@roji ping, in case you have some time to continue discussion. Or maybe I should take a look at some other issues (for example, #2282, #1810) for now?
roji commentedon Sep 6, 2020
Am just writing a response here :)
roji commentedon Sep 6, 2020
Sorry for disappearing for a while, had to deal with some personal issues. Although this issue is about timeouts, I'll try to summarize things as I see them for both timeouts and cancellation, as they're somewhat related.
User-triggered cancellation seems to be the simpler case (this is tracked by #2437).
User-managed timeout is more complicated:
statement_timeout.statement_timeoutfires after X seconds have passed, regardless of the type of time. For example, if the user code takes time in processing some row result, that time is counted in the timeout. This is very different from the socket timeouts we're using, which are only about time spent waiting for responses (on the network).statement_timeoutcompared to PG cancellation, is that if a network partition occurs at some point after the query has started, the former will cancel the query while the latter won't.statement_timeoutjust like any other PG parameter if that's what they want.In light of the above, I think we should continue not using
statement_timeout, and implement PG cancellation as already discussed in this issue. Basically, the scenario of a network partition after the query starts doesn't seem important enough compared to the quirkiness of howstatement_timeoutworks (and the subtle breakage that starting to use it would introduce).Also, as we're already considering doing the timeout part, we should consider doing the cancellation part as well for 5.0, to (finally) present a complete and working cancellation & timeout story in Npgsql.
Thoughts?
Brar commentedon Sep 6, 2020
Agreed.
Agreed.
Also if somebody really uses very heavy queries that should definitely stop after some time, even in case of a network partition, there's nothing stopping them from issuing a
SET statement_timeout TO 123;before the actual query.Yes absolutely. My replication work will build on top of (async) cancellation so it would be good for my work to even do the cancellation part first.
roji commentedon Sep 6, 2020
That's a very good point, and another argument why we don't need to do this in the driver. I've added it to the analysis above.
roji commentedon Sep 6, 2020
@vonzshik let's see what @YohDeadfall thinks, but I think we have a good consensus - unless something new comes from @YohDeadfall I think you can start implementation. You don't have to do both the timeout and cancellation in the same PR (in fact it's always better to split things to smaller chunks for easier reviewing).
YohDeadfall commentedon Sep 6, 2020
Am okay with everything said by @roji and @Brar. There is no third opinion.
roji commentedon Oct 2, 2020
FYI @vonzshik and others, here is the plan for timeouts in MySqlConnector by @bgrainger, which is similar to what we've done: mysql-net/MySqlConnector#455 (comment). It's good to have validation from another database driver expert that most of the decisions/arguments here are valid.