Skip to content

Remove backend timeouts #689

Closed
Closed

Description

@ksze

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?

Activity

  1. roji commented on Aug 8, 2015

    @roji
    Member

    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).

  2. Emill commented on Aug 8, 2015

    @Emill
    Contributor

    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.

  3. roji commented on Aug 8, 2015

    @roji
    Member

    @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.

  4. roji commented on Aug 8, 2015

    @roji
    Member

    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.

    Any other thoughts?

  5. roji commented on Aug 8, 2015

    @roji
    Member

    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...

  6. ksze commented on Aug 10, 2015

    @ksze
    Author

    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.

  7. roji commented on Aug 10, 2015

    @roji
    Member

    @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?

  8. Emill commented on Aug 10, 2015

    @Emill
    Contributor

    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.

  9. roji commented on Aug 10, 2015

    @roji
    Member

    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?

  10. franciscojunior commented on Aug 10, 2015

    @franciscojunior
    Member

    I agree with frontend timeout only. I also think statement_timeout isn't
    very suitable to our case.

  11. changed the title [-]Timeout exception after a few thousand NpgsqlDataReader.Read() calls.[/-] [+]Remove backend timeouts[/+] on Apr 17, 2016
  12. added this to the milestone on Apr 17, 2016
  13. roji commented on Apr 17, 2016

    @roji
    Member

    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.

  14. self-assigned this
    on Apr 17, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions