Skip to content
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

Ensuring cancellation of tokens when using GetLinkedCancellationToken(a,b) #55968

Conversation

MattyLeslie
Copy link
Contributor

@MattyLeslie MattyLeslie commented May 30, 2024

Fix HANDLE_LEAK issue by properly disposing CancellationTokenSource in RemoteJSDataStream

Summary of the changes
Properly dispose CancellationTokenSource in RemoteJSDataStream to prevent handle leaks.

Description
This PR addresses the HANDLE_LEAK issue identified in RemoteJSDataStream by ensuring that CancellationTokenSource instances are properly disposed of. The GetLinkedCancellationToken method was removed, and using statements were added directly within the ReadAsync methods to manage the lifecycle of CancellationTokenSource. Additionally, the test cases were updated to validate the correct disposal of CancellationTokenSource.

Changes Made

  • Removed GetLinkedCancellationToken method.
  • Added using statements in ReadAsync methods for proper disposal of CancellationTokenSource.
  • Updated test cases to validate the proper disposal of CancellationTokenSource.

Fixes #50676

@dotnet-issue-labeler dotnet-issue-labeler bot added the area-blazor Includes: Blazor, Razor Components label May 30, 2024
@dotnet-policy-service dotnet-policy-service bot added the community-contribution Indicates that the PR has been added by a community member label May 30, 2024
@MattyLeslie MattyLeslie marked this pull request as ready for review May 30, 2024 14:20
@MattyLeslie MattyLeslie requested a review from a team as a code owner May 30, 2024 14:21
@javiercn
Copy link
Member

@MattyLeslie thanks for the contribution.

Updated test cases to validate the proper disposal of CancellationTokenSource.

We don't see any updated tests are they missing?

@MattyLeslie
Copy link
Contributor Author

@javiercn Sorry I forgot to push!

@dotnet-policy-service dotnet-policy-service bot added the pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun label Jun 6, 2024
@MattyLeslie
Copy link
Contributor Author

@javiercn, may I please request a review for this.

1 similar comment
@MattyLeslie
Copy link
Contributor Author

@javiercn, may I please request a review for this.

@javiercn
Copy link
Member

javiercn commented Aug 5, 2024

@MattyLeslie sorry, this wasn't on my radar.

We'll take a look by end of week.
@MackinnonBuck can you take a quick look and take action here?

Copy link
Member

@MackinnonBuck MackinnonBuck 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 the contribution, @MattyLeslie, and apologies for the delayed review.

I've made some suggestions that will need to be addressed before this can get merged. If you'd like me to handle those, I'm happy to do so!

Comment on lines 180 to 187
using (var linkedCts = CancellationTokenSource.CreateLinkedTokenSource(_streamCancellationToken, cancellationToken))
{
return await _pipeReaderStream.ReadAsync(buffer.AsMemory(offset, count), linkedCts.Token);
}
Copy link
Member

Choose a reason for hiding this comment

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

A nice characteristic of the previous implementation was that it didn't allocate a new CancellationTokenSource if at least one CancellationToken was not cancelable. It would be good to retain that characteristic if possible.

Comment on lines 311 to 312
Assert.True(cts.Token.CanBeCanceled);
Assert.False(cts.IsCancellationRequested);
Copy link
Member

Choose a reason for hiding this comment

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

Does this validate disposal of the linked CTS created in the ReadAsync() method? These asserts are acting on the CTS created in this test, which shouldn't be affected by any linked CTS that gets created from it.

Same question applies for the other test.

@MattyLeslie
Copy link
Contributor Author

@MackinnonBuck If you're will to carry it over the line that would be great otherwise I'll address that feedback as soon as I get time.

@MackinnonBuck MackinnonBuck force-pushed the Ensuring-cancellation-of-tokens-when-using-GetLinkedCancellationToken(a,b) branch 2 times, most recently from d7af27e to 708c0fd Compare October 21, 2024 17:50
@MackinnonBuck MackinnonBuck force-pushed the Ensuring-cancellation-of-tokens-when-using-GetLinkedCancellationToken(a,b) branch from 1cd9b14 to 844215d Compare October 21, 2024 17:53
Comment on lines -196 to -203
if (a.CanBeCanceled && b.CanBeCanceled)
{
return CancellationTokenSource.CreateLinkedTokenSource(a, b).Token;
}
else if (a.CanBeCanceled)
{
return a;
}
Copy link
Member

Choose a reason for hiding this comment

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

What's the reason for this logic disappearing and relying directly on ValueLinkdedCancellationTokenSource.Create

Copy link
Member

Choose a reason for hiding this comment

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

Nevermind, I saw it got moved. I thought ValueLinkdedCancellationTokenSource was a framework class.

@MackinnonBuck MackinnonBuck merged commit 6023624 into dotnet:main Oct 23, 2024
27 checks passed
@dotnet-policy-service dotnet-policy-service bot added this to the 10.0-preview1 milestone Oct 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-blazor Includes: Blazor, Razor Components community-contribution Indicates that the PR has been added by a community member pending-ci-rerun When assigned to a PR indicates that the CI checks should be rerun
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Using of CancellationToken in RemoteJSDataStream
3 participants