Merge PR #709: fix: close log file handles to prevent resource leaks
Authored by memosr. Fixes bare open() calls in browser_tool.py and unclosed log file handles in rl_training_tool.py.
This commit is contained in:
commit
cb6b70bbfb
2 changed files with 19 additions and 4 deletions
|
|
@ -1745,7 +1745,7 @@ def cleanup_browser(task_id: Optional[str] = None) -> None:
|
||||||
pid_file = os.path.join(socket_dir, f"{session_name}.pid")
|
pid_file = os.path.join(socket_dir, f"{session_name}.pid")
|
||||||
if os.path.isfile(pid_file):
|
if os.path.isfile(pid_file):
|
||||||
try:
|
try:
|
||||||
daemon_pid = int(open(pid_file).read().strip())
|
daemon_pid = int(Path(pid_file).read_text().strip())
|
||||||
os.kill(daemon_pid, signal.SIGTERM)
|
os.kill(daemon_pid, signal.SIGTERM)
|
||||||
logger.debug("Killed daemon pid %s for %s", daemon_pid, session_name)
|
logger.debug("Killed daemon pid %s for %s", daemon_pid, session_name)
|
||||||
except (ProcessLookupError, ValueError, PermissionError, OSError):
|
except (ProcessLookupError, ValueError, PermissionError, OSError):
|
||||||
|
|
|
||||||
|
|
@ -323,7 +323,10 @@ async def _spawn_training_run(run_state: RunState, config_path: Path):
|
||||||
# Step 1: Start the Atropos API server (run-api)
|
# Step 1: Start the Atropos API server (run-api)
|
||||||
print(f"[{run_id}] Starting Atropos API server (run-api)...")
|
print(f"[{run_id}] Starting Atropos API server (run-api)...")
|
||||||
|
|
||||||
api_log_file = open(api_log, "w")
|
# File must stay open while the subprocess runs; we store the handle
|
||||||
|
# on run_state so _stop_training_run() can close it when done.
|
||||||
|
api_log_file = open(api_log, "w") # closed by _stop_training_run
|
||||||
|
run_state.api_log_file = api_log_file
|
||||||
run_state.api_process = subprocess.Popen(
|
run_state.api_process = subprocess.Popen(
|
||||||
["run-api"],
|
["run-api"],
|
||||||
stdout=api_log_file,
|
stdout=api_log_file,
|
||||||
|
|
@ -344,7 +347,8 @@ async def _spawn_training_run(run_state: RunState, config_path: Path):
|
||||||
# Step 2: Start the Tinker trainer
|
# Step 2: Start the Tinker trainer
|
||||||
print(f"[{run_id}] Starting Tinker trainer: launch_training.py --config {config_path}")
|
print(f"[{run_id}] Starting Tinker trainer: launch_training.py --config {config_path}")
|
||||||
|
|
||||||
trainer_log_file = open(trainer_log, "w")
|
trainer_log_file = open(trainer_log, "w") # closed by _stop_training_run
|
||||||
|
run_state.trainer_log_file = trainer_log_file
|
||||||
run_state.trainer_process = subprocess.Popen(
|
run_state.trainer_process = subprocess.Popen(
|
||||||
[sys.executable, "launch_training.py", "--config", str(config_path)],
|
[sys.executable, "launch_training.py", "--config", str(config_path)],
|
||||||
stdout=trainer_log_file,
|
stdout=trainer_log_file,
|
||||||
|
|
@ -384,7 +388,8 @@ async def _spawn_training_run(run_state: RunState, config_path: Path):
|
||||||
|
|
||||||
print(f"[{run_id}] Starting environment: {env_info.file_path} serve")
|
print(f"[{run_id}] Starting environment: {env_info.file_path} serve")
|
||||||
|
|
||||||
env_log_file = open(env_log, "w")
|
env_log_file = open(env_log, "w") # closed by _stop_training_run
|
||||||
|
run_state.env_log_file = env_log_file
|
||||||
run_state.env_process = subprocess.Popen(
|
run_state.env_process = subprocess.Popen(
|
||||||
[sys.executable, str(env_info.file_path), "serve", "--config", str(config_path)],
|
[sys.executable, str(env_info.file_path), "serve", "--config", str(config_path)],
|
||||||
stdout=env_log_file,
|
stdout=env_log_file,
|
||||||
|
|
@ -480,6 +485,16 @@ def _stop_training_run(run_state: RunState):
|
||||||
if run_state.status == "running":
|
if run_state.status == "running":
|
||||||
run_state.status = "stopped"
|
run_state.status = "stopped"
|
||||||
|
|
||||||
|
# Close log file handles that were opened for subprocess stdout.
|
||||||
|
for attr in ("env_log_file", "trainer_log_file", "api_log_file"):
|
||||||
|
fh = getattr(run_state, attr, None)
|
||||||
|
if fh is not None:
|
||||||
|
try:
|
||||||
|
fh.close()
|
||||||
|
except Exception:
|
||||||
|
pass
|
||||||
|
setattr(run_state, attr, None)
|
||||||
|
|
||||||
|
|
||||||
# ============================================================================
|
# ============================================================================
|
||||||
# Environment Discovery Tools
|
# Environment Discovery Tools
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue