Skip to content

Commit bf3786e

Browse files
author
Dylan Huang
committed
Add signal handler to manage zombie processes in eval_watcher.py
- Implemented a signal handler to automatically reap zombie child processes, preventing accumulation and potential resource leaks. - Enhanced process management by setting up the signal handler for SIGCHLD if available, ensuring better stability during evaluation execution.
1 parent dab44da commit bf3786e

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

eval_protocol/pytest/eval_watcher.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,25 @@
3939
LOCK_NAME = "eval_watcher"
4040

4141

42+
# Signal handler to automatically reap zombie processes
43+
def _reap_zombies(signum, frame):
44+
"""Reap zombie child processes to prevent them from accumulating."""
45+
try:
46+
while True:
47+
# Wait for any child process, but don't block
48+
pid, status = os.waitpid(-1, os.WNOHANG)
49+
if pid == 0: # No more children to reap
50+
break
51+
except OSError:
52+
# No child processes
53+
pass
54+
55+
56+
# Set up signal handler for SIGCHLD if available
57+
if hasattr(signal, "SIGCHLD"):
58+
signal.signal(signal.SIGCHLD, _reap_zombies)
59+
60+
4261
def get_eval_protocol_dir() -> Path:
4362
"""Get the evaluation protocol directory for lock files."""
4463
return Path(find_eval_protocol_dir())
@@ -184,6 +203,7 @@ def _start_watcher_process(check_interval: float) -> Optional[int]:
184203
stdout=subprocess.DEVNULL,
185204
stderr=subprocess.DEVNULL,
186205
stdin=subprocess.DEVNULL,
206+
close_fds=True,
187207
)
188208

189209
return process.pid

0 commit comments

Comments
 (0)