Repository navigation
First implementation of frontend timeouts for async methods - #2911
Conversation
|
@vonzshik @wes-novack apologies for not reviewing this earlier, am under a lot of load at the moment. I'll do my best to set aside some time soon for this. |
|
@roji Thank you for the update, just wanted to also express the desire for this and gratitude for the time you spend on this. Looking forward to seeing what happens here |
|
@roji Thank you 😃 |
YohDeadfall
left a comment
There was a problem hiding this comment.
Please rebase on the top of dev branch and resolve a conflict.
9c61833 to
9f77ec5
Compare
|
Looked at failing test for Issue2257, and found at least 2 problems:
using (var cmd = conn.CreateCommand())
{
cmd.CommandTimeout = 60;
//....
}
|
roji
left a comment
There was a problem hiding this comment.
High-level notes
- There are many direct calls into NpgsqlReadBuffer.Ensure that don't pass through NpgsqlConnector.ReadMessage (see all the type handlers). These should also get the timeout.
- To fix the above and to get a cleaner implementation all-around, it should be possible to implement the entire mechanism inside NpgsqlReadBuffer.Ensure, rather than at the upper layer in NpgsqlConnector.ReadMessage. NpgsqlReadBuffer could have a Timeout property set as appropriate, and when Ensure is called, that timeout would be applied appropriately for either sync (by setting Socket.ReceiveTimeout if needed), or async (via the cancellation token).
- We should reuse the cancellation token source and avoid re-allocating one for each async operation (as long as it hasn't fired). You can see an example of this reuse in the multiplexing write loop.
- Timeouts are currently applied only on reading, not on writing - that should also be done (should be a very similar implementation)
Some general related notes
- Just above in NpgsqlConnector.ReadMessageLong, we read prepended responses (these are PG responses for messages unrelated to this query which were prepended to it for efficiency). We have a feature where we allow a different timeout to be defined for those (InternalCommandTimeout instead of UserTimeout), but it seems to be broken: if there are prepended responses, we set ReceiveTimeout to InternalcommandTimeout, but then recursively call ReadMessageLong which will overwrite it with UserTimeout. Opened #3045 to look at that at some point (no need to do it now).
- The timeout is implemented for each read operation, not to the operation as a whole. That's OK, because this is also how sync frontend timeouts are implemented (see #1041 which is about possibly changing this).
- The user-provided cancellation token currently triggers server-side cancellation (dotnet/runtime#28596 (comment)), so there's no conflict/interference with implementing the socket timeout via an internal cancellation token as is done in this PR. But we may have to revisit this at some point.
|
Thank you for review, @roji I just pushed a few commits about moving timeout to |
roji
left a comment
There was a problem hiding this comment.
Thanks @vonzshik, this is getting close to mergeable - please take a look at the comments, and also at the remaining comments from the last review. BTW when a review comment has been addressed, it's good to mark it as "resolved" to let us know and help the next review.
|
@roji Thank you again for review. Just pushed commits concerning every requested change, so feel free to review when you have time.
I'll keep than in mind. |
Brar
left a comment
There was a problem hiding this comment.
This looks pretty good to me.
I've got a few minor comments and suggestions but don't see any real problems with this PR.
This reverts commit 54683e6.
|
@roji this really looks ready to me now. |
YohDeadfall
left a comment
There was a problem hiding this comment.
Only think I don't like is the inheritance from IDisposable, but it's not a hill I want to die on.
What I would like to see is this changes in our main branch, so can replicate the logic in new type handling am working on.
|
Sorry for taking so long with this - I promise I'll give this a review today or tomorrow for sure. It was already pretty close to good last time I looked, so feel free to base your replication work off of this if you want, in the meantime. |
|
|
||
| void BeginSend(NpgsqlConnector connector) | ||
| { | ||
| connector.WriteBuffer.Timeout = TimeSpan.FromSeconds(CommandTimeout); |
There was a problem hiding this comment.
It's worth mentioning that Npgsql didn't previously set a sending timeout (sync or async). It's definitely a good thing to have, but is beyond the scope of purely adding async timeouts.
On an unrelated note, our read/write timeouts currently leak from non-multiplexed to multiplexed operations. This is unrelated to this PR, I've opened #3110 to track.
| /// <summary> | ||
| /// Contains the current value of the Socket's RecieveTimeout, used to determine whether | ||
| /// we need to change it when commands are received. | ||
| /// It's used only for sync IO (<see cref="Stream.Read(byte[], int, int)"/>) |
There was a problem hiding this comment.
This comment doesn't seem right - isn't _currentSocketTimeout is used for async timeouts in Ensure (via the Timeout property?)
There was a problem hiding this comment.
Yes, it'not entirely correct. Rewrote it + renamed _currentSocketTimeout to _currentTimeout.
| } | ||
|
|
||
| // Resetting cancellation token source, so we can use it again | ||
| if (async) |
There was a problem hiding this comment.
It generally feels like this unified sync/async method branches a lot on async (i.e. lots of stuff specific only to one path but not the other). This may be an indication that it should be refactored into two separate methods (which possibly use some common logic in a separate inlined method).
But let's not hold this PR up on that, we can look at refactoring later.
|
Thank you for review @roji, it was fun making this pr 😃 . |
|
Thanks again @vonzshik, it's great to see this functionality making it into the next version. It would be great to have more contributions from you! There's nothing in particular to know, you can look through the issues and see something you find interesting. It's definitely better if you can find something that has the 5.0 milestone on it, though if you really want to work on a backlog item that's OK too. It's usually a good idea to post a comment before starting to work, so that we don't end up working on the same thing, and also so that you pick something that isn't in conflict with ongoing work (or just too complicated). Basically communication is the key :) |
|
Did timeouts for async get to some version? |
|
@freerider7777 yep, 5.0 |
|
Thanks, seems sync version also works much better now - I've tested 4.1.8 (lock the table and simple select from EF Core) - when timeout happens, the query continues to run on Postgres (eating resources!!! db admins don't like this), updating to 5.0.3 - I see the query terminates after timeout! Cool. |
|
async methods though do not terminate... Maybe that's because we use .Net core 2.2 for now with old version Npgsql.EntityFrameworkCore.PostgreSQL 2.2.4 + NpgSql |
|
@freerider7777 yeah, I believe, cancellation for the async methods of the |
Took a stab at #607.
While it's relatively simple fix (on the first glance), I may have missed quite a few corner cases.
What I'm definitely not sure is an exception I throw (took inspiration from NpgsqlConnector.ConnectAsync).
Also, maybe timeout exceptions for sync and async Read should be the same?