Skip to content

Commit 7dd4b67

Browse files
committed
close child when parent dies
1 parent 9e043dd commit 7dd4b67

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

EnvForwarder/system.cpp

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,9 @@ std::string getProgramPath()
5353
bool startProcess(const std::string& commandLine, unsigned long& exitCode)
5454
{
5555
std::wstring WCommandLine = sm::toWString(commandLine);
56-
STARTUPINFO si;
57-
PROCESS_INFORMATION pi;
58-
59-
ZeroMemory(&si, sizeof(si));
56+
STARTUPINFO si = { 0 };
57+
PROCESS_INFORMATION pi = { 0 };
6058
si.cb = sizeof(si);
61-
ZeroMemory(&pi, sizeof(pi));
6259

6360
if (!CreateProcessW(
6461
NULL,
@@ -76,6 +73,19 @@ bool startProcess(const std::string& commandLine, unsigned long& exitCode)
7673
return false;
7774
}
7875

76+
// Close child process when parent dies
77+
HANDLE hJob = CreateJobObject(NULL, NULL);
78+
JOBOBJECT_EXTENDED_LIMIT_INFORMATION jobExtendedLimitInfo = { 0 };
79+
jobExtendedLimitInfo.BasicLimitInformation.LimitFlags |= JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
80+
if (!SetInformationJobObject(hJob, JobObjectExtendedLimitInformation, &jobExtendedLimitInfo, sizeof(jobExtendedLimitInfo)))
81+
{
82+
Log().Get(LOG_WARNING) << "Could not make a process job";
83+
}
84+
else if (!AssignProcessToJobObject(hJob, pi.hProcess))
85+
{
86+
Log().Get(LOG_WARNING) << "Could not set the process job";
87+
}
88+
7989
// Wait for the process to finish
8090
WaitForSingleObject(pi.hProcess, INFINITE);
8191

@@ -84,6 +94,7 @@ bool startProcess(const std::string& commandLine, unsigned long& exitCode)
8494

8595
CloseHandle(pi.hProcess);
8696
CloseHandle(pi.hThread);
97+
CloseHandle(hJob);
8798

8899
if (!exitCodeOk) {
89100
exitCode = EXIT_FAILURE;

0 commit comments

Comments
 (0)