From c0e232941c121f7224fbbbfa5798b550ccc2305b Mon Sep 17 00:00:00 2001 From: TheLazySquid Date: Sat, 30 Nov 2024 10:11:33 -0500 Subject: [PATCH 1/4] Allow pty.kill to be awaited --- src/unixTerminal.ts | 2 +- src/windowsPtyAgent.ts | 19 +++++++++---------- src/windowsTerminal.ts | 18 ++++++++++-------- typings/node-pty.d.ts | 2 +- 4 files changed, 21 insertions(+), 20 deletions(-) diff --git a/src/unixTerminal.ts b/src/unixTerminal.ts index b745d762d..8887041c1 100644 --- a/src/unixTerminal.ts +++ b/src/unixTerminal.ts @@ -253,7 +253,7 @@ export class UnixTerminal extends Terminal { this._socket.destroy(); } - public kill(signal?: string): void { + public async kill(signal?: string): Promise { try { process.kill(this.pid, signal || 'SIGHUP'); } catch (e) { /* swallow */ } diff --git a/src/windowsPtyAgent.ts b/src/windowsPtyAgent.ts index 5e8c062db..d4bc7a491 100644 --- a/src/windowsPtyAgent.ts +++ b/src/windowsPtyAgent.ts @@ -157,21 +157,20 @@ export class WindowsPtyAgent { } } - public kill(): void { + public async kill(): Promise { this._inSocket.readable = false; this._outSocket.readable = false; // Tell the agent to kill the pty, this releases handles to the process if (this._useConpty) { - this._getConsoleProcessList().then(consoleProcessList => { - consoleProcessList.forEach((pid: number) => { - try { - process.kill(pid); - } catch (e) { - // Ignore if process cannot be found (kill ESRCH error) - } - }); - (this._ptyNative as IConptyNative).kill(this._pty, this._useConptyDll); + let consoleProcessList = await this._getConsoleProcessList(); + consoleProcessList.forEach((pid: number) => { + try { + process.kill(pid); + } catch (e) { + // Ignore if process cannot be found (kill ESRCH error) + } }); + (this._ptyNative as IConptyNative).kill(this._pty, this._useConptyDll); } else { // Because pty.kill closes the handle, it will kill most processes by itself. // Process IDs can be reused as soon as all handles to them are diff --git a/src/windowsTerminal.ts b/src/windowsTerminal.ts index 2ef9feddb..479a0e27b 100644 --- a/src/windowsTerminal.ts +++ b/src/windowsTerminal.ts @@ -166,14 +166,16 @@ export class WindowsTerminal extends Terminal { }); } - public kill(signal?: string): void { - this._deferNoArgs(() => { - if (signal) { - throw new Error('Signals not supported on windows.'); - } - this._close(); - this._agent.kill(); - }); + public kill(signal?: string): Promise { + return new Promise((res) => { + this._deferNoArgs(() => { + if (signal) { + throw new Error('Signals not supported on windows.'); + } + this._close(); + this._agent.kill().then(res); + }); + }) } private _deferNoArgs(deferredFn: () => void): void { diff --git a/typings/node-pty.d.ts b/typings/node-pty.d.ts index 7a14ec647..54859345e 100644 --- a/typings/node-pty.d.ts +++ b/typings/node-pty.d.ts @@ -181,7 +181,7 @@ declare module 'node-pty' { * Windows. * @throws Will throw when signal is used on Windows. */ - kill(signal?: string): void; + kill(signal?: string): Promise; /** * Pauses the pty for customizable flow control. From b7e7879c25636122f6ff9009c928aacddefce5ea Mon Sep 17 00:00:00 2001 From: TheLazySquid Date: Sat, 30 Nov 2024 10:54:26 -0500 Subject: [PATCH 2/4] Run linter --- src/windowsPtyAgent.ts | 2 +- src/windowsTerminal.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/windowsPtyAgent.ts b/src/windowsPtyAgent.ts index d4bc7a491..b5a9dd144 100644 --- a/src/windowsPtyAgent.ts +++ b/src/windowsPtyAgent.ts @@ -162,7 +162,7 @@ export class WindowsPtyAgent { this._outSocket.readable = false; // Tell the agent to kill the pty, this releases handles to the process if (this._useConpty) { - let consoleProcessList = await this._getConsoleProcessList(); + const consoleProcessList = await this._getConsoleProcessList(); consoleProcessList.forEach((pid: number) => { try { process.kill(pid); diff --git a/src/windowsTerminal.ts b/src/windowsTerminal.ts index 479a0e27b..8664f9eec 100644 --- a/src/windowsTerminal.ts +++ b/src/windowsTerminal.ts @@ -175,7 +175,7 @@ export class WindowsTerminal extends Terminal { this._close(); this._agent.kill().then(res); }); - }) + }); } private _deferNoArgs(deferredFn: () => void): void { From efe9763c99047051d10176f4f479cd365393e9bb Mon Sep 17 00:00:00 2001 From: TheLazySquid Date: Sat, 30 Nov 2024 14:27:43 -0500 Subject: [PATCH 3/4] Reject pty.kill promise if error happens --- src/windowsTerminal.ts | 6 +++--- typings/node-pty.d.ts | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/windowsTerminal.ts b/src/windowsTerminal.ts index 8664f9eec..eff9b062c 100644 --- a/src/windowsTerminal.ts +++ b/src/windowsTerminal.ts @@ -167,13 +167,13 @@ export class WindowsTerminal extends Terminal { } public kill(signal?: string): Promise { - return new Promise((res) => { + return new Promise((res, rej) => { this._deferNoArgs(() => { if (signal) { - throw new Error('Signals not supported on windows.'); + rej(new Error('Signals not supported on windows.')); } this._close(); - this._agent.kill().then(res); + this._agent.kill().then(res, rej); }); }); } diff --git a/typings/node-pty.d.ts b/typings/node-pty.d.ts index 54859345e..42802e22a 100644 --- a/typings/node-pty.d.ts +++ b/typings/node-pty.d.ts @@ -179,7 +179,7 @@ declare module 'node-pty' { * Kills the pty. * @param signal The signal to use, defaults to SIGHUP. This parameter is not supported on * Windows. - * @throws Will throw when signal is used on Windows. + * @throws Will reject when signal is used on Windows. */ kill(signal?: string): Promise; From e40339b36dfa2c4487f37cfb4c2a5b04d7efd939 Mon Sep 17 00:00:00 2001 From: TheLazySquid Date: Sat, 30 Nov 2024 15:23:58 -0500 Subject: [PATCH 4/4] Return if error is thrown --- src/windowsTerminal.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/windowsTerminal.ts b/src/windowsTerminal.ts index eff9b062c..341d2ba1d 100644 --- a/src/windowsTerminal.ts +++ b/src/windowsTerminal.ts @@ -171,6 +171,7 @@ export class WindowsTerminal extends Terminal { this._deferNoArgs(() => { if (signal) { rej(new Error('Signals not supported on windows.')); + return; } this._close(); this._agent.kill().then(res, rej);