Skip to content

Use smart pointers in coroutines#19752

Merged
DHowett merged 1 commit intomainfrom
dev/lhecker/19745-fix-async-crash
Jan 16, 2026
Merged

Use smart pointers in coroutines#19752
DHowett merged 1 commit intomainfrom
dev/lhecker/19745-fix-async-crash

Conversation

@lhecker
Copy link
Member

@lhecker lhecker commented Jan 16, 2026

After the coroutines resume, this may have already been destroyed.
So, this PR adds proper use of get_weak/strong.

Closes #19534
Closes #19745


const auto tasks = co_await _settings.GlobalSettings().ActionMap().FilterToSnippets(winrt::hstring{}, winrt::hstring{}); // IVector<Model::Command>
co_await wil::resume_foreground(Dispatcher());
co_await wil::resume_foreground(dispatcher);
Copy link
Member Author

Choose a reason for hiding this comment

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

This type of bug in particular is really annoying to spot. :(

Copy link
Member

Choose a reason for hiding this comment

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

SPECIFICALLY using dispatcher like this without storing it in a strong local? I do not see what the problem is - Dispatcher() is evaluated before the co_await operator

Copy link
Member Author

Choose a reason for hiding this comment

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

Because there's another co_await right above this one. this may already be gone. 🫠

Copy link
Member

Choose a reason for hiding this comment

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

oh. jeez.

Copy link
Member

@DHowett DHowett left a comment

Choose a reason for hiding this comment

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

fwiw I think using strong references in many of these places makes the code easier to read and reason about, at the potential cost of the coroutine keeping the [thing] alive until it's done rather than giving up on it


safe_void_coroutine TerminalPage::_doHandleSuggestions(SuggestionsArgs realArgs)
{
const auto weak = get_weak();
Copy link
Member

Choose a reason for hiding this comment

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

Is an alternative to simply get_strong before switching to the background? For things like this, I don't see the value in vending a weak reference and then immediately resolving it when we can just retain before and release after...

Copy link
Member Author

@lhecker lhecker Jan 16, 2026

Choose a reason for hiding this comment

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

I didn't do this for fear that this results in issues like XamlRoot being null, after the class got Close()d on the main thread and we continue using it through a coroutine.

Copy link
Member

Choose a reason for hiding this comment

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

ok fair

@microsoft-github-policy-service microsoft-github-policy-service bot added Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something and removed Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something labels Jan 16, 2026
@github-project-automation github-project-automation bot moved this to To Cherry Pick in 1.23 Servicing Pipeline Jan 16, 2026
@github-project-automation github-project-automation bot moved this to To Cherry Pick in 1.24 Servicing Pipeline Jan 16, 2026
@DHowett DHowett merged commit a528141 into main Jan 16, 2026
20 checks passed
@DHowett DHowett deleted the dev/lhecker/19745-fix-async-crash branch January 16, 2026 21:43
@DHowett DHowett moved this from To Cherry Pick to Cherry Picked in 1.23 Servicing Pipeline Jan 21, 2026
DHowett pushed a commit that referenced this pull request Jan 21, 2026
After the coroutines resume, `this` may have already been destroyed.
So, this PR adds proper use of `get_weak/strong`.

Closes #19745

(cherry picked from commit a528141)
Service-Card-Id: PVTI_lADOAF3p4s4AxadtzgjzPAU
Service-Version: 1.23
@DHowett DHowett moved this from To Cherry Pick to Cherry Picked in 1.24 Servicing Pipeline Jan 21, 2026
DHowett pushed a commit that referenced this pull request Jan 21, 2026
After the coroutines resume, `this` may have already been destroyed.
So, this PR adds proper use of `get_weak/strong`.

Closes #19745

(cherry picked from commit a528141)
Service-Card-Id: PVTI_lADOAF3p4s4BBcTlzgjzPAI
Service-Version: 1.24
@DHowett DHowett moved this from Cherry Picked to Shipped in 1.24 Servicing Pipeline Feb 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

2 participants