You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Opening a seperate issue for the problem described in this comment of issue #400.
What I mean by "same connection/command/datareader" is that it's always happening to my 3rd connection so far. Each of my connection/command/datareader reads from a different table, but from the same database on the same database server.
I'm starting to suspect that it's a multithreading issue, but I can't confirm right now because I'm away from my work computer.
What I have been doing is setup the connection/command/datareader in an IEnumerator<T>, which is iterated over by a System.Timers.Timer.Elapsed event handler. So every time NpgsqlDataReader.Read() is called, it can be executed in any one of the threads in the System.Threading.ThreadPool. Of course, I have ensured that the Read()s happen sequentially using C#'s lock construct, but I suspect that Npgsql objects are not designed to be usable in multiple threads even if I make sure my Read() calls don't overlap.
If that's the case, is there a sane way for the Npgsql objects to detect if they are being used in multiple threads and throw a more telling exception, instead of a timeout exception?
Calling Read() on the same reader for multiple threads shouldn't be a problem, as long as it's not done concurrently of course (you're already protecting against that with a lock).
I don't know enough about what you're doing, but your 3 concurrent connections may be having an impact on one another - the first two connections may be causing the 3rd to be particularly slow. It seems that if a query's results aren't consumed within PostgreSQL's statement_timeout, a timeout occurs (I want to verify this with the PG team).
The timeout occurs if the result hasn't been consumed within the time limit I think, which over tcp means that the server has sent the last response. Since buffering is used in both a tcp client and server, the result is considered consumed when the backend has sent the last response.
@Emill yeah, that's the way I understand it too, although it is not at all what I thought statement_timeout did. I sent a message to pgsql-hackers about it.
The pgsql-hackers confirm this behavior (see the thread).
I also found the following documentation on the SqlClient's CommandTimeout:
This property is the cumulative time-out (for all network packets that are read during the invocation of a method) for all network reads during command execution or processing of the results. A time-out can still occur after the first row is returned, and does not include user processing time, only network read time.
For example, with a 30 second time out, if Read requires two network packets, then it has 30 seconds to read both network packets. If you call Read again, it will have another 30 seconds to read any data that it requires.
So first of all, with SQL Server you have a somewhat similar situation of CommandTimeout covering result consumption as well as processing - and so a timeout can occur after the first row is returned. However, it covers only network reads, which of course is something that statement_timeout can never do because it's enforced at the backend.
It seems we should add some very clear documentation on this, but otherwise there's nothing much that can be done... @ksze, as to why your third connection happens to timeout in your specific scenario, that's a different question of course.
Thinking about this some more, there is something we can do...
If we really want to, we can stop using statement_timeout altogether, and enforce timeouts on the client-side only. We could aggregate network reads like SqlClient, and once the timeout occurs we could cancel the query, consume all remaining results, and throw a timeout exception.
Of course this is a pretty big change... I'm not even sure that switching the statement_timeout behavior for a more SqlClient-compatible one is important/correct - after all this happens to be the SqlClient behavior, and not some sort of clear ADO.NET standard thing...
Ok, I just wrote a simple test program and I am now sure it's not an issue with multithreading: the timeout exception occurs even if I have only one connection/command/datareader and do a while (reader.Read()) { Thread.Sleep(10); } in the main thread of a console application. The exception was thrown after about 11k rows.
@ksze, yes, I confirmed this as well in the comments above. The reason for this is that Npgsql implement ADO.NET's CommandTimeout by sending the statement_timeout parameter to PostgreSQL (note that since 3.0 CommandTimeout also has a frontend timeout implemented on the socket). And statement_timeout includes the time spent processing the results, so if you sleep on the client-side a timeout will happen.
I agree this isn't a great situation, but apart from abandoning statement_timeout nothing can be done. Npgsql 3.0 allows you to disable the use of statement_timeout by setting Backend Timeouts=false on the connection string (just updated the docs).
The only thing we can do, it seems, is to drop using statement_timeout altogether and switch to frontend-only timeouts (see proposal in this comment). Otherwise this should probably be documented. Any comments on this @Emill?
I don't like timeouts at all :)
That's probably the reason statement_timeout is disabled by default in PostgreSQL. If the query takes to long to process, you should rather try to optimize the query. If there are connection problems, you will eventually get I/O errors anyway from the TCP layer.
Yeah, I agree with you about timeouts. Take a look at the responses I got in pgsql-hackers, IMHO it means that statement_timeout has very little value as a timeout mechanism.
I still think there's value in a frontend-only timeout like SqlClient does. We can do like them and aggregate total network read time when processing a query (right now the frontend timeout doesn't aggregate). If a frontend timeout occurs, we currently break the connection which is a bit violent; instead we can send a cancel request and read the rest of the responses. Does that sound like a good plan to you?
There seems to be a pretty clear consensus that we should drop backend timeouts (i.e. PostgreSQL's statement_timeout parameter) as a default way of implementing ADO.NET CommandTimeout. These may cover frontend processing time and simply aren't reliable enough for what we want to achieve.
This will also simplify the codebase somewhat, setting backend timeouts implies a bit of complexity.
Note that users are obviously free to manually set statement_timeout if they wish.
Activity
roji commentedon Aug 8, 2015
Calling
Read()on the same reader for multiple threads shouldn't be a problem, as long as it's not done concurrently of course (you're already protecting against that with a lock).I don't know enough about what you're doing, but your 3 concurrent connections may be having an impact on one another - the first two connections may be causing the 3rd to be particularly slow. It seems that if a query's results aren't consumed within PostgreSQL's
statement_timeout, a timeout occurs (I want to verify this with the PG team).Emill commentedon Aug 8, 2015
The timeout occurs if the result hasn't been consumed within the time limit I think, which over tcp means that the server has sent the last response. Since buffering is used in both a tcp client and server, the result is considered consumed when the backend has sent the last response.
roji commentedon Aug 8, 2015
@Emill yeah, that's the way I understand it too, although it is not at all what I thought
statement_timeoutdid. I sent a message to pgsql-hackers about it.roji commentedon Aug 8, 2015
The pgsql-hackers confirm this behavior (see the thread).
I also found the following documentation on the SqlClient's CommandTimeout:
So first of all, with SQL Server you have a somewhat similar situation of CommandTimeout covering result consumption as well as processing - and so a timeout can occur after the first row is returned. However, it covers only network reads, which of course is something that
statement_timeoutcan never do because it's enforced at the backend.It seems we should add some very clear documentation on this, but otherwise there's nothing much that can be done... @ksze, as to why your third connection happens to timeout in your specific scenario, that's a different question of course.
Any other thoughts?
roji commentedon Aug 8, 2015
Thinking about this some more, there is something we can do...
If we really want to, we can stop using
statement_timeoutaltogether, and enforce timeouts on the client-side only. We could aggregate network reads like SqlClient, and once the timeout occurs we could cancel the query, consume all remaining results, and throw a timeout exception.Of course this is a pretty big change... I'm not even sure that switching the
statement_timeoutbehavior for a more SqlClient-compatible one is important/correct - after all this happens to be the SqlClient behavior, and not some sort of clear ADO.NET standard thing...ksze commentedon Aug 10, 2015
Ok, I just wrote a simple test program and I am now sure it's not an issue with multithreading: the timeout exception occurs even if I have only one connection/command/datareader and do a
while (reader.Read()) { Thread.Sleep(10); }in the main thread of a console application. The exception was thrown after about 11k rows.roji commentedon Aug 10, 2015
@ksze, yes, I confirmed this as well in the comments above. The reason for this is that Npgsql implement ADO.NET's
CommandTimeoutby sending thestatement_timeoutparameter to PostgreSQL (note that since 3.0CommandTimeoutalso has a frontend timeout implemented on the socket). Andstatement_timeoutincludes the time spent processing the results, so if you sleep on the client-side a timeout will happen.I agree this isn't a great situation, but apart from abandoning
statement_timeoutnothing can be done. Npgsql 3.0 allows you to disable the use ofstatement_timeoutby settingBackend Timeouts=falseon the connection string (just updated the docs).The only thing we can do, it seems, is to drop using
statement_timeoutaltogether and switch to frontend-only timeouts (see proposal in this comment). Otherwise this should probably be documented. Any comments on this @Emill?Emill commentedon Aug 10, 2015
I don't like timeouts at all :)
That's probably the reason statement_timeout is disabled by default in PostgreSQL. If the query takes to long to process, you should rather try to optimize the query. If there are connection problems, you will eventually get I/O errors anyway from the TCP layer.
roji commentedon Aug 10, 2015
Yeah, I agree with you about timeouts. Take a look at the responses I got in pgsql-hackers, IMHO it means that
statement_timeouthas very little value as a timeout mechanism.I still think there's value in a frontend-only timeout like SqlClient does. We can do like them and aggregate total network read time when processing a query (right now the frontend timeout doesn't aggregate). If a frontend timeout occurs, we currently break the connection which is a bit violent; instead we can send a cancel request and read the rest of the responses. Does that sound like a good plan to you?
franciscojunior commentedon Aug 10, 2015
I agree with frontend timeout only. I also think statement_timeout isn't
very suitable to our case.
[-]Timeout exception after a few thousand NpgsqlDataReader.Read() calls.[/-][+]Remove backend timeouts[/+]roji commentedon Apr 17, 2016
There seems to be a pretty clear consensus that we should drop backend timeouts (i.e. PostgreSQL's
statement_timeoutparameter) as a default way of implementing ADO.NET CommandTimeout. These may cover frontend processing time and simply aren't reliable enough for what we want to achieve.This will also simplify the codebase somewhat, setting backend timeouts implies a bit of complexity.
Note that users are obviously free to manually set
statement_timeoutif they wish.