Skip to content

Commit

Permalink
script_runner: Kill sub-processes on exit
Browse files Browse the repository at this point in the history
  • Loading branch information
aherrmann committed Oct 29, 2019
1 parent 5bf9588 commit 61217f0
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions bazel_tools/script_runner.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ def script_runner(ctx, name, script, data = [], collect_data = True, interpreter
#include <unistd.h>
#ifdef _WIN32
#include <Windows.h>
#endif
#include "tools/cpp/runfiles/runfiles.h"
using bazel::tools::cpp::runfiles::Runfiles;
Expand All @@ -100,6 +104,19 @@ int main(int argc, char** argv) {{
argv_new[argc + 1] = nullptr;
#ifdef _WIN32
// Create a JobObject to ensure that spawned subprocesses are killed.
// See https://docs.microsoft.com/en-us/windows/win32/api/winbase/nf-winbase-createjobobjecta.
std::unique_ptr<std::remove_pointer<HANDLE>::type, std::function<void(HANDLE)>> job_object(
CreateJobObject(NULL, NULL),
[](HANDLE h) {{ CloseHandle(h); }}
);
// Ensure that sub-processes are assigned to the same JobObject.
AssignProcessToJobObject(job_object.get(), GetCurrentProcess());
// Ensure that sub-processes are killed on exit.
JOBOBJECT_EXTENDED_LIMIT_INFORMATION info = {{0}};
info.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
SetInformationJobObject(job_object.get(), JobObjectExtendedLimitInformation, &info, sizeof(info));
// On Windows execv does not behave as expected,
// see https://github.com/austriancoder/ccache-win32/issues/3#issuecomment-120084234.
int status = spawnv(_P_WAIT, argv_new[0], argv_new.get());
Expand Down

0 comments on commit 61217f0

Please sign in to comment.