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

Allow pty.kill to be awaited #734

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion src/unixTerminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@ export class UnixTerminal extends Terminal {
this._socket.destroy();
}

public kill(signal?: string): void {
public async kill(signal?: string): Promise<void> {
try {
process.kill(this.pid, signal || 'SIGHUP');
} catch (e) { /* swallow */ }
Expand Down
19 changes: 9 additions & 10 deletions src/windowsPtyAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,21 +157,20 @@ export class WindowsPtyAgent {
}
}

public kill(): void {
public async kill(): Promise<void> {
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);
const 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
Expand Down
17 changes: 10 additions & 7 deletions src/windowsTerminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +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<void> {
return new Promise((res, rej) => {
this._deferNoArgs(() => {
if (signal) {
rej(new Error('Signals not supported on windows.'));
return;
}
this._close();
this._agent.kill().then(res, rej);
});
});
}

Expand Down
4 changes: 2 additions & 2 deletions typings/node-pty.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,9 @@ 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): void;
kill(signal?: string): Promise<void>;

/**
* Pauses the pty for customizable flow control.
Expand Down
Loading