Skip to content

Fix crash when closing scratchpad pane#20195

Open
carlos-zamora wants to merge 2 commits into
mainfrom
dev/cazamor/bugfix/close-scratchpad
Open

Fix crash when closing scratchpad pane#20195
carlos-zamora wants to merge 2 commits into
mainfrom
dev/cazamor/bugfix/close-scratchpad

Conversation

@carlos-zamora
Copy link
Copy Markdown
Member

Summary of the Pull Request

Fixes a crash when closing a scratchpad pane. The crash would occur in Pane::GetTerminalArgsForPane() via a null-pointer exception on _content when calling _content.GetNewTerminalArgs(kind);.

My root cause analysis revealed that this was happening:

  • Pane::Close() is synchronously called: _content is cleared and Closed is raised
  • The parent's response (_CloseChildRoutine) performs a close animation, and then calls _CloseChild to actually mutate the pane tree once the animation is complete.
  • During the animation, the closed leaf is in a "corpse" state:
    • still in the pane tree
    • _isLeaf() is true
    • _lastActive is true
    • pointed at by Tab::_activePane in the tab's Closed handler
      • NOTE: _activePane only updates when a parent of the active pane closes, never when the active pane itself closes
    • _content is null
  • A second closePane is dispatched due to the ctrl+shift+w keypress. This is due to PR Add a keyboard shortcut handler to the TabRowControl #12260 (ugh). This occurs during the animation!
    • _CloseFocusedPane calls activeTab->GetActivePane() which returns the "corpse" _activePane.
    • _HandleClosePaneRequested then calls GetTerminalArgsForPane(), which dereferences the null _content, and the crash occurs.

The minimum fix is:

  • add a Pane::IsClosed() helper
  • TerminalPage::_CloseFocusedPane: return early if pane is closed

The other changes are additional safeguards so that this doesn't happen in the future.

Validation Steps Performed

✅ Close scratchpad pane with ctrl+shift+w --> no crash!

PR Checklist

Closes #20187

// - <none>
void Pane::Close()
{
// GH#20187: Make Close() idempotent. The actual removal of this pane
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It's OK for us to leave briefer comments and not refer to github issues.

This whole thing could be...

// Make Close() idempotent

or just no comment

(because the current comment encodes implementation details from everywhere else in the project and will be incorrect the moment somebody fixes the double-close bug)

Comment on lines +818 to +828
// shortcut, focus moves off the closed pane while keys
// are still depressed. The corresponding KeyUp events
// arrive at TabRowControl, whose KeyUp is wired to
// TerminalPage::_KeyDownHandler (see TerminalPage.xaml).
// _KeyDownHandler can't tell it was invoked from KeyUp
// and dispatches the close action a second time. By
// then the parent's close animation in
// _CloseChildRoutine is still running, so
// Tab::_activePane still points at the now-closed leaf
// ("corpse"). Detect that here and bail before we
// dereference its null content in GetTerminalArgsForPane.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

way too much detail

// Tab::_activePane still points at the now-closed leaf
// ("corpse"). Detect that here and bail before we
// dereference its null content in GetTerminalArgsForPane.
if (pane->IsClosed())
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

it should be sufficient to fix Close, right? I know this is "defense in depth" but it feels wrong to add a public API to pane to say "Are you closed?"

In general, we should not need to interrogate objects to say "are you half-dead?" because in the best case scenario they're HALF DEAD and should not be answering questions...

@microsoft-github-policy-service microsoft-github-policy-service Bot added the Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something label May 7, 2026
@microsoft-github-policy-service microsoft-github-policy-service Bot added the No-Recent-Activity This issue/PR is going stale and may be auto-closed without further activity. label May 14, 2026
@microsoft-github-policy-service
Copy link
Copy Markdown
Contributor

This pull request has been automatically marked as stale because it has been marked as requiring author feedback but has not had any activity for 7 days. It will be closed if no further activity occurs within 7 days of this comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Needs-Author-Feedback The original author of the issue/PR needs to come back and respond to something No-Recent-Activity This issue/PR is going stale and may be auto-closed without further activity.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Close scratchpad --> crash

2 participants