Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a ControlCore race condition on connection close #13882

Merged
2 commits merged into from
Sep 6, 2022

Conversation

lhecker
Copy link
Member

@lhecker lhecker commented Aug 30, 2022

As noted by the winrt::event documentation:

[...] But for asynchronous events, even after revoking [...], an in-flight
event might reach your object after it has started destructing.

This is because while adding/removing/calling event handlers might be
thread-safe, there's no guarantee that they run mutually exclusive.

This commit fixes the issue by reverting 6f0f245. Since we never checked
the result of closing a terminal connection anyways, this commit simply drops
the wait on the connection being teared down to ensure #1996 doesn't regress.

Closes #13880

Validation Steps Performed

  • Open tab, close tab, open tab, close tab, open tab, close tab
    • ConPTY ✅
    • Azure ✅
  • Closing a tab with a huge amount of panes ✅
  • Opening a bunch of tabs and then closing the window ✅
  • Closing a tab while it's busy with VT ✅
  • wtd -w 0 nt cmd /c exit
  • wtd -w -1 cmd /c exit
    • No WerFault spawns ✅

@ghost ghost added Area-TerminalConnection Issues pertaining to the terminal<->backend connection interface Area-TerminalControl Issues pertaining to the terminal control (input, selection, keybindings, mouse interaction, etc.) Issue-Bug It either shouldn't be doing this or needs an investigation. Priority-1 A description (P1) Product-Terminal The new Windows Terminal. labels Aug 30, 2022
@lhecker lhecker force-pushed the dev/lhecker/13880-connection-close-race branch from ce0aa9c to 9cd5ea6 Compare August 30, 2022 13:57
@DHowett
Copy link
Member

DHowett commented Aug 31, 2022

I've asked Leonard for more tests:

  • closing a tab with a HUGE amount of panes in it?
  • a tab whose connection exits before it even starts?
    • before terminal launches (wt -w -1 cmd /c exit)
  • after terminal is running (nt cmd /c exit from the commandline palette)
  • opening a bunch of tabs and then closing the entire terminal window?

Copy link
Member

@zadjii-msft zadjii-msft left a comment

Choose a reason for hiding this comment

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

I'm not signing off so that we hold till 1.17, but treat this as a ✔️ if the baby arrives before the 1.16/1.17 fork

@zadjii-msft
Copy link
Member

But also maybe test blatting big.txt and closing the pane

@lhecker
Copy link
Member Author

lhecker commented Aug 31, 2022

Alright, I finished testing everything and updated the PR message.

@DHowett
Copy link
Member

DHowett commented Aug 31, 2022

ONE FINAL TEST

@DHowett
Copy link
Member

DHowett commented Aug 31, 2022

Nevermind I couldn't even get it to work normally.

@lhecker
Copy link
Member Author

lhecker commented Sep 1, 2022

Could we get this into 1.16? I think this issue might be the cause for a crash on tab close I'm seeing quite often in debug builds...

@lhecker lhecker added this to the Terminal v1.16 milestone Sep 5, 2022
Copy link
Member

@zadjii-msft zadjii-msft left a comment

Choose a reason for hiding this comment

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

