[PR #4538] Fix the crash when closing Terminal containing multiple tabs. #25818

Open
opened 2026-01-31 09:11:58 +00:00 by claunia · 0 comments
Owner

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

State: closed
Merged: Yes


The Terminal would crash when closing it when there are multiple tabs
open. This was due to TerminalPage attempting to select a nonexistent
tab.

The block of code that was removed was causing issues when trying to
close all tabs at once. The way we close all our tabs in
_CloseAllTabs() was by repeatedly calling
_RemoveTabViewItemByIndex(0) until _tabs.Size() == 0. The problem
was that _RemoveTabViewItemByIndex would eventually call a coroutine
to set the next tab as the SelectedItem after removing a tab. The
coroutine would then pass control back to _CloseAllTabs() to finish
its loop, and by the time the coroutine resumes control, _tabs and
TabView().TabItems() would both be empty and it would crash attempting
to focus a tab.

Luckily, the functionality that this block of code provided is really no
longer needed . This code was used to focus on the next tab after
closing a tab. This might have been written way back when TabView
didn't have this functionality built in. It seems now that after
removing a TabItem from the TabView, the SelectedItem of the
TabView automatically updates, making this block of code unnecessary.

Validation Steps Performed

Did a lot of multiple tab open and closings and closing the window after
opening a ton of tabs. No crashes seem to occur anymore!
Test cases still pass.

Closes #4482

**Original Pull Request:** https://github.com/microsoft/terminal/pull/4538 **State:** closed **Merged:** Yes --- The Terminal would crash when closing it when there are multiple tabs open. This was due to `TerminalPage` attempting to select a nonexistent tab. The block of code that was removed was causing issues when trying to close all tabs at once. The way we close all our tabs in `_CloseAllTabs()` was by repeatedly calling `_RemoveTabViewItemByIndex(0)` until `_tabs.Size() == 0`. The problem was that `_RemoveTabViewItemByIndex` would eventually call a coroutine to set the next tab as the `SelectedItem` after removing a tab. The coroutine would then pass control back to `_CloseAllTabs()` to finish its loop, and by the time the coroutine resumes control, `_tabs` and `TabView().TabItems()` would both be empty and it would crash attempting to focus a tab. Luckily, the functionality that this block of code provided is really no longer needed . This code was used to focus on the next tab after closing a tab. This might have been written way back when TabView didn't have this functionality built in. It seems now that after removing a `TabItem` from the `TabView`, the `SelectedItem` of the TabView automatically updates, making this block of code unnecessary. ## Validation Steps Performed Did a lot of multiple tab open and closings and closing the window after opening a ton of tabs. No crashes seem to occur anymore! Test cases still pass. Closes #4482
claunia added the pull-request label 2026-01-31 09:11:58 +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#25818