Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions backend/app/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,12 @@
SDK_DOCS_DIST = Path(__file__).resolve().parents[2] / "site"


@app.on_event("shutdown")
def stop_active_runner_processes() -> None:
"""Prevent browsers from outliving the local API process on reload."""
store.shutdown()


def safely(action):
try:
return action()
Expand Down
26 changes: 20 additions & 6 deletions backend/app/store.py
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,23 @@ def __init__(self) -> None:
self._recover_interrupted_runs()
self.tracer = configure_telemetry(TELEMETRY)

@staticmethod
def _stop_process_group(process: subprocess.Popen[str], *, force: bool = False) -> None:
"""Stop a runner and every subprocess it spawned, including browsers."""
if process.poll() is not None:
return
if os.name == "nt":
(process.kill if force else process.terminate)()
return
os.killpg(process.pid, signal.SIGKILL if force else signal.SIGTERM)

def shutdown(self) -> None:
"""Release child process groups before an API reload or shutdown."""
with self._lock:
processes = list(self._processes.values())
for process in processes:
self._stop_process_group(process)

def _recover_interrupted_runs(self) -> None:
"""Do not present orphaned in-memory pipelines as still running.

Expand Down Expand Up @@ -4757,7 +4774,7 @@ def capture_output() -> None:
self._fail(run, step.id, f"exit code {process.returncode}")
return
except subprocess.TimeoutExpired:
process.kill()
self._stop_process_group(process, force=True)
span.add_event("process.timeout", {"timeout.seconds": step.timeout_seconds})
if self._load(run_id).status == "cancelled":
return
Expand All @@ -4780,11 +4797,8 @@ def cancel(self, run_id: str) -> Run:
run = self._load(run_id)
with self._lock:
process = self._processes.get(run_id)
if process and process.poll() is None:
if os.name == "nt":
process.terminate()
else:
os.killpg(process.pid, signal.SIGTERM)
if process:
self._stop_process_group(process)
run.status, run.pid, run.current_step, run.current_phase, run.updated_at, run.finished_at = (
"cancelled",
None,
Expand Down
Loading