[PR #5051] Rework TermControl's initialization #26094

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

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

State: closed
Merged: Yes


This commit rewrites a large swath of TermControl's initialization code.

  • TermControl now always has a _terminal; it will never be null
  • Event registration for _terminal and any other available-at-init
    fixtures has been moved into the constructor.
  • Event handlers how more uniformly check _closing if they interact
    with the _terminal.
  • Swap chain attachment has been cleaned up and no longer uses a
    coroutine when it's spawned from the UI thread.
    • We have to register the renderer's swapchain change notification
      handler after we set the swap chain, otherwise it'll call us back
      when it initializes itself.
  • InitializeTerminal now happens under the _terminal's write lock
    • Certain things that InitializeTerminal were calling themselves
      attempted to take the lock. They no longer do so.
  • TermControlAutomationPeer cannot take the read lock, because setting
    the scrollbar's Maximum during InitializeTerminal will trigger
    vivification of the automation peer tree; if it attempts to take the
    lock it will deadlock during initialization.
  • BlinkCursor was renamed to CursorTimerTick because it's the "Tick"
    handler for the "CursorTimer".
  • DragDropHandler was converted into a coroutine instead of just
    calling a coroutine.

Caveats:

Terminal may not have a _buffer until InitializeTerminal happens.
There's a nasty coupling between RenderTarget and TextBuffer that means
that we need to have a renderer before we have a buffer.

There's a second nasty coupling between RenderThread and Renderer: we
can't create a RenderThread during construction because it needs to be
given a renderer, and we can't create a Renderer during construction
because it needs a RenderThread. We don't want to kick off a thread
during construction.

Testing:

I wailed on this by opening and closing and resizing terminals and panes
and tabs, up to a hundred open tabs and one tab with 51 panes. I set one
tab to update the title as fast as it possibly could and tested
teardown, zoom, resize, mouse movement, etc. while this was all
happening.

Closes #4613.

**Original Pull Request:** https://github.com/microsoft/terminal/pull/5051 **State:** closed **Merged:** Yes --- This commit rewrites a large swath of TermControl's initialization code. * `TermControl` now _always_ has a `_terminal`; it will never be null * Event registration for `_terminal` and any other available-at-init fixtures has been moved into the constructor. * Event handlers how more uniformly check `_closing` if they interact with the _terminal. * Swap chain attachment has been cleaned up and no longer uses a coroutine when it's spawned from the UI thread. * We have to register the renderer's swapchain change notification handler after we set the swap chain, otherwise it'll call us back when it initializes itself. * `InitializeTerminal` now happens under the `_terminal`'s write lock * Certain things that InitializeTerminal were calling themselves attempted to take the lock. They no longer do so. * TermControlAutomationPeer cannot take the read lock, because setting the scrollbar's `Maximum` during `InitializeTerminal` will trigger vivification of the automation peer tree; if it attempts to take the lock it will deadlock during initialization. * `BlinkCursor` was renamed to `CursorTimerTick` because it's the "Tick" handler for the "CursorTimer". * `DragDropHandler` was converted into a coroutine instead of just _calling_ a coroutine. Caveats: Terminal may not have a `_buffer` until InitializeTerminal happens. There's a nasty coupling between RenderTarget and TextBuffer that means that we need to have a renderer before we have a buffer. There's a second nasty coupling between RenderThread and Renderer: we can't create a RenderThread during construction because it needs to be given a renderer, and we can't create a Renderer during construction because it needs a RenderThread. We don't want to kick off a thread during construction. Testing: I wailed on this by opening and closing and resizing terminals and panes and tabs, up to a hundred open tabs and one tab with 51 panes. I set one tab to update the title as fast as it possibly could and tested teardown, zoom, resize, mouse movement, etc. while this was all happening. Closes #4613.
claunia added the pull-request label 2026-01-31 09:13:53 +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#26094