Sure. I don't think there's any reason this would cause client exes to get leaked. I'm not sure there's any reason we need to wait on the client app before finishing Connection::Close(). Going back a while (#3461, #1340, I don't see evidence for needing it)

Co-authored-by: Dustin L. Howett <duhowett@microsoft.com>
@DHowett DHowett added the AutoMerge Marked for automatic merge by the bot when requirements are met label Sep 6, 2022
@ghost
Copy link

ghost commented Sep 6, 2022

Hello @DHowett!

Because this pull request has the AutoMerge label, I will be glad to assist with helping to merge this pull request once all check-in policies pass.

p.s. you can customize the way I help with merging this pull request, such as holding this pull request until a specific person approves. Simply @mention me (@msftbot) and give me an instruction to get started! Learn more here.

@ghost ghost merged commit 666c446 into main Sep 6, 2022
@ghost ghost deleted the dev/lhecker/13880-connection-close-race branch September 6, 2022 21:37
@ghost
Copy link

ghost commented Sep 13, 2022

🎉Windows Terminal Preview v1.16.252 has been released which incorporates this pull request.:tada:

Handy links:

DHowett pushed a commit that referenced this pull request Oct 12, 2022
As noted by the `winrt::event` documentation:
> [...] But for asynchronous events, even after revoking [...], an in-flight
> event might reach your object after it has started destructing.

This is because while adding/removing/calling event handlers might be
thread-safe, there's no guarantee that they run mutually exclusive.

This commit fixes the issue by reverting 6f0f245. Since we never checked
the result of closing a terminal connection anyways, this commit simply drops
the wait on the connection being teared down to ensure #1996 doesn't regress.

Closes #13880

## Validation Steps Performed
* Open tab, close tab, open tab, close tab, open tab, close tab
  * ConPTY ✅
  * Azure ✅
* Closing a tab with a huge amount of panes ✅
* Opening a bunch of tabs and then closing the window ✅
* Closing a tab while it's busy with VT ✅
* `wtd -w 0 nt cmd /c exit` ✅
* `wtd -w -1 cmd /c exit`
  * No WerFault spawns ✅

(cherry picked from commit 666c446)
Service-Card-Id: 86178273
Service-Version: 1.15
@ghost
Copy link

ghost commented Oct 18, 2022

🎉Windows Terminal v1.15.2874 has been released which incorporates this pull request.:tada:

Handy links:

ghost pushed a commit that referenced this pull request Dec 16, 2022
2 new ConPTY APIs were added as part of this commit:
* `ClosePseudoConsoleTimeout`
  Complements `ClosePseudoConsole`, allowing users to override the `INFINITE`
  wait for OpenConsole to exit with any arbitrary timeout, including 0.
* `ConptyReleasePseudoConsole`
  This releases the `\Reference` handle held by the `HPCON`. While it makes
  launching further PTY clients via `PROC_THREAD_ATTRIBUTE_PSEUDOCONSOLE`
  impossible, it does allow conhost/OpenConsole to exit naturally once all
  console clients have disconnected. This makes it unnecessary to having to
  monitor the exit of the spawned shell/application, just to then call
  `ClosePseudoConsole`, while carefully continuing to read from the output
  pipe. Instead users can now just read from the output pipe until it is
  closed, knowing for sure that no more data will come or clients connect.
  This is especially useful in combination with `ClosePseudoConsoleTimeout`
  and a timeout of 0, to allow conhost/OpenConsole to exit asynchronously.

These new APIs are used to fix an array of bugs around Windows Terminal exiting
either too early or too late. They also make usage of the ConPTY API simpler in
most situations (when spawning a single application and waiting for it to exit).

Depends on #13882, #14041, #14160, #14282

Closes #4564
Closes #14416
Closes MSFT-42244182

## Validation Steps Performed
* Calling `FreeConsole` in a handoff'd application closes the tab ✅
* Create a .bat file containing only `start /B cmd.exe`.
  If WT stable is the default terminal the tab closes instantly ✅
  With these changes included the tab stays open with a cmd.exe prompt ✅
* New ConPTY tests ✅
@zadjii-msft zadjii-msft linked an issue Jan 6, 2023 that may be closed by this pull request
This pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area-TerminalConnection Issues pertaining to the terminal<->backend connection interface Area-TerminalControl Issues pertaining to the terminal control (input, selection, keybindings, mouse interaction, etc.) AutoMerge Marked for automatic merge by the bot when requirements are met Issue-Bug It either shouldn't be doing this or needs an investigation. Priority-1 A description (P1) Product-Terminal The new Windows Terminal.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Terminal is crashing when closing tabs with no error message Terminal crashes when exiting a bash shell
3 participants