Skip to content

Fix cancellation by polling example (#31643) #31695

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Nov 9, 2022
Merged

Fix cancellation by polling example (#31643) #31695

merged 5 commits into from
Nov 9, 2022

Conversation

JJS
Copy link
Contributor

@JJS JJS commented Oct 11, 2022

Summary

I moved the token.IsCancellationRequested-block out of the outer loop to ensure it is checked in all cases.

Fixes #31643

@dotnet-bot dotnet-bot added this to the October 2022 milestone Oct 11, 2022
@ghost ghost added the community-contribution Indicates PR is created by someone from the .NET community. label Oct 11, 2022
Copy link
Member

@BillWagner BillWagner left a comment

Choose a reason for hiding this comment

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

Thanks for updating this @JJS

I've reviewed these changes, and I'll :shipit: now.

@JJS
Copy link
Contributor Author

JJS commented Oct 12, 2022

Is there something to do about the failed snippet? The error seems not to be related to the issue

@BillWagner
Copy link
Member

Is there something to do about the failed snippet? The error seems not to be related to the issue

Yes, if you create a default .csproj and .vbproj in the two folders with these source files, that will fix the issue.

It's a historical artifact. The source code was imported into GitHub from an internal system that built the samples when edited. There weren't project files, and we've been adding them whenever we edit any of the existing samples.

}
if (token.IsCancellationRequested) {
// Cleanup or undo here if necessary...
Console.WriteLine("\r\nCancelling after row {0}.", x);
Copy link
Member

Choose a reason for hiding this comment

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

Is x in scope here?

Copy link
Member

Choose a reason for hiding this comment

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

I don't see how cancellation is useful after the heavy work is already done?

Copy link
Contributor Author

@JJS JJS Oct 12, 2022

Choose a reason for hiding this comment

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

true, x is not in scope, I'll fix that.

As the usefulness of the cancellation is concerned, the overall behavior of the code did not change.

The polling example's main part is the IsCancellationRequested check in the loop condition, which did not change.

I suppose you could remove the additional if block, which is only there to provide a place for clean up code in case of cancellation. But you might need a place to put the ThrowIfCancellationRequested call.

I am not sure what you mean by "heavy work is already done" as the outer loop can be left at any point when cancellation is requested.

Copy link
Member

Choose a reason for hiding this comment

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

I could be misunderstanding something (and in fact I'm not sure what's the issue being resolved here), but I see token.IsCancellationRequested is done after the loops have already finished and completed the work.

This also doesn't make sense for me with the existing comment that says:

    // Assume that we know that the inner loop is very fast.
    // Therefore, checking once per row in the outer loop is sufficient.

What do you think @BillWagner?

Copy link
Contributor Author

@JJS JJS Oct 12, 2022

Choose a reason for hiding this comment

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

I updated the scope, the comments and some other issues with mixed up columns/rows.

token.IsCancellationRequested is called in the loop condition (so polling each column), not only after the loops. So the loops may not have completed their work. As I understand the original example, the call to token.IsCancellationRequested after the loops is only for clean up or calling token.ThrowIfCancellationRequested.

As the linked issue correctly states, there is an unexpected behavior if the cancellation is requested after the if condition is evaluated and before the loop condition is evaluated. In this case, the clean up code is not called.

As I said, there is a point to removing the if block completely but there must be some place to put the token.ThrowIfCancellationRequested in.

Copy link
Member

Choose a reason for hiding this comment

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

token.IsCancellationRequested is called in the loop condition

Ah I missed that.

Copy link
Member

@IEvangelist IEvangelist left a comment

Choose a reason for hiding this comment

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

A few nits

Co-authored-by: David Pine <david.pine@microsoft.com>
@BillWagner BillWagner self-assigned this Nov 9, 2022
@BillWagner BillWagner merged commit 5d91ed5 into dotnet:main Nov 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community-contribution Indicates PR is created by someone from the .NET community.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Mistake in example for listening for cancellation requests by polling
5 participants