forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsupervise.py
More file actions
52 lines (43 loc) · 1.54 KB
/
Copy pathsupervise.py
File metadata and controls
52 lines (43 loc) · 1.54 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""Tiny process supervisor used so recorded PIDs carry an ownership marker."""
from __future__ import annotations
import argparse
import os
import signal
import subprocess
import sys
_child: subprocess.Popen[bytes] | None = None
def _forward(signum: int, _frame: object) -> None:
if _child is not None and _child.poll() is None:
_child.send_signal(signum)
def main(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser()
parser.add_argument("--marker", required=True)
parser.add_argument("--service", required=True)
parser.add_argument("command", nargs=argparse.REMAINDER)
args = parser.parse_args(argv)
command = args.command[1:] if args.command[:1] == ["--"] else args.command
if not command:
parser.error("command is required after --")
if os.environ.get("OMI_HARNESS_PRIVATE_UMASK") == "077":
os.umask(0o077)
signal.signal(signal.SIGTERM, _forward)
signal.signal(signal.SIGINT, _forward)
# Keep a second, independent marker-bearing process in the owned group.
# If this supervisor crashes, teardown can still prove the surviving
# process group belongs to the exact random marker persisted in run.json.
guarded_command = [
sys.executable,
"-m",
"dev_harness.owned_child",
"--marker",
args.marker,
"--service",
args.service,
"--",
*command,
]
global _child
_child = subprocess.Popen(guarded_command)
return _child.wait()
if __name__ == "__main__":
raise SystemExit(main())