← Back to Pull requests

loading: terminate precompilation workers when their parent dies #63511

Merged
maleadt merged 5 files (160 lines) on Oct 04, 2026 23:32 UTC
View on GitHub
maleadt maleadt commented on Sep 30, 2026 18:49 UTC
Author

When a julia process is killed abruptly while precompiling (by the OOM killer, kill -9, or the SIGHUP of a closing terminal), its precompilation workers are reparented to init and keep running, possibly indefinitely if precompilation hangs.

Have create_expr_cache pass its pid to the worker in JULIA_EXIT_WITH_PARENT_PID. At startup, the worker then arranges to terminate when that parent exits: through a parent-death signal on Linux (PR_SET_PDEATHSIG) and FreeBSD (PROC_PDEATHSIG_CTL), and on macOS, which lacks one, with a thread waiting for the parent's exit event on a kqueue. If the parent already died before that, the worker exits right away. The variable is removed from the environment so it is not passed on to processes the worker spawns. On Windows, libuv's job object already terminates children together with their parent.

This should make it possible to get rid of my pkill -P 1 julia cron job.

Assisted by: Opus 5.5, Sol 6.1

Diff: 160 lines (+156 / -4) across 5 files Reviewer: vtjnash Review duration: 6042m
All Vouch review confidence checks passed
Residual risk 0.06 = Change Risk (0.39) × (1 − Review Confidence (0.84)). Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.39 · 160 lines diff across 5 files
0.39
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.85 · 5 substantive comment(s)
0.85
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 50%
84%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size diff_size
0.304
Real PR Conversation Timeline
13 items
vtjnash vtjnash commented on code on Sep 30, 2026 21:20 UTC
Code Review
src/signals-unix.c:1
@@ -1445,6 +1449,58 @@ void restore_signals(void)
     }
 }
 
+#ifdef _OS_DARWIN_
+static void *parent_watcher(void *arg) JL_NOTSAFEPOINT
+{
+    int kq = (int)(intptr_t)arg;
+    struct kevent ev;
+    int nevents;
+    do {
+        nevents = kevent(kq, NULL, 0, &ev, 1, NULL);

we already have a background kevent thread watching for signals, so I think you can just add this there, instead of adding another kevent on another thread

vtjnash vtjnash commented on code on Sep 30, 2026 21:25 UTC
Code Review
src/signals-unix.c:1
@@ -1445,6 +1449,58 @@ void restore_signals(void)
     }
 }
 
+#ifdef _OS_DARWIN_
+static void *parent_watcher(void *arg) JL_NOTSAFEPOINT
+{
+    int kq = (int)(intptr_t)arg;
+    struct kevent ev;
+    int nevents;
+    do {
+        nevents = kevent(kq, NULL, 0, &ev, 1, NULL);
+    } while (nevents == -1 && errno == EINTR);
+    if (nevents == 1 && ev.filter == EVFILT_PROC && (ev.fflags & NOTE_EXIT))
+        _exit(1);
+    close(kq);
+    return NULL;
+}
+#endif
+
+// Terminate this process when its parent exits. This is best-effort: we keep
+// running if the platform does not support it or setting it up fails.
+void jl_exit_with_parent(pid_t parent) JL_NOTSAFEPOINT
+{
+#if defined(_OS_LINUX_)
+    // N.B.: this fires when the spawning *thread* exits, but Julia threads
+    //       generally live as long as the process.
+    prctl(PR_SET_PDEATHSIG, SIGKILL);
+#elif defined(_OS_FREEBSD_)
+    int sig = SIGKILL;
+    procctl(P_PID, 0, PROC_PDEATHSIG_CTL, &sig);
+#elif defined(_OS_DARWIN_)
+    // there is no parent-death signal, so wait for the exit event on a thread
+    int kq = kqueue();
+    if (kq != -1) {
+        fcntl(kq, F_SETFD, FD_CLOEXEC);
+        struct kevent ev;
+        EV_SET(&ev, parent, EVFILT_PROC, EV_ADD, NOTE_EXIT, 0, NULL);
+        // leave signal handling to the threads Julia sets up
+        sigset_t sset, oset;
+        sigfillset(&sset);
+        pthread_sigmask(SIG_SETMASK, &sset, &oset);
+        pthread_t thread;
+        if (kevent(kq, &ev, 1, NULL, 0, NULL) == 0 &&
+            pthread_create(&thread, NULL, parent_watcher, (void*)(intptr_t)kq) == 0)
+            pthread_detach(thread);
+        else
+            close(kq);
+        pthread_sigmask(SIG_SETMASK, &oset, NULL);
+    }
+#endif
+    // the parent may have exited before we started watching it
+    if (getppid() != parent)
+        _exit(1);

        jl_exit(1);

vtjnash vtjnash commented on code on Sep 30, 2026 21:30 UTC
Code Review
src/signals-unix.c:1
@@ -1445,6 +1449,58 @@ void restore_signals(void)
     }
 }
 
