[PR #8982] [MERGED] Fix crash in terminal when tab closed while data is being output #27361

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

📋 Pull Request Information

Original PR: https://github.com/microsoft/terminal/pull/8982
Author: @j4james
Created: 2/1/2021
Status: Merged
Merged: 2/1/2021
Merged by: @undefined

Base: mainHead: fix-close-crash


📝 Commits (2)

  • d1ac220 Lock terminal while closing to prevent a crash.
  • ad50651 Add issue number to the comment.

📊 Changes

1 file changed (+11 additions, -0 deletions)

View changed files

📝 src/cascadia/TerminalControl/TermControl.cpp (+11 -0)

📄 Description

If there is data being output when a tab is closed, that can sometimes
result in the application crashing, because the renderer may still be in
use at the time is it destroyed. This PR attempts to prevent that from
happening by adding a lock in the TermControl::Close method.

What we're trying to prevent is the connection thread still being
active, and potentially accessing the renderer, after it has been
destroyed. So by acquiring the terminal lock in TermControl::Close,
after we've stopped accepting new output, we can be sure that the
connection thread is no longer active (it holds the lock while it is
processing output). So once we've acquired and released the lock, it
should be safe to tear down and destroy the renderer.

Validation Steps Performed

While this crash is difficult to reproduce in general usage, it occurred
quite frequently when testing my DECPS implementation (there is some
tricky thread synchronisation, which seems more likely to trigger the
issue). With this patch applied, though, those crashes have stopped
occurring.

I've also stepped through the shutdown code in the debugger, manually
freezing threads to get them aligned in the right way to trigger the
crash (as explained in issue #8734). Again with the patch applied, I can
no longer get the crash to occur.

Closes #8734


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/microsoft/terminal/pull/8982 **Author:** [@j4james](https://github.com/j4james) **Created:** 2/1/2021 **Status:** ✅ Merged **Merged:** 2/1/2021 **Merged by:** [@undefined](undefined) **Base:** `main` ← **Head:** `fix-close-crash` --- ### 📝 Commits (2) - [`d1ac220`](https://github.com/microsoft/terminal/commit/d1ac220850d8f467f215958057b45c7762046954) Lock terminal while closing to prevent a crash. - [`ad50651`](https://github.com/microsoft/terminal/commit/ad50651346f2fbf3d6c8544de410d5d024d1ffe5) Add issue number to the comment. ### 📊 Changes **1 file changed** (+11 additions, -0 deletions) <details> <summary>View changed files</summary> 📝 `src/cascadia/TerminalControl/TermControl.cpp` (+11 -0) </details> ### 📄 Description If there is data being output when a tab is closed, that can sometimes result in the application crashing, because the renderer may still be in use at the time is it destroyed. This PR attempts to prevent that from happening by adding a lock in the `TermControl::Close` method. What we're trying to prevent is the connection thread still being active, and potentially accessing the renderer, after it has been destroyed. So by acquiring the terminal lock in `TermControl::Close`, after we've stopped accepting new output, we can be sure that the connection thread is no longer active (it holds the lock while it is processing output). So once we've acquired and released the lock, it should be safe to tear down and destroy the renderer. ## Validation Steps Performed While this crash is difficult to reproduce in general usage, it occurred quite frequently when testing my `DECPS` implementation (there is some tricky thread synchronisation, which seems more likely to trigger the issue). With this patch applied, though, those crashes have stopped occurring. I've also stepped through the shutdown code in the debugger, manually freezing threads to get them aligned in the right way to trigger the crash (as explained in issue #8734). Again with the patch applied, I can no longer get the crash to occur. Closes #8734 --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
claunia added the pull-request label 2026-01-31 09:21:32 +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#27361