[PR #19395] Avoid reentrancy issues when dropping AppHost, even harder #31821

Closed
opened 2026-01-31 09:49:46 +00:00 by claunia · 0 comments
Owner

Original Pull Request: https://github.com/microsoft/terminal/pull/19395

State: closed
Merged: Yes


The previous fix in #19296 moved the destruction of AppHost into the tail end after we manipulate the _windows vector; however, it kept the part which calls into XAML (Close) before the erase. I suspect that we still had some reentrancy issues, where we cached an iterator before the list was modified by another window close event.

That is:

sequenceDiagram
		Emperor->>Emperor: Close Window
		Emperor->>+AppHost: Close (a)
		AppHost->>XAML: Close
		XAML-->>Emperor: pump loop
		Emperor->>Emperor: Close Window
		Emperor->>+AppHost: Close (b)
		AppHost->>XAML: Close
		XAML-->>Emperor: pump loop
		AppHost->>-Emperor: Closed
		Emperor->>Emperor: erase(b)
		AppHost->>-Emperor: Closed
		Emperor->>Emperor: erase(a)

Moving the Close() to after the erase ensures that there are no cached iterators that survive beyond XAML pumping the message loop.

Fixes 8d41ace3

**Original Pull Request:** https://github.com/microsoft/terminal/pull/19395 **State:** closed **Merged:** Yes --- The previous fix in #19296 moved the _destruction_ of AppHost into the tail end after we manipulate the `_windows` vector; however, it kept the part which calls into XAML (`Close`) before the `erase`. I suspect that we still had some reentrancy issues, where we cached an iterator before the list was modified by another window close event. That is: ```mermaid sequenceDiagram Emperor->>Emperor: Close Window Emperor->>+AppHost: Close (a) AppHost->>XAML: Close XAML-->>Emperor: pump loop Emperor->>Emperor: Close Window Emperor->>+AppHost: Close (b) AppHost->>XAML: Close XAML-->>Emperor: pump loop AppHost->>-Emperor: Closed Emperor->>Emperor: erase(b) AppHost->>-Emperor: Closed Emperor->>Emperor: erase(a) ``` Moving the `Close()` to after the `erase` ensures that there are no cached iterators that survive beyond XAML pumping the message loop. Fixes 8d41ace3
claunia added the pull-request label 2026-01-31 09:49:46 +00:00
Sign in to join this conversation.
No Label pull-request
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#31821