Skip to content

Fully honor timeouts during connection phase - #3229

Merged
roji merged 1 commit into
npgsql:mainfrom
roji:ConnectionTimeout
Oct 12, 2020
Merged

roji merged 1 commit into
npgsql:mainfrom
roji:ConnectionTimeout

Conversation

@roji

@roji roji commented Oct 10, 2020

Copy link
Copy Markdown
Member

Improve PG mocking infrastructure to support connection-time scenarios.

@russellfoster while working on the PostgreSQL mocking infrastructure for testing the issue, I ended up doing the fix as well.

Fixes #3227
Replaces #3226

/cc @russellfoster @vonzshik

@roji
roji requested a review from YohDeadfall as a code owner October 10, 2020 08:41
@roji

roji commented Oct 10, 2020

Copy link
Copy Markdown
Member Author

Two additional notes - this isn't yet ready for merging.

  • The connection timeout is generally treated as aggregate, for the entire connection attempt, but this PR simply sets the UserTimeout to whatever is left once Connect completes. In other words, if after Connect there are several long operations during authentication (or other post-Connect reads), the timeout is applied each time and we can run over. I don't think that's a critical issue, but I'll look into it.
  • Up to now we've done various client-side checks for whether the timeout has expired (NpgsqlTimeout.Check) - this was done mainly because we didn't have async timeouts, which we do now. I'll look at removing the client-side checks entirely and only rely on I/O timeouts which are now fully implemented.

@roji
roji marked this pull request as draft October 10, 2020 08:57
Comment thread src/Npgsql/NpgsqlConnector.cs Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UserTimeout only holds current timeout. Buffer's timeout is set during connector's ReadMessage.
Also, the connector sets the timeout only for the ReadBuffer.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

UserTimeout only holds current timeout. Buffer's timeout is set during connector's ReadMessage.

Right, but the first time that a message will be read (i.e. for authentication) the timeout will be properly set on the read buffer - just like the regular flow when executing a command (that's why the test passes). In fact, if we don't set UserTimeout, the test fails because ReadMessage sets the buffer's timeout from it (and it means "no timeout").

Also, the connector sets the timeout only for the ReadBuffer.

That's true, I'll set the write timeout here as well. The design is currently smelly, in that the read timeout is set automatically from NpgsqlConnector.UserTimeout (in ReadMessage), but the write timeout is set via a completely different mechanism. This is mainly because reading always goes through one place (ReadMessage), but writing can happen via various different places.

Even though it's a code smell, I propose to leave it as it is as it doesn't have an actual impact - we can always clean up later if we want.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, but the first time that a message will be read (i.e. for authentication) the timeout will be properly set on the read buffer - just like the regular flow when executing a command (that's why the test passes).

But it wouldn't be set for reading a response for the ssl request just below.

Even though it's a code smell, I propose to leave it as it is as it doesn't have an actual impact - we can always clean up later if we want.

It's definitely something we should take another look. But I agree, that it can (and should) be done later.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But it wouldn't be set for reading a response for the ssl request just below.

Good catch. I've updated to apply the timeout both directly to the read buffer, and also to UserTimeout.

@roji
roji marked this pull request as ready for review October 10, 2020 17:09
@roji
roji requested a review from vonzshik October 10, 2020 17:09

@vonzshik vonzshik left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! A few small changes, and it should be ok to merge.

Comment thread test/Npgsql.Tests/ConnectionTests.cs Resolved
@roji
roji requested a review from vonzshik October 11, 2020 14:09
Improve PG mocking infrastructure to support connection-time
scenarios.

Fixes npgsql#3227
@roji
roji force-pushed the ConnectionTimeout branch from 2a14489 to bfdacc8 Compare October 12, 2020 09:58
@roji
roji merged commit e780f90 into npgsql:main Oct 12, 2020
@roji
roji deleted the ConnectionTimeout branch October 12, 2020 10:28
@russellfoster

Copy link
Copy Markdown

Tested on my system and the sync Open works as expected:

10/13/20 09:44:15.643 [10] [Debug] Npgsql.LogDebug: Authenticating...
10/13/20 09:45:15.480 [10] [Error] Npgsql.LogError: Breaking connector
Npgsql.NpgsqlException (0x80004005): Exception while reading from stream ---> System.TimeoutException: Timeout during reading attempt

Hooray! One thing: the OpenAsync does not. Is there a different way to apply a timeout for async? I know the socker ReceiveTimeout is not applicable for this. May something similar to my hack in #3227 (but no need for reflection)?

@roji

roji commented Oct 13, 2020

Copy link
Copy Markdown
Member Author

@russellfoster this PR was definitely supposed to address async timeouts as well. Which version of .NET Core are you using? Cancellation tokens (which are used for timeouts) are only honored in recent versions.

@vonzshik

Copy link
Copy Markdown
Contributor

Cancellation tokens (which are used for timeouts) are only honored in recent versions.

That is, .net 5 for the Socket.ConnectAsync, and I believe .net core 3.0 (or 3.1) for the NetworkStream.ReadAsync (and WriteAsync).

@russellfoster

Copy link
Copy Markdown

OK, dependant on Net version. I'm on 4.6.1. Yes old, I know :)

@roji

roji commented Oct 13, 2020

Copy link
Copy Markdown
Member Author

@russellfoster yeah, that would be the reason. Definitely consider moving to .NET Core!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Timeouts not fully applied when opening connections

3 participants