Skip to content

Dropped connections not throwing exception when using async reader #631

Closed

Description

@oldsaltycrab

Hello, I'm on the release-3.0.0 branch (c5f2caa).

If I use an async reader and manage to get my connection terminated by the server, my request hangs forever. If you follow the steps outlined in #630 to get a dropped connection, this becomes evident.

Using the above scenario, if I run something that queries the DB and use ExecuteReader(), the server will drop the connection and I'll eventually get an IOException thrown in NpgsqlBuffer.cs (Unable to read data from the transport connection: A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond.)

If I instead use ExecuteReaderAsync(), the application will hang. I don't know if this is a pgbouncer specific issue, but I do believe that we're seeing this in load tests that would have nothing to do with #630.

Thanks, let me know if you need more information.

Activity

  1. roji commented on Jun 3, 2015

    @roji
    Member

    I looked into the issue of async operations and timeouts a few weeks ago, I think the current behavior is correct although I'm not sure (@Emill, maybe you want to give us your opinion as well?)

    First, there's a clear warning on the SqlCommand.CommandTimeout page specifying that this property is ignored for async operations such as BeginExecuteReader. Now, BeginExecuteReader is an old-style async method (pre-4.5), so I'm not 100% sure the same thing applies to the new-style methods such as ExecuteReaderAsync; I don't have an SqlServer here to test, if anyone can help confirm this behavior that would be really great.

    Second, Npgsql has two timeouts: backend (via sending SET statement_timeout) and frontend (by setting the socket's ReceiveTimeout property. The ReceiveTimeout page has a similar comment saying that the property only applies for synchronous operations.

    In other words, it seems that asynchronous operations don't respect timeouts, neither on the socket level nor on the ADO.NET level (at least in SqlServer). This makes some sense to me: unlike with sync operations, where a thread is stuck on a kernel call, with async operations there is no thread waiting and a timeout implies an additional timer operation somewhere. It's also very easy to add a timeout to async operations like this:

    await Task.WhenAny(cmd.ExecuteNonQueryAsync(), Task.Delay(timeout));
    

    You can also wrap this in an extension method - await cmd.ExecuteNonQueryAsync().WithTimeout(timeout) - I routinely do this. Of course none of this is possible for synchronous operations.

    Let me know if you think my understanding of the specs/desired behavior is wrong, I'm not 100% sure about it either.

  2. oldsaltycrab commented on Jun 3, 2015

    @oldsaltycrab
    Author

    Thanks @roji, it's clear that the behaviour I was expecting is only found in the synchronous versions of these calls, I think you did it correctly. Great tip with the task timeout.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions