Skip to content

Rework TermControl's initialization - #5051

Merged
Dustin L. Howett (MSFT) (DHowett-MSFT) merged 3 commits into
masterfrom
dev/duhowett/term_control_init
Mar 26, 2020
Merged

Dustin L. Howett (MSFT) (DHowett-MSFT) merged 3 commits into
masterfrom
dev/duhowett/term_control_init

Conversation

@DHowett-MSFT

Copy link
Copy Markdown
Contributor

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.

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.
@DHowett-MSFT

Copy link
Copy Markdown
Contributor Author

I hate how I hodgepodge decided what gets and what doesn't get the lock. I/O should probably take the lock but contention could be huge for I/O and i don't want to exacerbate the problem where ^C is delayed during long streams...

@zadjii-msft Mike Griese (zadjii-msft) left a comment

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.

This seems fine to me, but maybe we should have the team self-host this for a while to check before merging this one, or at least get the internal selfhost on this for a while before pushing to the public.

Also mild worry that this will blow up #4608

{
return false;
}
{ // scope for terminalLock

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.

Would it make more sense to just unlock the lock instead of introducing another scope? idk

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Generally I prefer scopes for these, but it is largely stylistic

void ResetFontSize();

winrt::fire_and_forget SwapChainChanged();
winrt::fire_and_forget RenderEngineSwapChainChanged();

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.

The nit-iest nit: Are these supposed to be public? Private? I'd think both private, and both with _ prefixes on the names

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

yeah i'm not sure why we made that one public

this file is huge and could use some organization

@zadjii-msft Mike Griese (zadjii-msft) added Area-TerminalControl Issues pertaining to the terminal control (input, selection, keybindings, mouse interaction, etc.) Product-Terminal The new Windows Terminal. labels Mar 23, 2020
@DHowett-MSFT

Copy link
Copy Markdown
Contributor Author

Mike Griese (@zadjii-msft) this is in selfhost if you want to wail on it.

I might have found one crash; doing a wt split-pane ; new-tab, then focusing the first tab, then closing that tab, will explode. This is likely my code, not yours. :)

@DHowett-MSFT

Copy link
Copy Markdown
Contributor Author

Alright, this has been in test for 48 hours. I've found one crash, but it's in TerminalPage's handling of tabs. Perhaps this is it?

@zadjii-msft

Copy link
Copy Markdown
Member

Do we have a LOS on the TerminalPage crash, or how prevalant that crash might end up?

@DHowett-MSFT

Copy link
Copy Markdown
Contributor Author

Not yet, but bug incoming. It's a mismanagement of _tabs, as per usual 😉

@DHowett-MSFT

Copy link
Copy Markdown
Contributor Author

(not caused by this, just latent)

@zadjii-msft Mike Griese (zadjii-msft) left a comment

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.

Since the crash was pre-existing, then :shipit:

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

gogogo

@ghost

Copy link
Copy Markdown

🎉Windows Terminal Preview v0.11.1121.0 has been released which incorporates this pull request.:tada:

Handy links:

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

Labels

Area-TerminalControl Issues pertaining to the terminal control (input, selection, keybindings, mouse interaction, etc.) Needs-Second It's a PR that needs another sign-off Product-Terminal The new Windows Terminal.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Crash: _terminal (and other members?) used after freed in TermControl

4 participants