mirror of
https://github.com/IgorAherne/Spellcast3d.git
synced 2026-12-01 08:06:27 -10:00
32 lines
1.3 KiB
Python
32 lines
1.3 KiB
Python
"""Linux: a child process the kernel ends when its parent's process ends, whatever ends it. STDLIB ONLY: the hub ties
|
|
its tools to itself with it (procman), and the installer the programs it runs - pip, git, a tool's own installer -
|
|
so that none of them works on in a venv after the hub or the installer was killed. Windows has the hub's job object
|
|
for the same (procman).
|
|
"""
|
|
|
|
from __future__ import annotations
|
|
|
|
import ctypes
|
|
import signal
|
|
import sys
|
|
import threading
|
|
from typing import Callable
|
|
|
|
_PR_SET_PDEATHSIG = 1
|
|
_libc = ctypes.CDLL(None, use_errno=True) if sys.platform.startswith("linux") else None
|
|
|
|
|
|
def _die_with_the_parent() -> None:
|
|
"""Runs in the new process before its program starts. Nothing else belongs here - the child of a process with
|
|
threads may call next to nothing."""
|
|
_libc.prctl(_PR_SET_PDEATHSIG, signal.SIGKILL) # type: ignore[union-attr]
|
|
|
|
|
|
def tie() -> Callable[[], None] | None:
|
|
"""A Popen `preexec_fn` that ties the new process to this one - or None where that cannot be asked: off Linux,
|
|
and from any thread but the main one (the kernel sends the signal when the THREAD that started the process
|
|
ends, and only the main thread lives as long as the process)."""
|
|
if _libc is None or threading.current_thread() is not threading.main_thread():
|
|
return None
|
|
return _die_with_the_parent
|