+#ifdef _OS_DARWIN_
+static void *parent_watcher(void *arg) JL_NOTSAFEPOINT
+{
+    int kq = (int)(intptr_t)arg;
+    struct kevent ev;
+    int nevents;
+    do {
+        nevents = kevent(kq, NULL, 0, &ev, 1, NULL);
+    } while (nevents == -1 && errno == EINTR);
+    if (nevents == 1 && ev.filter == EVFILT_PROC && (ev.fflags & NOTE_EXIT))
+        _exit(1);
+    close(kq);
+    return NULL;
+}
+#endif
+
+// Terminate this process when its parent exits. This is best-effort: we keep
+// running if the platform does not support it or setting it up fails.
+void jl_exit_with_parent(pid_t parent) JL_NOTSAFEPOINT
+{
+#if defined(_OS_LINUX_)
+    // N.B.: this fires when the spawning *thread* exits, but Julia threads

This gotcha isn't necessary here. (also don't be subject to Claude's tyranny and reject the use of to randomly annotate prose):

:robot: PR_SET_PDEATHSIG fires when the parent thread that spawned us exits, not when the parent process exits (see prctl(2)). In that case the kernel reparents the child to another live thread in the same thread group (find_new_reaper → find_alive_thread in kernel/exit.c), so getppid() stays the same. But forget_original_parent still sends pdeath_signal unconditionally, and the signal stays armed, so it fires again on every later exit of a parent thread.

With SIGKILL, the worker gets no chance to re-check getppid(), so it would be killed spuriously whenever the thread that ran uv_spawn exits. Any task can do the spawn, including one on an adopted foreign thread that later goes away. That's rare, but it can happen, and it isn't something we can rule out by relying on Julia threads living as long as the process.

Suggested fix, in order of preference:

1. pidfd_open(parent) plus a watcher thread (Linux ≥ 5.3). A pidfd refers to the whole process, not a thread, so thread exits don't matter. This is the same structure as the Darwin EVFILT_PROC thread: block in poll() on the pidfd and _exit(1) when it becomes readable. Keep the getppid() == parent check after* opening the pidfd; if it passes, the pidfd refers to our actual parent, because a pid can't be reused while its owner is still our parent.
2. Fallback on ENOSYS: PR_SET_PDEATHSIG with a catchable signal and a re-check. It can't be SIGTERM: parents send that on purpose (kill(p) defaults to it), and si_pid doesn't tell the two cases apart. A dedicated SIGRTMIN+k would work: block it in exit_with_parent before any threads exist, add it to jl_sigsetset when armed, and have signal_listener do if (getppid() != parent) _exit(1); continue;.

FreeBSD's PROC_PDEATHSIG_CTL and the macOS EVFILT_PROC watch are both per-process, so they don't have this problem.

vtjnash vtjnash commented on Sep 30, 2026 21:30 UTC
Reviewer

SGTM, with some improvements

maleadt maleadt commented on code on Oct 01, 2026 15:15 UTC
Code Review
src/signals-unix.c:1
@@ -1445,6 +1449,58 @@ void restore_signals(void)
     }
 }
 
+#ifdef _OS_DARWIN_
+static void *parent_watcher(void *arg) JL_NOTSAFEPOINT
+{
+    int kq = (int)(intptr_t)arg;
+    struct kevent ev;
+    int nevents;
+    do {
+        nevents = kevent(kq, NULL, 0, &ev, 1, NULL);
+    } while (nevents == -1 && errno == EINTR);
+    if (nevents == 1 && ev.filter == EVFILT_PROC && (ev.fflags & NOTE_EXIT))
+        _exit(1);
+    close(kq);
+    return NULL;
+}
+#endif
+
+// Terminate this process when its parent exits. This is best-effort: we keep
+// running if the platform does not support it or setting it up fails.
+void jl_exit_with_parent(pid_t parent) JL_NOTSAFEPOINT
+{
+#if defined(_OS_LINUX_)
+    // N.B.: this fires when the spawning *thread* exits, but Julia threads
+    //       generally live as long as the process.
+    prctl(PR_SET_PDEATHSIG, SIGKILL);
+#elif defined(_OS_FREEBSD_)
+    int sig = SIGKILL;
+    procctl(P_PID, 0, PROC_PDEATHSIG_CTL, &sig);
+#elif defined(_OS_DARWIN_)
+    // there is no parent-death signal, so wait for the exit event on a thread
+    int kq = kqueue();
+    if (kq != -1) {
+        fcntl(kq, F_SETFD, FD_CLOEXEC);
+        struct kevent ev;
+        EV_SET(&ev, parent, EVFILT_PROC, EV_ADD, NOTE_EXIT, 0, NULL);
+        // leave signal handling to the threads Julia sets up
+        sigset_t sset, oset;
+        sigfillset(&sset);
+        pthread_sigmask(SIG_SETMASK, &sset, &oset);
+        pthread_t thread;
+        if (kevent(kq, &ev, 1, NULL, 0, NULL) == 0 &&
+            pthread_create(&thread, NULL, parent_watcher, (void*)(intptr_t)kq) == 0)
+            pthread_detach(thread);
+        else
+            close(kq);
+        pthread_sigmask(SIG_SETMASK, &oset, NULL);
+    }
+#endif
+    // the parent may have exited before we started watching it
+    if (getppid() != parent)
+        _exit(1);

jl_exit is rejected here by the safepoint analyzer (no GC-unsafe region).

maleadt maleadt commented on code on Oct 01, 2026 15:16 UTC
Code Review
src/signals-unix.c:1
@@ -1445,6 +1449,58 @@ void restore_signals(void)
     }
 }
 
