Boot a uvicorn server in a subprocess and tear it down on exit. ``server_module`` is e.g. ``scripts.clawcodex_api_server:app`` — passed straight through to uvicorn. ``cwd`` is ``swebench_repo`` so the ``scripts`` package import resolves.
(
*,
swebench_repo: Path,
swebench_python: str,
server_module: str,
port: int,
log_path: Path,
env: dict[str, str],
)
| 259 | |
| 260 | @contextlib.contextmanager |
| 261 | def _spawn_server( |
| 262 | *, |
| 263 | swebench_repo: Path, |
| 264 | swebench_python: str, |
| 265 | server_module: str, |
| 266 | port: int, |
| 267 | log_path: Path, |
| 268 | env: dict[str, str], |
| 269 | ) -> Iterable[subprocess.Popen[bytes]]: |
| 270 | """Boot a uvicorn server in a subprocess and tear it down on exit. |
| 271 | |
| 272 | ``server_module`` is e.g. ``scripts.clawcodex_api_server:app`` — passed |
| 273 | straight through to uvicorn. ``cwd`` is ``swebench_repo`` so the |
| 274 | ``scripts`` package import resolves. |
| 275 | """ |
| 276 | cmd = [ |
| 277 | swebench_python, |
| 278 | "-m", |
| 279 | "uvicorn", |
| 280 | server_module, |
| 281 | "--host", |
| 282 | "127.0.0.1", |
| 283 | "--port", |
| 284 | str(port), |
| 285 | "--log-level", |
| 286 | "warning", |
| 287 | ] |
| 288 | log_path.parent.mkdir(parents=True, exist_ok=True) |
| 289 | log_handle = log_path.open("wb") |
| 290 | _info(f" starting: {' '.join(cmd)} (cwd={swebench_repo})") |
| 291 | # On Windows, place uvicorn in its own process group so CTRL_BREAK_EVENT |
| 292 | # only reaches uvicorn, not the run_compare process and any sibling harness |
| 293 | # subprocess we spawn next. Without this, the signal leaks to the whole |
| 294 | # console group and aborts the next harness run with an empty log. |
| 295 | popen_kwargs: dict[str, object] = {} |
| 296 | if os.name == "nt": |
| 297 | popen_kwargs["creationflags"] = subprocess.CREATE_NEW_PROCESS_GROUP |
| 298 | proc = subprocess.Popen( # noqa: S603 |
| 299 | cmd, |
| 300 | cwd=str(swebench_repo), |
| 301 | stdout=log_handle, |
| 302 | stderr=subprocess.STDOUT, |
| 303 | env=env, |
| 304 | **popen_kwargs, |
| 305 | ) |
| 306 | try: |
| 307 | try: |
| 308 | _wait_for_health(f"http://127.0.0.1:{port}/health", timeout=30.0) |
| 309 | except RuntimeError: |
| 310 | # /health may not exist on older clawcodex_api_server.py — accept |
| 311 | # any TCP-level liveness instead. |
| 312 | _info(" /health probe failed; falling back to socket-only liveness check") |
| 313 | _wait_for_socket("127.0.0.1", port, timeout=30.0) |
| 314 | yield proc |
| 315 | finally: |
| 316 | if proc.poll() is None: |
| 317 | _info(f" stopping uvicorn (pid={proc.pid}) on port {port}") |
| 318 | try: |
no test coverage detected