Skip to content

Refactor/Improve InvokeAsync to address potential disposing issues #13564

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 7 commits into from
Aug 15, 2025

Conversation

KlausLoeffelmann
Copy link
Member

@KlausLoeffelmann KlausLoeffelmann commented Jun 7, 2025

Addresses #12696, #12697
(Mentioning the latter just for same topic. I do not think, this is an issue really.)

Microsoft Reviewers: Open in CodeFlow

Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR refactors the InvokeAsync methods in Control_InvokeAsync.cs to address potential disposing issues and adds improved exception documentation.

  • Added a null-check exception comment for the callback parameter.
  • Inserted a handle creation check before invoking the asynchronous callback.
  • Updated the cancellation token registration handling and its disposal pattern.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
src/System.Windows.Forms/System/Windows/Forms/Control_InvokeAsync.cs Adds handle creation checks and adjusts cancellation token registration disposal in the async InvokeAsync implementations.
src/System.Windows.Forms/GlobalSuppressions.cs Introduces new suppressions for CA2007 warnings on the InvokeAsync methods.
Comments suppressed due to low confidence (1)

src/System.Windows.Forms/System/Windows/Forms/Control_InvokeAsync.cs:186

  • [nitpick] It may be beneficial to add a comment explaining that the cancellation token registration is disposed inside WrappedCallbackAsync to ensure proper unregistration and avoid potential disposal issues in asynchronous contexts.
using (registration)

@KlausLoeffelmann KlausLoeffelmann force-pushed the FixInvokeAsyncAllocationIssue branch 2 times, most recently from a231d58 to 3d907e2 Compare June 29, 2025 20:18
@KlausLoeffelmann KlausLoeffelmann force-pushed the FixInvokeAsyncAllocationIssue branch 2 times, most recently from bc2c642 to 7de829c Compare July 15, 2025 18:14
@KlausLoeffelmann KlausLoeffelmann force-pushed the FixInvokeAsyncAllocationIssue branch from 9d06bec to 43d4551 Compare August 12, 2025 23:23
@KlausLoeffelmann KlausLoeffelmann marked this pull request as ready for review August 13, 2025 20:07
@KlausLoeffelmann KlausLoeffelmann requested a review from a team as a code owner August 13, 2025 20:07
@dotnet-policy-service dotnet-policy-service bot removed the draft draft PR label Aug 13, 2025
Copy link

codecov bot commented Aug 15, 2025

Codecov Report

❌ Patch coverage is 73.61111% with 19 lines in your changes missing coverage. Please review.
✅ Project coverage is 51.91707%. Comparing base (687601b) to head (f10fa28).
⚠️ Report is 1 commits behind head on main.

❗ There is a different number of reports uploaded between BASE (687601b) and HEAD (f10fa28). Click for more details.

HEAD has 2 uploads less than BASE
Flag BASE (687601b) HEAD (f10fa28)
Debug 3 2
test 1 0
Additional details and impacted files
@@                 Coverage Diff                  @@
##                main      #13564          +/-   ##
====================================================
- Coverage   77.08926%   51.91707%   -25.17219%     
====================================================
  Files           3272        2064        -1208     
  Lines         644545      287861      -356684     
  Branches       47673       42105        -5568     
====================================================
- Hits          496875      149449      -347426     
+ Misses        144002      135523        -8479     
+ Partials        3668        2889         -779     
Flag Coverage Δ
Debug 51.91707% <73.61111%> (-25.17219%) ⬇️
integration 18.99633% <2.77778%> (+0.00093%) ⬆️
production 51.91707% <73.61111%> (+0.01861%) ⬆️
test ?
unit 49.32832% <73.61111%> (+0.02078%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@KlausLoeffelmann KlausLoeffelmann force-pushed the FixInvokeAsyncAllocationIssue branch 2 times, most recently from edce38a to 805baf5 Compare August 15, 2025 02:20
@KlausLoeffelmann KlausLoeffelmann force-pushed the FixInvokeAsyncAllocationIssue branch from 805baf5 to 4266df2 Compare August 15, 2025 17:12
Improve invoke behavior in InvokeAsync.

Update folder for Copilot instructions for generating the InvokeAsync unit tests.

(Note: They had been manually refactored quite a lot, but still saved tons of time.)
@KlausLoeffelmann KlausLoeffelmann force-pushed the FixInvokeAsyncAllocationIssue branch from 5b22c35 to 550fc44 Compare August 15, 2025 18:32
else
{
completion.TrySetException(ex);
}
Copy link
Member

Choose a reason for hiding this comment

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

We can create a function to avoid repeating this pattern.

Copy link
Member

@Shyam-Gupta Shyam-Gupta left a comment

Choose a reason for hiding this comment

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

Just those two suggestions. Rest looks good to me.

Shyam-Gupta
Shyam-Gupta previously approved these changes Aug 15, 2025
@mmitche mmitche disabled auto-merge August 15, 2025 21:54
@mmitche mmitche merged commit 93a2ef9 into dotnet:main Aug 15, 2025
6 of 8 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants