fix: utility process exit code for graceful termination (reland) (#44733)

* chore: reland "fix: utility process exit code for graceful termination"

This reverts commit 1cae73ba09.

Co-authored-by: deepak1556 <hop2deep@gmail.com>

* fix: exit code on posix when killed via api

Co-authored-by: deepak1556 <hop2deep@gmail.com>

* chore: fix code style

Co-authored-by: Robo <hop2deep@gmail.com>

---------

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: deepak1556 <hop2deep@gmail.com>
This commit is contained in:
trop[bot] 2024-11-19 22:09:53 +01:00 committed by GitHub
parent d477926b5d
commit 39e50be043
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 30 additions and 49 deletions

View file

@ -145,8 +145,8 @@ UtilityProcessWrapper::UtilityProcessWrapper(
}
}
if (!content::ServiceProcessHost::HasObserver(this))
content::ServiceProcessHost::AddObserver(this);
// Watch for service process termination events.
content::ServiceProcessHost::AddObserver(this);
mojo::PendingReceiver<node::mojom::NodeService> receiver =
node_service_remote_.BindNewPipeAndPassReceiver();
@ -258,6 +258,23 @@ void UtilityProcessWrapper::HandleTermination(uint64_t exit_code) {
pid_ = base::kNullProcessId;
CloseConnectorPort();
if (killed_) {
#if BUILDFLAG(IS_POSIX)
// UtilityProcessWrapper::Kill relies on base::Process::Terminate
// to gracefully shutdown the process which is performed by sending
// SIGTERM signal. When listening for exit events via ServiceProcessHost
// observers, the exit code on posix is obtained via
// BrowserChildProcessHostImpl::GetTerminationInfo which inturn relies
// on waitpid to extract the exit signal. If the process is unavailable,
// then the exit_code will be set to 0, otherwise we get the signal that
// was sent during the base::Process::Terminate call. For a user, this is
// still a graceful shutdown case so lets' convert the exit code to the
// expected value.
if (exit_code == SIGTERM || exit_code == SIGKILL) {
exit_code = 0;
}
#endif
}
EmitWithoutEvent("exit", exit_code);
Unpin();
}
@ -337,7 +354,7 @@ void UtilityProcessWrapper::PostMessage(gin::Arguments* args) {
connector_->Accept(&mojo_message);
}
bool UtilityProcessWrapper::Kill() const {
bool UtilityProcessWrapper::Kill() {
if (pid_ == base::kNullProcessId)
return false;
base::Process process = base::Process::Open(pid_);
@ -350,6 +367,7 @@ bool UtilityProcessWrapper::Kill() const {
// process reap should be signaled through the zygote via
// content::ZygoteCommunication::EnsureProcessTerminated.
base::EnsureProcessTerminated(std::move(process));
killed_ = result;
return result;
}