Skip to content

Passing the cancellation token from the command to the read and write buffers - #3173

Merged
roji merged 12 commits into
npgsql:mainfrom
vonzshik:Combined-cancellation
Sep 24, 2020
Merged

roji merged 12 commits into
npgsql:mainfrom
vonzshik:Combined-cancellation

Conversation

@vonzshik

Copy link
Copy Markdown
Contributor

Closes #2437.

The final part for the cancellation story. The only thing missing here is the timeout, after which the token would be cancelled. It seems, I would have to add another parameter to the connection string (as HardCommandTimeout is only until the next read, not for the whole cancellation request). @roji objections?

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

I think that HardCommandTimeout could still be the right parameter for both scenarios (though we may want to rename it). It means the same thing: how much do we wait, after sending a PG cancellation, before declaring that the network (or PG) is totally dead and break the connection. The critical part is that PG cancellation isn't supposed to depend on the query being cancelled in any way - it's supposed to be "instantaneous". So in my mind it doesn't really matter if the PG cancellation was sent after a regular CommandTimeout, or because the user explicitly triggered it.

Maybe we should just call the parameter CancellationTimeout?

Comment thread src/Npgsql/NpgsqlCommand.cs Resolved
Comment thread src/Npgsql/NpgsqlCommand.cs Resolved
@vonzshik
vonzshik requested a review from roji September 22, 2020 05:43

@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 @vonzshik, looks really close - see below.

Comment thread src/Npgsql/NpgsqlCommand.cs Outdated
try
{
((NpgsqlCommand)cmd!).Cancel(true);
if (cancellationTimeout > 0)

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.

Just a thought (not for this PR)... It's true that 0 usually means "no timeout" (e.g. with CommandTimeout). However, CancellationTimeout is a bit special, in that a zero timeout could actually make sense - it would mean "don't attempt to send a PG cancellation and just break the connection right away.

I'm not sure if disabling PG cancellation could actually be useful to anyone - maybe some pseudo-PostgreSQL variant that doesn't support cancellation. It's also a bit confusing to give 0 a different meaning for CancellationTimeout than what it means for CommandTimeout. But something makes me want to support this, for completeness (and possibly use -1 to mean no timeout).

Any thoughts @vonzshik @YohDeadfall @Brar?

(no need to block this PRs on this question, we can change this later).

@vonzshik vonzshik Sep 23, 2020 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

An interesting idea. And I don't see a reason why not - although I highly doubt anyone would really use it. Maybe only if you want the fastest shutdown possible.

Comment thread src/Npgsql/NpgsqlCommand.cs Resolved
Comment thread src/Npgsql/NpgsqlCommand.cs Resolved
@vonzshik
vonzshik requested a review from roji September 23, 2020 06:25

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

Apart from that last allocation thing, looks great.

Re test coverage... We have a flaky (and somewhat badly-written) CancelAsync that's being ignored. Do you want to take a look at it and update it to the new behavior? I think it should be possible for it not to be flaky.

Unfortunately we cannot easily test scenarios of cancellation not working (i.e. network partition). One day we'll mock PostgreSQL :)

Comment thread src/Npgsql/NpgsqlCommand.cs Resolved
@vonzshik
vonzshik requested a review from roji September 23, 2020 10:18
var cancellationSource = new CancellationTokenSource(300);
var t = cmd.ExecuteNonQueryAsync(cancellationSource.Token);
Assert.That(async () => await t.ConfigureAwait(false), Throws.Exception.TypeOf<NpgsqlException>());
Assert.That(async () => await t.ConfigureAwait(false), Throws.Exception.TypeOf<PostgresException>());

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 to the test I suddenly realized we we're throwing PostgresException when the cancellation token is triggered - shouldn't we be throwing the standard OperationCanceledException instead? This is important not just because it's the standard exception for cancellation, but also because it affects the status of Tasks (canceled instead of faulted). This would mean translating the PG cancellation messages to OperationCanceledException when we received them (a bit like how we translated socket timeout exceptions to TimeoutException).

If we do this, we'd probably make the same change for sync cancellation via Cancel - a breaking change but I think we should do it.

Thoughts?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

There is actually an issue about this #1146.

Not sure, if we should change anything for sync cancellation - the result of the cancellation via Cancel should always be a PostgresException. It's just for the cancellation via the cancellation token, we would hide the original exception behind the OperationCanceledException.

Are we doing this as a part of this pr, or a separate one?

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.

There is actually an issue about this #1146.

Of course, forgot about that.

Not sure, if we should change anything for sync cancellation - the result of the cancellation via Cancel should always be a PostgresException

I'm not sure... I think making (sync) cancellation throw OperationCanceledException is similar to making the timeout throw TimeoutException - in both cases we're translating a lower-level exception type to a standard, higher-level .NET exception that conceptually means what we want (cancellation, timeout). Do you have any specific argument on why this should continue throwing PostgresException, apart from it being a breaking change (which is definitely an argument)?

It's just for the cancellation via the cancellation token, we would hide the original exception behind the OperationCanceledException.

It's definitely possible, though I don't think I've ever seen an inner exception on a OperationCanceledException. It could provide the extra information on where exactly the operation was cancelled (assuming a big stack with different components, and a cancellation token being passed around to everyone).

Are we doing this as a part of this pr, or a separate one?

As you're already working right in the area, it could make sense to just do it - but it's totally up to you. I'm fine with merging without this and fixing later.

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.

BTW another small argument for throwing OperationCanceledException for sync cancellation, is that it would make sync and async more consistent.

Comment thread src/Npgsql/NpgsqlCommand.cs Resolved
Comment thread test/Npgsql.Tests/CommandTests.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.

Looks great but holding off on merge for the remaining comments - let me know @vonzshik.

@vonzshik

Copy link
Copy Markdown
Contributor Author

@roji let's go with it as is right now - I'll make another pr for #1146, where we can discuss it further.

@roji
roji merged commit bc4b096 into npgsql:main Sep 24, 2020
@roji

roji commented Sep 24, 2020

Copy link
Copy Markdown
Member

Sounds good, thanks as always @vonzshik!

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.

Implement async cancellation

3 participants