Repository navigation
Description
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
roji commentedon Jul 3, 2016
@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. ThedontBreakOnTimeoutsis a special case used only when waiting for asynchronous notifications viaNpgsqlConnection.Wait(timeout), in which case a timeout is a normal non-breaking event.We have test
CommandTests.Timeoutwhich does pretty much what you suggest, running a long command with timeout 1, and the test passes. Are you seeing some sort of issue?shortspider commentedon Jul 3, 2016
@roji I made a mistake. I read the
dontBreakOnTimeoutsvariable 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
PostgresExceptionwas not thrown. While digging into it I found thedontBreakOnTimeoutsvariable and it looked to me like it was being used backwards which is why I opened the issue.CommandTests.Timeoutdoes 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 atNpgsqlException.InnerExceptionto see if it's anIOExceptionand then see if that exception is aSocketExceptionwith a timeout code correct?Thank you for the quick response.
roji commentedon Jul 3, 2016
Yes, if you specifically want to check for a timeout, you're going to have a to look at
NpgsqlException.InnerExceptionmuch 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.