Skip to content

Timeout Exception Not Thrown When Command Times Out #1186

Closed

Description

@shortspider

Previously when you ran a command that took longer then the provided timeout you got an exception with a Postgres error code. Per #689 that will not happen anymore because Npgsql doesn't set a back-end timeout. However, I would still expect to get a timeout exception from the network layer.

Looking at ReadBuffer, the exception handling in Ensure was changed by 610e319. The code now looks like this:

internal void Ensure(int count, bool dontBreakOnTimeouts=false)
{
    Contract.Requires(count <= Size);
    count -= ReadBytesLeft;
    if (count <= 0) { return; }
    if (ReadPosition == _filledBytes) {
            Clear();
    } else if (count > Size - _filledBytes) {
            Array.Copy(_buf, ReadPosition, _buf, 0, ReadBytesLeft);
            _filledBytes = ReadBytesLeft;
            ReadPosition = 0;
    }

    try
    {
        while (count > 0)
        {
            var toRead = Size - _filledBytes;
            var read = Underlying.Read(_buf, _filledBytes, toRead);
            if (read == 0)
                  throw new EndOfStreamException();
            count -= read;
            _filledBytes += read;
        }
    }
    // We have a special case when reading async notifications - a timeout may be normal
    // shouldn't be fatal
    catch (IOException e) when (
        dontBreakOnTimeouts && (e.InnerException as SocketException)?.SocketErrorCode == SocketError.TimedOut
    )
    {
        throw new TimeoutException("Timeout while reading from stream");
    }
    catch (Exception e)
    {
        Connector.Break();
        throw new NpgsqlException("Exception while reading from stream", e);
    }
}

So if the command takes too long to return a result a TimoutException should be thrown. However I'm not seeing a timeout exception, instead I see a NpgsqlException with the text 'Exception while reading from stream'. The inner exception is a socket timeout exception.

Looking at the code in Ensure, is the us of dontBreakOnTimeouts backwards? As in, right now it says 'if we are not breaking on timeouts and the exception is a socket timeout exception throw a timeout exception' whereas it should say 'if we are not not breaking on timeouts and the exception is a socket timeout throw a timeout exception'?

To see this, create a NpgsqlCommand, set the timeout to 1 and run any command that would take longer than a second.

@roji can you comment? If you agree I can submit a patch.

Activity

  1. roji commented on Jul 3, 2016

    @roji
    Member

    @shortspider, I'm not exactly sure what you're reporting...

    In 3.1, #897 was made, which means we wrap all I/O errors (including timeouts) in NpgsqlException rather than throwing TimeoutException directly. This seems to be the standard behavior in other ADO.NET providers, and it makes it much easier to catch any database-related exception in user code, distinguishing it from other non-database I/O errors.

    So the normal code flow for timeouts (or other I/O errors) is to throw the catch (Exception e): the connection is set to state broken, the I/O exception is wrapped inside an NpgsqlException and thrown. The dontBreakOnTimeouts is a special case used only when waiting for asynchronous notifications via NpgsqlConnection.Wait(timeout), in which case a timeout is a normal non-breaking event.

    We have test CommandTests.Timeout which does pretty much what you suggest, running a long command with timeout 1, and the test passes. Are you seeing some sort of issue?

  2. shortspider commented on Jul 3, 2016

    @shortspider
    ContributorAuthor

    @roji I made a mistake. I read the dontBreakOnTimeouts variable as a flag used to prevent timeout exceptions from being thrown but it looks like it's used to do the opposite.

    I think my problem has been caused by not knowing about #689. After updating Npgsql in my application some tests failed because a PostgresException was not thrown. While digging into it I found the dontBreakOnTimeouts variable and it looked to me like it was being used backwards which is why I opened the issue.

    CommandTests.Timeout does almost exactly what my test does and gets the result that I am getting. I will have to update the test on my end to handle this correctly. So now if we want to determine if there is a timeout we have to look at NpgsqlException.InnerException to see if it's an IOException and then see if that exception is a SocketException with a timeout code correct?

    Thank you for the quick response.

  3. roji commented on Jul 3, 2016

    @roji
    Member

    Yes, if you specifically want to check for a timeout, you're going to have a to look at NpgsqlException.InnerException much like the test does. It's a bit clunky but it seems like the right thing to do.

    Let me know if you run into anything.

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