-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkill.py
More file actions
251 lines (212 loc) · 8.15 KB
/
Copy pathkill.py
File metadata and controls
251 lines (212 loc) · 8.15 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
from __future__ import annotations
import argparse
import os
import signal
import subprocess
import time
from dataclasses import dataclass
from app.server_runtime import NamespaceLaunchProfile
from app.server_runtime import load_all_namespace_launch_profiles
from app.services.exception_capture import CapturedExceptionContext
_WAIT_POLL_INTERVAL_SECONDS = 0.25
_DEFAULT_TERM_TIMEOUT_SECONDS = 5.0
_DEFAULT_KILL_TIMEOUT_SECONDS = 5.0
@dataclass(frozen=True)
class NamespacePort:
namespace: str
service: str
port: int
def _parse_args() -> argparse.Namespace:
parser = argparse.ArgumentParser(
description="Stop processes listening on saved MetaList namespace ports."
)
parser.add_argument(
"--dry-run",
action="store_true",
help="Print listener PIDs without terminating them.",
)
parser.add_argument(
"--term-timeout",
type=float,
default=_DEFAULT_TERM_TIMEOUT_SECONDS,
help="Seconds to wait after SIGTERM before SIGKILL.",
)
parser.add_argument(
"--kill-timeout",
type=float,
default=_DEFAULT_KILL_TIMEOUT_SECONDS,
help="Seconds to wait after SIGKILL before failing.",
)
return parser.parse_args()
def _namespace_ports_from_profile(*, profile: NamespaceLaunchProfile) -> list[NamespacePort]:
ports: list[NamespacePort] = [
NamespacePort(namespace=profile.namespace, service="HTTP", port=profile.port),
NamespacePort(namespace=profile.namespace, service="MCP", port=profile.mcp_port),
]
if profile.https_port is not None:
ports.append(NamespacePort(namespace=profile.namespace, service="HTTPS", port=profile.https_port))
return ports
def _load_namespace_ports() -> list[NamespacePort]:
profiles = load_all_namespace_launch_profiles()
ports: list[NamespacePort] = []
seen_ports: set[int] = set()
for profile in profiles:
for namespace_port in _namespace_ports_from_profile(profile=profile):
if namespace_port.port in seen_ports:
continue
seen_ports.add(namespace_port.port)
ports.append(namespace_port)
return ports
def _read_process_state(*, pid: int) -> str | None:
completed = subprocess.run(
["ps", "-o", "stat=", "-p", str(pid)],
capture_output=True,
check=False,
text=True,
)
if completed.returncode != 0:
return None
process_state = completed.stdout.strip()
if process_state == "":
return None
return process_state
def _is_process_running(*, pid: int) -> bool:
if not isinstance(pid, int):
raise TypeError(f"pid must be an int, got {type(pid)}")
if pid <= 0:
raise ValueError(f"pid must be positive, got: {pid}")
kill_capture = CapturedExceptionContext(ProcessLookupError, PermissionError)
with kill_capture:
os.kill(pid, 0)
if kill_capture.captured_exception is not None:
if isinstance(kill_capture.captured_exception, ProcessLookupError):
return False
if isinstance(kill_capture.captured_exception, PermissionError):
return True
raise RuntimeError("Unexpected process-probe exception type")
process_state = _read_process_state(pid=pid)
if process_state is None:
return True
if process_state.startswith("Z"):
return False
return True
def _wait_for_process_exit(*, pid: int, timeout_seconds: float) -> bool:
if not isinstance(timeout_seconds, float):
raise TypeError(f"timeout_seconds must be a float, got {type(timeout_seconds)}")
if timeout_seconds < 0.0:
raise ValueError(f"timeout_seconds must be >= 0.0, got {timeout_seconds}")
deadline = time.monotonic() + timeout_seconds
while time.monotonic() < deadline:
if not _is_process_running(pid=pid):
return True
time.sleep(_WAIT_POLL_INTERVAL_SECONDS)
return not _is_process_running(pid=pid)
def _send_signal_if_running(*, pid: int, signal_number: int) -> None:
if not _is_process_running(pid=pid):
return
signal_capture = CapturedExceptionContext(ProcessLookupError)
with signal_capture:
os.kill(pid, signal_number)
if signal_capture.captured_exception is not None:
return
def _stop_process(*, pid: int, term_timeout_seconds: float, kill_timeout_seconds: float) -> None:
if not _is_process_running(pid=pid):
return
_send_signal_if_running(pid=pid, signal_number=signal.SIGTERM)
if _wait_for_process_exit(pid=pid, timeout_seconds=term_timeout_seconds):
return
_send_signal_if_running(pid=pid, signal_number=signal.SIGKILL)
if _wait_for_process_exit(pid=pid, timeout_seconds=kill_timeout_seconds):
return
raise RuntimeError(f"Timed out waiting for process {pid} to exit")
def _find_listening_pids_for_port(*, port: int) -> list[int]:
if not isinstance(port, int):
raise TypeError(f"port must be an int, got {type(port)}")
if port <= 0 or port > 65535:
raise ValueError(f"port must be between 1 and 65535, got: {port}")
completed = subprocess.run(
["lsof", f"-tiTCP:{port}", "-sTCP:LISTEN"],
capture_output=True,
check=False,
text=True,
)
if completed.returncode == 1:
return []
if completed.returncode != 0:
stderr = completed.stderr.strip()
stdout = completed.stdout.strip()
raise RuntimeError(
f"`lsof` failed for port {port}: exit={completed.returncode} stdout={stdout!r} stderr={stderr!r}"
)
seen_pids: set[int] = set()
ordered_pids: list[int] = []
for raw_line in completed.stdout.splitlines():
raw_pid = raw_line.strip()
if raw_pid == "":
continue
if not raw_pid.isdigit():
raise RuntimeError(f"`lsof` returned a non-numeric pid for port {port}: {raw_pid!r}")
pid = int(raw_pid)
if pid <= 0:
raise RuntimeError(f"`lsof` returned invalid pid for port {port}: {pid}")
if pid in seen_pids:
continue
seen_pids.add(pid)
ordered_pids.append(pid)
return ordered_pids
def _stop_namespace_port(
*,
namespace_port: NamespacePort,
dry_run: bool,
current_pid: int,
term_timeout_seconds: float,
kill_timeout_seconds: float,
) -> int:
listener_pids = _find_listening_pids_for_port(port=namespace_port.port)
stopped_count = 0
if len(listener_pids) == 0:
print(f"{namespace_port.namespace} {namespace_port.service} {namespace_port.port}: no listener")
return stopped_count
for pid in listener_pids:
if pid == current_pid:
raise RuntimeError(f"Refusing to stop this kill.py process on port {namespace_port.port}")
if dry_run:
print(
f"{namespace_port.namespace} {namespace_port.service} {namespace_port.port}: "
f"would stop pid {pid}"
)
continue
print(f"{namespace_port.namespace} {namespace_port.service} {namespace_port.port}: stopping pid {pid}")
_stop_process(
pid=pid,
term_timeout_seconds=term_timeout_seconds,
kill_timeout_seconds=kill_timeout_seconds,
)
stopped_count += 1
return stopped_count
def main() -> None:
args = _parse_args()
if args.term_timeout < 0.0:
raise RuntimeError(f"--term-timeout must be >= 0, got: {args.term_timeout}")
if args.kill_timeout < 0.0:
raise RuntimeError(f"--kill-timeout must be >= 0, got: {args.kill_timeout}")
namespace_ports = _load_namespace_ports()
if len(namespace_ports) == 0:
print("No saved namespace launch profiles found.")
return
stopped_count = 0
current_pid = os.getpid()
for namespace_port in namespace_ports:
stopped_count += _stop_namespace_port(
namespace_port=namespace_port,
dry_run=args.dry_run,
current_pid=current_pid,
term_timeout_seconds=args.term_timeout,
kill_timeout_seconds=args.kill_timeout,
)
if args.dry_run:
print("Dry run complete.")
return
print(f"Stopped {stopped_count} listener process(es).")
if __name__ == "__main__":
main()