-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathrun-simtest-isolated.py
More file actions
executable file
·67 lines (55 loc) · 1.9 KB
/
run-simtest-isolated.py
File metadata and controls
executable file
·67 lines (55 loc) · 1.9 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python3
# Copyright (c) Walrus Foundation
# SPDX-License-Identifier: Apache-2.0
"""
Wrapper script to run seed-search.py in an isolated process group.
This prevents seed-search.py from killing the parent process when it exits.
"""
import os
import sys
import subprocess
import signal
# Global variable to store the subprocess
proc = None
def signal_handler(signum, frame):
"""Handle signals by forwarding them to the subprocess."""
global proc
if proc and proc.poll() is None:
# Forward the signal to the entire process group of the subprocess
try:
os.killpg(os.getpgid(proc.pid), signum)
except ProcessLookupError:
pass
sys.exit(130 if signum == signal.SIGINT else 1)
def main():
global proc
if len(sys.argv) < 2:
print("Usage: run-simtest-isolated.py <seed-search.py arguments>", file=sys.stderr)
sys.exit(1)
# Create a new process group
os.setpgid(0, 0)
# Set up signal handlers to forward signals to the subprocess
signal.signal(signal.SIGINT, signal_handler)
signal.signal(signal.SIGTERM, signal_handler)
# Run seed-search.py with all the arguments passed to this script
cmd = ["./scripts/simtest/seed-search.py"] + sys.argv[1:]
try:
# Start the subprocess in a new process group
proc = subprocess.Popen(cmd, preexec_fn=os.setsid)
# Wait for the subprocess to complete
returncode = proc.wait()
sys.exit(returncode)
except KeyboardInterrupt:
# Handle Ctrl+C
if proc and proc.poll() is None:
try:
os.killpg(os.getpgid(proc.pid), signal.SIGINT)
proc.wait(timeout=5)
except:
proc.kill()
sys.exit(130)
except Exception as e:
print(f"Error running seed-search.py: {e}", file=sys.stderr)
sys.exit(1)
if __name__ == "__main__":
main()