Skip to content

* Fixed an issue where reads in NpgsqlConnector.Open were never timin… - #3226

Closed
russellfoster wants to merge 1 commit into
npgsql:hotfix/3.2.8from
russellfoster:hotfix/3.2.8
Closed

russellfoster wants to merge 1 commit into
npgsql:hotfix/3.2.8from
russellfoster:hotfix/3.2.8

Conversation

@russellfoster

Copy link
Copy Markdown

Hello, I fixed an issue that I ran into:

  1. The server is in a low memory state.
  2. The connection is accepted, but:
  3. The first read never completes.

In this situation, Npgsql will hang on Connection.Open(). I tried this with the latest (4.6.1), and it occurs there as well. The patch I have is for 3.2.7. Here is the related stack trace, the last op being where it was hung:

[Managed to Native Transition]	
System.dll!System.Net.Sockets.Socket.Receive(byte[] buffer, int offset, int size, System.Net.Sockets.SocketFlags socketFlags, out System.Net.Sockets.SocketError errorCode)	Unknown
System.dll!System.Net.Sockets.Socket.Receive(byte[] buffer, int offset, int size, System.Net.Sockets.SocketFlags socketFlags)	Unknown
System.dll!System.Net.Sockets.NetworkStream.Read(byte[] buffer, int offset, int size)	Unknown
Npgsql.dll!Npgsql.ReadBuffer.Ensure(int count, bool async, bool dontBreakOnTimeouts)	Unknown
Npgsql.dll!Npgsql.NpgsqlConnector.DoReadMessage(bool async, Npgsql.DataRowLoadingMode dataRowLoadingMode, bool readingNotifications, bool isPrependedMessage)	Unknown
Npgsql.dll!Npgsql.NpgsqlConnector.ReadMessage(bool async, Npgsql.DataRowLoadingMode dataRowLoadingMode, bool readingNotifications)	Unknown
[Resuming Async Method]	
mscorlib.dll!System.Runtime.CompilerServices.AsyncTaskMethodBuilder<System.__Canon>.Start<Npgsql.NpgsqlConnector.<ReadMessage>d__156>(ref Npgsql.NpgsqlConnector.<ReadMessage>d__156 stateMachine)	Unknown
Npgsql.dll!Npgsql.NpgsqlConnector.ReadExpecting<Npgsql.BackendMessages.AuthenticationRequestMessage>(bool async)	Unknown
Npgsql.dll!Npgsql.NpgsqlConnector.Authenticate(string username, Npgsql.NpgsqlTimeout timeout, bool async, System.Threading.CancellationToken cancellationToken)	Unknown
Npgsql.dll!Npgsql.NpgsqlConnector.Open(Npgsql.NpgsqlTimeout timeout, bool async, System.Threading.CancellationToken cancellationToken)	Unknown
Npgsql.dll!Npgsql.ConnectorPool.AllocateLong(Npgsql.NpgsqlConnection conn, Npgsql.NpgsqlTimeout timeout, bool async, System.Threading.CancellationToken cancellationToken)	Unknown
Npgsql.dll!Npgsql.NpgsqlConnection.Open(bool async, System.Threading.CancellationToken cancellationToken)	Unknown
Npgsql.dll!Npgsql.NpgsqlConnection.Open()	Unknown

One thing I noticed is there are no write timeouts. I guess this is by design? I did not see it hang on writes, but wanted to cover all the bases to make sure we don't see this in the field somewhere.

@YohDeadfall

Copy link
Copy Markdown
Contributor

This version is pretty outdated, so not supported anymore. Try to update to 4.1.5 or wait a bit more for 5.0.0 which will support socket level timeouts.

@russellfoster

Copy link
Copy Markdown
Author

It occurs in 4.1.6 as well.

@YohDeadfall

Copy link
Copy Markdown
Contributor

@vonzshik, @roji may you take a look at this issue? The fix can be used for 4.1.6 as well, but I don't remember all nuances.

@russellfoster you may be interested in trying unreleased 5.0.0 which we are working on. It has the feature you want as other cancellation related things too.

@russellfoster

Copy link
Copy Markdown
Author

Ok, will do. Thanks!

@vonzshik

vonzshik commented Oct 9, 2020 •

Copy link
Copy Markdown
Contributor

Well, it should be enough just to set timeouts for buffers after they're created here:

ReadBuffer = new NpgsqlReadBuffer(this, _stream, _socket, Settings.ReadBufferSize, TextEncoding, RelaxedTextEncoding);
WriteBuffer = new NpgsqlWriteBuffer(this, _stream, _socket, Settings.WriteBufferSize, TextEncoding);

And also set the connector's UserTimeout.

@vonzshik

vonzshik commented Oct 9, 2020

Copy link
Copy Markdown
Contributor

But yeah, both 5.0 and 4.1.6 should have this problem.

@roji roji left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

@russellfoster thanks for reporting this.

First, this PR includes a lot of unrelated indentation/whitespace changes - please revert those.

Second, I agree with @vonzshik's comment that it should be enough to just set the timeout as usual once the buffers are created - this is a simpler approach and more in-line with how things are done in the driver (in older versions this means setting NpgsqlConnector.ReceiveTimeout, in 5.0 it's setting timeout on the buffers themselves which manage the timeouts).

For 5.0 (main), we also need a regression test for this, using the new PostgreSQL mocking infrastructure. As this is new and not trivial, I'll take care of it.

Finally, we usually apply fixes to main (5.0) first, and then backport to older versions. I'd suggest starting a new PR against main, and I can take care of backporting later. Does that sound good?

Comment thread src/Npgsql/NpgsqlConnector.cs Resolved
@roji

roji commented Oct 10, 2020

Copy link
Copy Markdown
Member

Note: I've opened #3227 as a tracking issue for this.

@roji

roji commented Oct 12, 2020

Copy link
Copy Markdown
Member

Superceded by #3229

@roji roji closed this Oct 12, 2020
@russellfoster
russellfoster deleted the hotfix/3.2.8 branch October 12, 2020 12:10
@russellfoster
russellfoster restored the hotfix/3.2.8 branch October 12, 2020 12:14
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.

4 participants