Skip to content

Passing the cancellation token to the ReadBuffer - #3158

Merged
roji merged 19 commits into
npgsql:mainfrom
vonzshik:Cancellation
Sep 19, 2020
Merged

roji merged 19 commits into
npgsql:mainfrom
vonzshik:Cancellation

Conversation

@vonzshik

Copy link
Copy Markdown
Contributor

Related to #2437.

As this pr is already pretty big, I think it should be split in 3 parts:

  • Reading cancellation part (this pr).
  • Writing cancellation part.
  • Wrap it all up, so it would work with the cancellation request from the user token.

Also, might be worth to add some CancellationToken.ThrowIfCancellationRequested calls for the sake of .net 4.6.1. (which doesn't support the cancellation on the socket layer).

@roji

roji commented Sep 19, 2020

Copy link
Copy Markdown
Member

@vonzshik the plan sounds good to me - no problem to split the PRs.

Also, might be worth to add some CancellationToken.ThrowIfCancellationRequested calls for the sake of .net 4.6.1. (which doesn't support the cancellation on the socket layer).

Socket cancellation support was added to .NET Core 3.0, so previous versions of .NET Core are also affected. Also, ThrowIfCancellationRequested is considered good practice regardless of whether socket cancellation is supported or not, to abort earlier etc.

In general, we do already do this at the upper layer API surface. In methods without the async keyword, the pattern is:

if (cancellationToken.IsCancellationRequested)
    return Task.FromCanceled<T>(cancellationToken);

But we can definitely go over the code to make sure it's done everywhere.

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

Before giving this a full review, here are some general comments:

  • Any reason not to put the new CancellationToken parameter at the end, with a default of default which is the same as CancellationToken.None? Both seem to be the standard for this kind of thing. If so, this needs to be done across the whole codebase.
  • If we do the above, the cancellation token becomes optional, and methods which don't pass it (e.g. sync ones) can omit it.

(BTW I hope you're using a good IDE which allows for advanced refactoring, Jetbrains can change parameter order across the entire type hierarchy in one op)

Comment thread src/Npgsql.GeoJSON/GeoJSONHandler.cs Resolved

@YohDeadfall YohDeadfall left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

While @roji required changes, I'm happy with the current design. The common way doesn't mean the right one, because the devil is in details, so let's list them:

  1. Since it's internal, the cancellationToken cannot be optional. Otherwise, the code will be error prone and we don't need so (Shay too, but he did a very quick review (: ).
  2. Because the parameter isn't optional somewhere it won't be the last. Mixed style is what we would like avoid too, it makes code not fluent and increases threshold for new contributors because we require from them keep the current style, but at the same time there is no current, but many styles.
  3. There is the async parameter. Having cancellationToken following it groups them by meaning. It's a plus.

If I would make the change, it would be exactly like yours.

@roji

roji commented Sep 19, 2020

Copy link
Copy Markdown
Member

Since it's internal, the cancellationToken cannot be optional

Why not?

Because the parameter isn't optional somewhere it won't be the last

I agree that if the parameter isn't optional, it can't be the last everywhere. But:

  1. I think it should be optional
  2. Even if it can't be last everywhere, why shouldn't we make it last wherever possible? This would implement the behavior in CA1068 (warn when the parameter isn't last, unless it's non-optional and followed by optional).

There is the async parameter. Having cancellationToken following it groups them by meaning

That's a pretty weak argument IMHO for not following standard C# practices...

To summarize, there's not only a C# standard here - there's also compiler analysis warnings for making sure it is that way, and preventing the common pitfalls of forgetting to pass it along. I just don't see a reason not to follow it.

@YohDeadfall

Copy link
Copy Markdown
Contributor

I think it should be optional

Easy to forget somewhere and get a bug. Strongly disagreed with you. See @Brar's comment.

That's a pretty weak argument IMHO for not following standard C# practices..

Yep, but it's a plus.

@roji

roji commented Sep 19, 2020

Copy link
Copy Markdown
Member

I think it should be optional

Easy to forget somewhere and get a bug. Strongly disagreed with you. See @Brar's comment.

Have you seen my comment above? #3158 (comment)

@YohDeadfall

Copy link
Copy Markdown
Contributor

I have seen, but let's not make things optional when they are not intended to be optional. It's an internal API and the parameter must be set mostly always. Therefore, I don't see how it could be optional.

@roji

roji commented Sep 19, 2020

Copy link
Copy Markdown
Member

I have seen, but let's not make things optional when they are not intended to be optional. It's an internal API and the parameter must be set mostly always. Therefore, I don't see how it could be optional.

I'm still not understanding the reason not to have it optional, given that the compiler can catch errors for us. As you say, the parameter is mostly set, but not always - there are sync methods which don't need to pass it, for example. The methods do not require the cancellation token - they can work fine without it - so that means it's optional no?

This is also about respecting common C# coding guidelines, so that when new people come in, everything works the way they're used to. I'm fine with not following standards/guidelines, but we should do that when there's a good reason to do that, and I'm not sure I see that here...

Comment thread src/Npgsql.GeoJSON/GeoJSONHandler.cs Resolved
@roji

roji commented Sep 19, 2020

Copy link
Copy Markdown
Member

FYI opened #3162 to track setting up analyzers as appropriate.

@Brar Brar 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 the discussion about whether the cancellation token should have a default value or not on our internal interfaces, making them a bit more verbose either in the signature or at the call site isn't really worth our time.
Since we're talking about an internal interface we are completely free to change it whenever we feel like it and if there's a new standard that just evolved, we can choose to follow it immediately (opt into the optional warnings now) or do it once the dust around it has settled (opt into the optional warnings when we think the time has come to do it).
Mind you this isn't about correctness of implementation but rather about intent/taste and I guess there are not much more than three people at all, who care about intent/style of internal Npgsql-interfaces.
Personally I'm willing to accept it this way or that way.

Comment thread src/Npgsql.Json.NET/JsonbHandler.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.

Starting to look close to done, but please look the additional comments below. BTW it's fine to introduce new overloads to minimize the need for named parameters if those are used frequently.

Comment thread src/Npgsql/NpgsqlBinaryExporter.cs Resolved
Comment thread src/Npgsql/NpgsqlCommand.cs Resolved
Comment thread src/Npgsql/NpgsqlCommand.cs Resolved
@vonzshik
vonzshik requested a review from roji September 19, 2020 16:47

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

Almost there :) Just a few nits remaining.

Comment thread src/Npgsql/NpgsqlBinaryExporter.cs Resolved
Comment thread src/Npgsql/NpgsqlDataReader.cs Resolved
Comment thread src/Npgsql/NpgsqlDataReader.cs Resolved
Comment thread src/Npgsql/NpgsqlDataReader.cs Resolved
Comment thread src/Npgsql/NpgsqlDataReader.cs Resolved
Comment thread src/Npgsql/NpgsqlDataReader.cs Resolved
Comment thread src/Npgsql/NpgsqlReadBuffer.cs Resolved
@vonzshik
vonzshik requested a review from roji September 19, 2020 18:55

@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 as always for the great work.

Approving but there's a build failure, will merge once that's fixed.

@vonzshik

Copy link
Copy Markdown
Contributor Author

@roji yep, my mistake - fixed. And thank you again for reviews.

Now, unto the cancellation for the writing part...

@roji
roji merged commit 5a1b3fd into npgsql:main Sep 19, 2020
@Brar

Brar commented Sep 19, 2020

Copy link
Copy Markdown
Member

Thank you @vonzshik!
That was great work - and a lot of it!

@vonzshik

Copy link
Copy Markdown
Contributor Author

@Brar you kind of done the most of it - my part was not that big 😊

@vonzshik
vonzshik deleted the Cancellation branch September 28, 2020 06:50
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.

4 participants