loading: terminate precompilation workers when their parent dies #63511
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
jl_exit(1);
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.
SGTM, with some improvements
jl_exit is rejected here by the safepoint analyzer (no GC-unsafe region).
Fixes. Claude preferred the second option rather than a pidfd watcher, so the signal listener handles everything on all platforms.
It still should go through normal exit, not syscall terminate
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)
// 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)
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);
Approved these changes.
CI failure is https://github.com/JuliaLang/julia/pull/63548
vtjnash
maleadt
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_cachepass 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 juliacron job.Assisted by: Opus 5.5, Sol 6.1