From 8bef5eefd5c4baa2a74242c9d3140542f67e9041 Mon Sep 17 00:00:00 2001 From: "Dustin L. Howett" Date: Fri, 8 Jan 2021 02:18:27 -0800 Subject: [PATCH] winconpty: close the pty host handle after terminating it (#8707) It rather raises the question as to how we missed this. Closes #8706 --- src/winconpty/ft_pty/ConPtyTests.cpp | 9 +++++---- src/winconpty/winconpty.cpp | 1 + 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/src/winconpty/ft_pty/ConPtyTests.cpp b/src/winconpty/ft_pty/ConPtyTests.cpp index 188d90adc..e4f8a0925 100644 --- a/src/winconpty/ft_pty/ConPtyTests.cpp +++ b/src/winconpty/ft_pty/ConPtyTests.cpp @@ -440,10 +440,11 @@ void ConPtyTests::DiesOnClose() VERIFY_IS_TRUE(GetExitCodeProcess(piClient.hProcess, &dwExit)); VERIFY_ARE_EQUAL(dwExit, (DWORD)STILL_ACTIVE); - // Stash the pty process, it'll get zero'd after the call to close - const auto hConPtyProcess = pty.hConPtyProcess; + // Duplicate the pty process, it'll get closed and zero'd after the call to close + wil::unique_handle hConPtyProcess{}; + THROW_IF_WIN32_BOOL_FALSE(DuplicateHandle(GetCurrentProcess(), pty.hConPtyProcess, GetCurrentProcess(), hConPtyProcess.put(), 0, FALSE, DUPLICATE_SAME_ACCESS)); - VERIFY_IS_TRUE(GetExitCodeProcess(hConPtyProcess, &dwExit)); + VERIFY_IS_TRUE(GetExitCodeProcess(hConPtyProcess.get(), &dwExit)); VERIFY_ARE_EQUAL(dwExit, (DWORD)STILL_ACTIVE); Log::Comment(NoThrowString().Format(L"Sleep a bit to let the process attach")); @@ -451,6 +452,6 @@ void ConPtyTests::DiesOnClose() _ClosePseudoConsoleMembers(&pty); - GetExitCodeProcess(hConPtyProcess, &dwExit); + GetExitCodeProcess(hConPtyProcess.get(), &dwExit); VERIFY_ARE_NOT_EQUAL(dwExit, (DWORD)STILL_ACTIVE); } diff --git a/src/winconpty/winconpty.cpp b/src/winconpty/winconpty.cpp index fc92b138b..fbf7fa13b 100644 --- a/src/winconpty/winconpty.cpp +++ b/src/winconpty/winconpty.cpp @@ -269,6 +269,7 @@ void _ClosePseudoConsoleMembers(_In_ PseudoConsole* pPty) } TerminateProcess(pPty->hConPtyProcess, 0); + CloseHandle(pPty->hConPtyProcess); pPty->hConPtyProcess = nullptr; } // Then take care of the reference handle.