Skip to content

Commit 3efdda8

Browse files
louztHansKristian-Work
authored andcommitted
cli: Kill child replay processes when parent dies on Linux
When Fossilize is forcefully terminated or crashes, the child replay processes currently become orphans and continue to compile shaders in the background, leading to excessive CPU/RAM usage in some cases where the existing reaping logic fails. This adds PR_SET_PDEATHSIG to guarantee child processes are terminated immediately when the parent process dies, matching the expected lifecycle of the application. PR squashed and stipped down by HansKristian-Work.
1 parent 137407b commit 3efdda8

2 files changed

Lines changed: 26 additions & 0 deletions

File tree

‎cli/fossilize_replay_linux.hpp‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include <sys/timerfd.h>
3333
#include <sys/stat.h>
3434
#include <sys/mman.h>
35+
#include <sys/prctl.h>
3536
#include <fcntl.h>
3637
#include <limits.h>
3738
#include <errno.h>
@@ -524,6 +525,7 @@ bool ProcessProgress::start_child_process(vector<ProcessProgress> &siblings)
524525
if (pipe(input_fds) < 0)
525526
return false;
526527

528+
pid_t parent_pid = getpid();
527529
pid_t new_pid = fork(); // Fork off a child.
528530
if (new_pid > 0)
529531
{
@@ -560,6 +562,17 @@ bool ProcessProgress::start_child_process(vector<ProcessProgress> &siblings)
560562
close(Global::control_fd);
561563

562564
// We're the child process.
565+
// Kill the child immediately if the parent (fossilize_replay / steam) dies.
566+
if (prctl(PR_SET_PDEATHSIG, SIGKILL) != 0)
567+
{
568+
LOGE("Failed to set PR_SET_PDEATHSIG, aborting child startup.\n");
569+
_exit(EXIT_FAILURE);
570+
}
571+
572+
// Resolve race condition if parent got reaped too early.
573+
if (getppid() != parent_pid)
574+
_exit(EXIT_FAILURE);
575+
563576
// Unblock the signal mask.
564577
if (pthread_sigmask(SIG_SETMASK, &Global::old_mask, nullptr) != 0)
565578
return EXIT_FAILURE;

‎fossilize_external_replayer_linux.hpp‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
#include <sys/stat.h>
2727
#include <sys/socket.h>
2828
#include <sys/wait.h>
29+
#include <sys/prctl.h>
2930
#include <fcntl.h>
3031
#include <unistd.h>
3132
#include <sched.h>
@@ -948,6 +949,7 @@ bool ExternalReplayer::Impl::start(const ExternalReplayer::Options &options)
948949
if (socketpair(AF_UNIX, SOCK_SEQPACKET, 0, control_fds) < 0)
949950
return false;
950951

952+
pid_t parent_pid = getpid();
951953
pid_t new_pid = fork();
952954
if (new_pid > 0)
953955
{
@@ -966,6 +968,17 @@ bool ExternalReplayer::Impl::start(const ExternalReplayer::Options &options)
966968
}
967969
else if (new_pid == 0)
968970
{
971+
// Kill replayer process immediately if parent application dies.
972+
if (prctl(PR_SET_PDEATHSIG, SIGKILL) != 0)
973+
{
974+
LOGE("Failed to set PR_SET_PDEATHSIG, aborting child startup.\n");
975+
_exit(1);
976+
}
977+
978+
// Resolve race condition if parent got reaped too early.
979+
if (getppid() != parent_pid)
980+
_exit(1);
981+
969982
close(fds[0]);
970983
close(control_fds[1]);
971984
close(child_fds[0]);

0 commit comments

Comments
 (0)