Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions src/win/conpty.cc
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ static Napi::Value PtyResize(const Napi::CallbackInfo& info) {
std::lock_guard<std::mutex> lock(g_ptyHandlesMutex);
const pty_baton* handle = get_pty_baton(lock, id);

if (handle != nullptr) {
if (handle != nullptr && handle->hpc != nullptr) {
HANDLE hLibrary = LoadConptyDll(info, useConptyDll);
bool fLoadedDll = hLibrary != nullptr;
if (fLoadedDll)
Expand Down Expand Up @@ -537,7 +537,7 @@ static Napi::Value PtyClear(const Napi::CallbackInfo& info) {
std::lock_guard<std::mutex> lock(g_ptyHandlesMutex);
const pty_baton* handle = get_pty_baton(lock, id);

if (handle != nullptr) {
if (handle != nullptr && handle->hpc != nullptr) {
HANDLE hLibrary = LoadConptyDll(info, useConptyDll);
bool fLoadedDll = hLibrary != nullptr;
if (fLoadedDll)
Expand Down Expand Up @@ -577,9 +577,12 @@ static Napi::Value PtyKill(const Napi::CallbackInfo& info) {
PFNCLOSEPSEUDOCONSOLE const pfnClosePseudoConsole = (PFNCLOSEPSEUDOCONSOLE)GetProcAddress(
(HMODULE)hLibrary,
useConptyDll ? "ConptyClosePseudoConsole" : "ClosePseudoConsole");
if (pfnClosePseudoConsole)
if (pfnClosePseudoConsole && handle->hpc != nullptr)
{
pfnClosePseudoConsole(handle->hpc);
// The handle stays in ptyHandles until the exit watcher erases it, so
// null hpc to keep a later call from using the closed pseudoconsole.
handle->hpc = nullptr;
}
}
// Defensive: if PtyConnect was never called (or failed before closing the
Expand Down
7 changes: 7 additions & 0 deletions src/windowsTerminal.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,13 @@ if (process.platform === 'win32') {
term.on('exit', () => done());
term.kill();
});
it('should not crash parent process when killed twice', function (done) {
this.timeout(20000);
const term = new WindowsTerminal('cmd.exe', [], { useConptyDll });
term.on('exit', () => done());
term.kill();
term.kill();
});
it('should kill the process tree', function (done: Mocha.Done): void {
this.timeout(20000);
const term = new WindowsTerminal('cmd.exe', [], { useConptyDll });
Expand Down