+#ifdef _OS_DARWIN_
+static void *parent_watcher(void *arg) JL_NOTSAFEPOINT
+{
+    int kq = (int)(intptr_t)arg;
+    struct kevent ev;
+    int nevents;
+    do {
+        nevents = kevent(kq, NULL, 0, &ev, 1, NULL);
+    } while (nevents == -1 && errno == EINTR);
+    if (nevents == 1 && ev.filter == EVFILT_PROC && (ev.fflags & NOTE_EXIT))
+        _exit(1);
+    close(kq);
+    return NULL;
+}
+#endif
+
+// Terminate this process when its parent exits. This is best-effort: we keep
+// running if the platform does not support it or setting it up fails.
+void jl_exit_with_parent(pid_t parent) JL_NOTSAFEPOINT
+{
+#if defined(_OS_LINUX_)
+    // N.B.: this fires when the spawning *thread* exits, but Julia threads

Fixes. Claude preferred the second option rather than a pidfd watcher, so the signal listener handles everything on all platforms.

vtjnash vtjnash commented on code on Oct 01, 2026 16:20 UTC
Code Review
src/signals-unix.c:1
@@ -1266,6 +1323,11 @@ static void *signal_listener(void *arg) JL_NOTSAFEPOINT
                     signal(*sig, SIG_DFL);
                 continue;
             }
+            if (ev.filter == EVFILT_PROC) {
+                if (ev.fflags & NOTE_EXIT)
+                    _exit(1); // our parent exited

It still should go through normal exit, not syscall terminate

vtjnash vtjnash commented on code on Oct 01, 2026 16:23 UTC
Code Review
src/signals-unix.c:1
@@ -1085,11 +1088,52 @@ const static int sigwait_sigs[] = {
     0
 };
 
+// The process whose exit should terminate ours, if any (see jl_exit_with_parent)
+static pid_t exit_with_parent_pid = 0;
+#if defined(_OS_LINUX_) || defined(_OS_FREEBSD_)
+// Sent by the kernel when our parent exits, and received by the signal listener.
+// Other platforms have it watch the parent through its kqueue instead.
+#define PARENT_DEATH_SIGNAL SIGRTMIN

I'm not sure glibc specifies what happens if you try to use this signal. We should be safer to reuse our existing IPC choice (e.g. SIGUSR1)

vtjnash vtjnash commented on code on Oct 02, 2026 19:17 UTC
Code Review
src/signals-unix.c:1
@@ -1085,13 +1088,46 @@ const static int sigwait_sigs[] = {
     0
 };
 
+// The process whose exit should terminate ours, if any (see jl_exit_with_parent)

// The process whose exit should terminate ours, if any

Comments should not document the function name -- that's already obvious from the code (all comments here have this issue)

vtjnash vtjnash commented on code on Oct 02, 2026 19:18 UTC
Code Review
src/signals-unix.c:1
@@ -1110,6 +1146,16 @@ static void kqueue_signal(int *sigqueue, struct kevent *ev, int sig)
         signal(sig, sig == SIGINT ? sigint_handler : SIG_IGN);
     }
 }
+
+static void kqueue_parent(int sigqueue, struct kevent *ev)
+{
+    EV_SET(ev, exit_with_parent_pid, EVFILT_PROC, EV_ADD, NOTE_EXIT, 0, NULL);
+    // the parent may have exited before we started watching it, in which case
+    // registering fails or no exit event will be delivered
+    if ((kevent(sigqueue, ev, 1, NULL, 0, NULL) != 0 && errno == ESRCH) ||
+        getppid() != exit_with_parent_pid)
+        kill(getpid(), SIGTERM);

I don't think we actually want to crash the process, but if we do, at least we should call the right helper:

        jl_raise(SIGTERM);

vtjnash vtjnash approved these changes on Oct 02, 2026 20:32 UTC
Approved Reviewer

Approved these changes.

maleadt maleadt commented on Oct 03, 2026 07:19 UTC
Author
Reviewers
Assignees
maleadt maleadt
Labels
risk: low confidence: 84.2% reviewed
Vouch Risk Breakdown
AI-powered review risk analysis
Change Risk
Change Risk · Model 1 (XGBoost)
Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
Score: 39.2% (MEDIUM) · Higher = larger blast radius
39.2%
Review Confidence
Review Confidence · Models 2 & 3
Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
Score: 84.2% · Lower = rushed or shallow review
84.2%
Residual Risk
Residual Risk · Core Equation
Residual = Change × (1 − Confidence)
Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
Score: 6.2% · Safe to merge
6.2%