Skip to content

Commit 1ac7e65

Browse files
DHowettmsftbot[bot]
authored and
msftbot[bot]
committed
Fix the total teardown order for ConhostConnection (#1340)
The signal pipe must be terminated first. It is this very termination that signals to the connected console host that it should begin exiting in an orderly manner. We're introducing an indefinite wait (yes, I know: it's not great) for conhost to exit. **This matches ClosePseudoConsole in kernelbase/winconpty.** If it does not exit in an orderly manner, powershell (and perhaps other .NET CLI applications) may crash immediately after conhost exits. Fixes #1338.
1 parent 440bee0 commit 1ac7e65

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/cascadia/TerminalConnection/ConhostConnection.cpp

+11-4
Original file line numberDiff line numberDiff line change
@@ -162,15 +162,22 @@ namespace winrt::Microsoft::Terminal::TerminalConnection::implementation
162162

163163
if (!_closing.exchange(true))
164164
{
165-
_hJob.reset(); // This will terminate the process _piConhost is holding.
166-
_piConhost.reset();
167-
165+
// It is imperative that the signal pipe be closed first; this triggers the
166+
// pseudoconsole host's teardown. See PtySignalInputThread.cpp.
167+
_signalPipe.reset();
168168
_inPipe.reset();
169169
_outPipe.reset();
170-
_signalPipe.reset();
171170

171+
// Tear down our output thread -- now that the output pipe was closed on the
172+
// far side, we can run down our local reader.
172173
WaitForSingleObject(_hOutputThread.get(), INFINITE);
173174
_hOutputThread.reset();
175+
176+
// Wait for conhost to terminate.
177+
WaitForSingleObject(_piConhost.hProcess, INFINITE);
178+
179+
_hJob.reset(); // This is a formality.
180+
_piConhost.reset();
174181
}
175182
}
176183

0 commit comments

Comments
 (0)