Skip to content

Command Timeout Behaviour from 2.X -> 3.X #1551

Closed

Description

@msroest

I just have a question regarding the changed handling of Command Timeouts coming from the 2.x series. We're currently using a heavily in house modified version of Npgsql 2.x that we're trying to move away from in our endeavour to get our projects up to date. This involves moving all projects to a modern version of .Net and off custom home grown versions as much as possible. We have a concern with the changed command timeout behaviour however. In npgsql 2.x on a command timeout the driver would open a new connection to the server and forcibly cancel the query on the server, the new behaviour of relying on a socket timeout will allow the queries to continue to run on the server after the client has stopped waiting on them. This will allow queries that no longer have any chance of returning a useful result to continue to impact the server. Is there any plans to reintroduce the previous behaviour/would a PR optionally reintroducing this behaviour be something the maintainers would be willing to merge?

Thanks

Activity

  1. self-assigned this
    on May 17, 2017
  2. roji commented on May 17, 2017

    @roji
    Member

    Npgsql has indeed moved away from using backend timeouts (PostgreSQL's statement_timeout parameter) for enforcing CommandTimeout. Both frontend (socket) timeouts and backend timeouts have their advantages and disadvantages, and as you say frontend timeouts don't guarantee that the query will actually be cancelled at the backend. On the other hand, backend timeouts won't affect your client in the case of network failure, leaving your client application blocking forever.

    The good news is that you don't need Npgsql's help to turn on backend timeouts again: simply set it yourself (SET statement_timeout=30). This was one of the reasons for the switch: while frontend timeouts require specific Npgsql support (because Npgsql owns and manages the socket), backend timeouts are a PostgreSQL feature that's accessible to users without any special support from Npgsql.

    Note that in the general case you shouldn't need this. When a socket timeout occurs, Npgsql will simply close the socket and break the connection. Then, whenever the server attempts to write any data to the client, it should get a failure and the query should get cancelled immediately. However, it is true that if you're invoking a very long wait without making the server write any responses right away, the client-side socket timeout will have no effect on the server (and it makes sense to use statement_timeout).

    Hope this answers everything for you. Am closing the issue, but feel free to continue the conversation.

  3. msroest commented on May 18, 2017

    @msroest
    Author

    Hi @roji,
    Thanks for the reply, I read all the threads about removing the statement_timeout as the method for command timeout and it all made sense (especially how streaming the data back is included in the time of the statement_timeout). In our case we don't especially care about how long the data takes to come back from the server as that portion generally doesn't put a bunch of load on the server. It's more the actual execution and beginning to return data that is the problem.

    The functionality that we're really looking for is what 2.X had vs the statement_timeout method of 3.X. In the 2.X there was https://github.com/npgsql/npgsql/blob/v2.2.7/Npgsql/Npgsql/NpgsqlCancelRequest.cs which explicitly went to the server with a new connection and cancelled the other running PID. This allowed the behaviour of the command timeout corresponding to exactly the amount of time to allow the execution of the command but not including the data transfer time.

    Looking at the other threads about statement_timeout it doesn't seem like this usage pattern will be possible with Npgsql 3.x hence the question about if there is any plans to return the explicit cancel behavior controlled directly by the client. Or if there is nothing in the roadmap if the team would be open to a PR that includes this behaviour (optionally via a connection string configuration).

    Thanks

  4. roji commented on May 18, 2017

    @roji
    Member

    Command cancellation is present in 3.x just like it was in 2.x - you simply call NpgsqlCommand.Cancel(). This will contact the backend and immediately trigger a cancellation of the currently-running command, just like before.

    Unless I'm mistaken, there was never a feature where timeouts were implemented with this cancellation mechanism - they were always implemented with statement_timeout until that was dropped in favor of a socket timeout. There has been a suggestion to implement timeout as cancellation + socket timeout - this would handle your case as well but is a bit complex (I don't have the issue number right now).

    For now, nothing is preventing you from setting statement_timeout yourself, or even manually canceling the command after some time has elapsed (which would have a similar effect, but be more complicated to implement).

  5. msroest commented on May 18, 2017

    @msroest
    Author

    Sorry I was looking for this last night when I was at home but didn't have access to my full setup.

    This is where it is sending cancel on timeout in 2.x:
    https://github.com/npgsql/npgsql/blob/support/2.x/Npgsql/Npgsql/NpgsqlState.cs#L309

    I think this issue is the one you're referring too is
    #1344
    This issue where the backend timeouts were proposed/implemented to be removed also talks about doing timeouts on the client side with socket+cancel.
    #689

    I'll doing some playing around with statement_timeout. We don't tend to do a bunch of processing while reading from a datareader so it's possible that statement_timeout will be a good enough replacement. If there are places in our code that are doing that much work while reading from the datareader that it will hit our timeout they should really be cleaned up. The only really place that we may have problems is our reporting (where we have a 9 minute timeout set for commandtimeout which we do hit sometimes in the current functionality but I'll have to pull some stats as to the number of reports we currently have where the total DB time including reader time is greater then the 9 minute limit which would start failing if we move to 3.x + statement_timeout)

  6. roji commented on May 20, 2017

    @roji
    Member

    Hmm, thanks for the precise line - I admit I didn't remember this behavior in Npgsql 2.x (it's been a long time...).

    I definitely agree that it makes sense to trigger a backend cancellation once a frontend timeout occurs. I've opened #1567 to track this for 3.3.

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

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions