Skip to content

fix: ensure proper cleanup of console process on shutdown #755

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

Merged
merged 3 commits into from
Feb 3, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
26 changes: 26 additions & 0 deletions src/win/conpty.cc
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ struct pty_baton {

static std::vector<pty_baton*> ptyHandles;
static volatile LONG ptyCounter;
static HANDLE pty_global_job_handle_;
INIT_ONCE pty_initonce = INIT_ONCE_STATIC_INIT;

static pty_baton* get_pty_baton(int id) {
for (size_t i = 0; i < ptyHandles.size(); ++i) {
Expand All @@ -73,6 +75,22 @@ static bool remove_pty_baton(int id) {
return false;
}

static BOOL InitPtyGlobalJobHandle(INIT_ONCE* once, void* param, void** context) {
pty_global_job_handle_ = CreateJobObjectW(nullptr, nullptr);
if (pty_global_job_handle_ == NULL) {
return FALSE;
}
JOBOBJECT_EXTENDED_LIMIT_INFORMATION limits = {};
limits.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
if (!SetInformationJobObject(pty_global_job_handle_,
JobObjectExtendedLimitInformation,
&limits,
sizeof(limits))) {
return FALSE;
}
return TRUE;
}

struct ExitEvent {
int exit_code = 0;
};
Expand Down Expand Up @@ -426,6 +444,14 @@ static Napi::Value PtyConnect(const Napi::CallbackInfo& info) {
throw errorWithCode(info, "Cannot create process");
}

if (!InitOnceExecuteOnce(&pty_initonce, InitPtyGlobalJobHandle, NULL, NULL)) {
throw errorWithCode(info, "InitPtyGlobalJobHandle failed");
}

if (!AssignProcessToJobObject(pty_global_job_handle_, piClient.hProcess)) {
throw errorWithCode(info, "AssignProcessToJobObject failed");
}

// Update handle
handle->hShell = piClient.hProcess;

Expand Down
11 changes: 1 addition & 10 deletions src/windowsPtyAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,16 +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) {
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);
});
(this._ptyNative as IConptyNative).kill(this._pty, this._useConptyDll);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this means we can remove conpty_console_list_agent.ts and win/conpty_console_list.cc

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah left it as such incase you had use cases in future. Happy to remove them.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's remove if we don't need

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I retain the old tree kill logic for useConpty mode while useConptyDll mode uses the new logic. This allows for a possible fallback incase things go horribly wrong. We can merge the two paths once we gain confidence.

} 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