Utilise Windows Job Object so child dies w/ parent

This commit is contained in:
Atlante45 2017-08-29 16:24:34 -07:00
parent 4b8021bc4a
commit 462ec30cac
3 changed files with 67 additions and 0 deletions

View file

@ -28,6 +28,10 @@
const QString ASSIGNMENT_CLIENT_MONITOR_TARGET_NAME = "assignment-client-monitor";
const int WAIT_FOR_CHILD_MSECS = 1000;
#ifdef Q_OS_WIN
HANDLE JOB_OBJECT = createJobObject();
#endif
AssignmentClientMonitor::AssignmentClientMonitor(const unsigned int numAssignmentClientForks,
const unsigned int minAssignmentClientForks,
const unsigned int maxAssignmentClientForks,
@ -202,6 +206,10 @@ void AssignmentClientMonitor::spawnChildClient() {
assignmentClient->setProcessChannelMode(QProcess::ForwardedChannels);
assignmentClient->start(QCoreApplication::applicationFilePath(), _childArguments);
#ifdef Q_OS_WIN
addProcessToJobObject(JOB_OBJECT, assignmentClient->processId());
#endif
QString stdoutPath, stderrPath;
if (_wantsChildFileLogging) {

View file

@ -1113,3 +1113,56 @@ void watchParentProcess(int parentPID) {
timer->start();
}
#endif
#ifdef Q_OS_WIN
QString GetLastErrorAsString() {
//Get the error message, if any.
DWORD errorMessageID = ::GetLastError();
if (errorMessageID == 0)
return QString(); //No error message has been recorded
LPSTR messageBuffer = nullptr;
size_t size = FormatMessageA(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, errorMessageID, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), (LPSTR)&messageBuffer, 0, NULL);
std::string message(messageBuffer, size);
//Free the buffer.
LocalFree(messageBuffer);
return QString::fromStdString(message);
}
HANDLE createJobObject() {
HANDLE jobObject = CreateJobObject(nullptr, nullptr);
if (jobObject == nullptr) {
qWarning() << "Could NOT create job object:" << GetLastErrorAsString();
return nullptr;
}
JOBOBJECT_EXTENDED_LIMIT_INFORMATION JELI;
if (!QueryInformationJobObject(jobObject, JobObjectExtendedLimitInformation, &JELI, sizeof(JELI), nullptr)) {
qWarning() << "Could NOT query job object information" << GetLastErrorAsString();
return nullptr;
}
JELI.BasicLimitInformation.LimitFlags = JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
if (!SetInformationJobObject(jobObject, JobObjectExtendedLimitInformation, &JELI, sizeof(JELI))) {
qWarning() << "Could NOT set job object information" << GetLastErrorAsString();
return nullptr;
}
return jobObject;
}
void addProcessToJobObject(HANDLE jobObject, DWORD processId) {
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS, FALSE, processId);
if (hProcess == nullptr) {
qCritical() << "Could NOT open process" << GetLastErrorAsString();
}
if (!AssignProcessToJobObject(jobObject, hProcess)) {
qCritical() << "Could NOT assign process to job object" << GetLastErrorAsString();
}
}
#endif

View file

@ -238,4 +238,10 @@ void setMaxCores(uint8_t maxCores);
const QString PARENT_PID_OPTION = "parent-pid";
void watchParentProcess(int parentPID);
#ifdef _WIN32
HANDLE createJobObject();
void addProcessToJobObject(HANDLE jobObject, DWORD processId);
#endif
#endif // hifi_SharedUtil_h