Skip to content

Commit 7e05022

Browse files
a-sivaCommit Queue
authored andcommitted
Ensure only the child Dart VM process that is spawned from the
Dart CLI process gets killed when the Dart CLI process dies. (See #61148) TEST=manual and flutter_attach_test Change-Id: I297f6b20aa30f45ef3f4aec6f24db9fc94751b94 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/440264 Commit-Queue: Siva Annamalai <[email protected]> Reviewed-by: Alexander Aprelev <[email protected]>
1 parent c4f4dcb commit 7e05022

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

runtime/bin/process_win.cc

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,7 @@ class ProcessStarter {
585585
return 0;
586586
}
587587

588-
int StartForExec() {
588+
int StartForExec(HANDLE hjob) {
589589
ASSERT(mode_ == kInheritStdio);
590590
ASSERT(Process::ModeIsAttached(mode_));
591591
ASSERT(!Process::ModeHasStdio(mode_));
@@ -642,14 +642,20 @@ class ProcessStarter {
642642
reinterpret_cast<STARTUPINFOW*>(&startup_info), &process_info);
643643

644644
if (result == 0) {
645-
return SetOsErrorMessage(os_error_message_);
645+
return CleanupAndReturnError();
646646
}
647647
child_process_handle_ = process_info.hProcess;
648648
CloseHandle(process_info.hThread);
649649
CloseHandle(stdin_handle);
650650
CloseHandle(stdout_handle);
651651
CloseHandle(stderr_handle);
652652

653+
// Put this new process into the job object of the parent so that it
654+
// is killed when the parent is killed.
655+
if (!AssignProcessToJobObject(hjob, child_process_handle_)) {
656+
return CleanupAndReturnError();
657+
}
658+
653659
// Return process id.
654660
*id_ = process_info.dwProcessId;
655661
return 0;
@@ -978,7 +984,8 @@ int Process::Exec(Namespace* namespc,
978984
f.Printf("Process::Exec - CreateJobObject failed %d\n", GetLastError());
979985
return -1;
980986
}
981-
JOBOBJECT_EXTENDED_LIMIT_INFORMATION info;
987+
JOBOBJECT_EXTENDED_LIMIT_INFORMATION info = {
988+
{{{0, 0}}, {{0, 0}}, 0, 0, 0, 0, 0, 0, 0}, {0}, 0, 0, 0, 0};
982989
DWORD qresult;
983990
if (!QueryInformationJobObject(hjob, JobObjectExtendedLimitInformation, &info,
984991
sizeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION),
@@ -988,7 +995,13 @@ int Process::Exec(Namespace* namespc,
988995
GetLastError());
989996
return -1;
990997
}
991-
info.BasicLimitInformation.LimitFlags |= JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE;
998+
// Ensure that a child process that adds itself to this job object will
999+
// be killed when the parent dies and child processes that do not add
1000+
// themselves to this job object will not get killed when the parent
1001+
// dies.
1002+
info.BasicLimitInformation.LimitFlags |=
1003+
(JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE |
1004+
JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK);
9921005
if (!SetInformationJobObject(hjob, JobObjectExtendedLimitInformation, &info,
9931006
sizeof(JOBOBJECT_EXTENDED_LIMIT_INFORMATION))) {
9941007
BufferFormatter f(errmsg, errmsg_len);
@@ -1019,7 +1032,7 @@ int Process::Exec(Namespace* namespc,
10191032
ProcessStarter starter(path, &(arguments[1]), (arguments_length - 1),
10201033
working_directory, nullptr, 0, kInheritStdio, nullptr,
10211034
nullptr, nullptr, &pid, nullptr, &os_error_message);
1022-
int result = starter.StartForExec();
1035+
int result = starter.StartForExec(hjob);
10231036
if (result != 0) {
10241037
BufferFormatter f(errmsg, errmsg_len);
10251038
f.Printf("Process::Exec - %s\n", os_error_message);

0 commit comments

Comments
 (0)