Skip to content

Cancelling queries on timeouts - #3130

Merged
roji merged 29 commits into
npgsql:mainfrom
vonzshik:CancelOnTimeout
Sep 18, 2020
Merged

roji merged 29 commits into
npgsql:mainfrom
vonzshik:CancelOnTimeout

Conversation

@vonzshik

@vonzshik vonzshik commented Sep 8, 2020

Copy link
Copy Markdown
Contributor

Concerning #1567

A few things, which I don't like and IMO deserve a separate pr:

  • RIght now, checking if an exception from a ReadBuffer is for a sync query timeout is kind of a pain. I think, it should be done similar to an async query - NpgsqlException with an inner exception as TimeoutException.
  • NpgsqlConnector.CancelRequest never throws an exception. Which in case of this pr is a bad thing, as there is no way to be sure if the cancellation was successful or not. I think a connection should be broken immediately if we were unable to cancel a query.
    @YohDeadfall @roji @Brar thoughts?

@roji

roji commented Sep 8, 2020

Copy link
Copy Markdown
Member

RIght now, checking if an exception from a ReadBuffer is for a sync query timeout is kind of a pain. I think, it should be done similar to an async query - NpgsqlException with an inner exception as TimeoutException.

Are you referring to the fact that for sync, NpgsqlException wraps an IOException wrapping a SocketException (see this test)? If so, I don't think it's a trivial question... There's something more correct about Npgsql simply bubbling up whatever low-level I/O exceptions it encounters - this allows users to unwrap the NpgsqlException and to be left with the standard .NET way of representing the problem. However, I do agree that the inconsistency between sync and async isn't ideal, and that it's a bit cumbersome to work through 3 exception types (and an error code) just to identify a timeout. Can you please open a separate issue for this?

NpgsqlConnector.CancelRequest never throws an exception. Which in case of this pr is a bad thing, as there is no way to be sure if the cancellation was successful or not. I think a connection should be broken immediately if we were unable to cancel a query.

As far as the user-facing cancellation API goes, this is by-design: cancellation is a best-effort thing and there are no guarantees. This is actually one of the few cases where the behavior is explicitly said in the specs:

If there is nothing to cancel, nothing happens. However, if there is a command in process, and the attempt to cancel fails, no exception is generated.

However, I absolutely agree with what you're saying - for the internal cancellation generated as part of the timeout mechanism, if there's a failure of any sort we can definitely skip the additional waiting time and go directly to breaking the connection. This should be a pretty simple refactoring - I think it should be fine to do as part of this PR (unless you really think otherwise).

@roji

roji commented Sep 8, 2020

Copy link
Copy Markdown
Member

(FYI holding off on review until you request one!)

@vonzshik
vonzshik marked this pull request as ready for review September 12, 2020 10:01
@vonzshik vonzshik changed the title DRAFT: Cancelling queries on timeouts Cancelling queries on timeouts Sep 12, 2020
@vonzshik

vonzshik commented Sep 12, 2020 •

Copy link
Copy Markdown
Contributor Author

@roji not a draft anymore and ready for the review

@roji roji left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Congrats on grokking the codebase and doing this well - it looks pretty close to mergeable... Please see my comments below, there will probably be another round after this, as this isn't trivial functionality.

Thanks again!

Comment thread src/Npgsql/NpgsqlConnector.cs Resolved
Comment thread src/Npgsql/NpgsqlConnector.cs Resolved
Comment thread src/Npgsql/NpgsqlConnector.cs Resolved
Comment thread src/Npgsql/NpgsqlConnector.cs Resolved
Comment thread src/Npgsql/NpgsqlConnector.cs Resolved
Comment thread src/Npgsql/NpgsqlReadBuffer.cs Resolved
Comment thread src/Npgsql/NpgsqlConnector.cs Resolved
@vonzshik

Copy link
Copy Markdown
Contributor Author

As we're now never break the connection on timeouts, should I get rid of a dontBreakOnTimeouts param from the NpgsqlReadBuffer.Ensure?

@roji

roji commented Sep 13, 2020

Copy link
Copy Markdown
Member

As we're now never break the connection on timeouts, should I get rid of a dontBreakOnTimeouts param from the NpgsqlReadBuffer.Ensure?

Makes sense to me... As long as Wait/WaitAsync work like they're supposed to - that's what that parameter was for.

Comment thread src/Npgsql/NpgsqlConnector.cs Resolved

@roji roji left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this looks really almost ready to me. I do think we should add a connection string parameter for the 2nd timeout, apart from that there are only a couple of nits left.

Comment thread src/Npgsql/NpgsqlConnector.cs Resolved
Comment thread src/Npgsql/NpgsqlConnector.cs Resolved
Comment thread src/Npgsql/NpgsqlReadBuffer.cs Resolved
@roji
roji changed the base branch from dev to main September 17, 2020 14:33
@vonzshik
vonzshik requested a review from roji September 17, 2020 19:14
Comment thread src/Npgsql/NpgsqlConnector.cs Resolved

@roji roji left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for all the work @vonzshik! I will probably do a small fix-up commit for the naming/messages after merging, will ping you on it.

@roji
roji merged commit 0c19179 into npgsql:main Sep 18, 2020
@roji roji linked an issue Sep 18, 2020 that may be closed by this pull request
roji added a commit that referenced this pull request Sep 18, 2020
@roji

roji commented Sep 18, 2020

Copy link
Copy Markdown
Member

Fix-up commit: 32d5721.

Thanks again @vonzshik, it was a pleasure to work with you on this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Trigger backend cancellation when a (frontend) timeout occurs

3 participants