#74 (2/4): hapi serve runs the API and probes in one event loop #195

Open
bobby wants to merge 1 commits from 74-2-serve into 74-1-probe-app
pull from: 74-2-serve
Owner

Part of #74. Stack: 1. probe app → 2. hapi serve → 3. module boundaries → 4. pre-commit. Based on the 1/4 branch, so the diff shows only this slice.

Summary

hapi serve [--host 127.0.0.1] [--api-port 8000] [--probe-port 8081]
  platform.server.serve([Listener(api), Listener(probes)])
    asyncio.run
      uvicorn :8000  ┐ same event loop: a wedged loop fails liveness (ADR-0009)
      uvicorn :8081  ┘
    on SIGINT/SIGTERM          -> stop both
    on one listener stopping   -> stop the other

uvicorn's own per-server signal capture is turned off, because two servers in one process would chain each other's handlers. serve owns signal handling instead. just dev runs hapi serve.

Evidence

  • Before: no way to run the app.
    After: tests/test_serve.py starts hapi serve as a real process on free ports:
probes answer on the probe port only   :8081/livez -> 200, :8000/livez -> 404   PASSED
sigterm stops both listeners cleanly   exit code 0                              PASSED

These tests use a subprocess rather than calling the CLI in-process (spec Seam 2), because real ports and signals are the behaviour under test. If a port is already taken, the process exits 3 with a uvicorn traceback rather than hanging.

Merge Danger

Door: two-way

Blast Radius: none

#77 (/readyz + drain) should build on the shared shutdown here. --host defaults to 127.0.0.1; the image will need 0.0.0.0 (or a setting in #75).

🤖 Generated with Claude Code

Part of #74. Stack: 1. probe app → **2. `hapi serve`** → 3. module boundaries → 4. pre-commit. Based on the 1/4 branch, so the diff shows only this slice. ## Summary ```text hapi serve [--host 127.0.0.1] [--api-port 8000] [--probe-port 8081] platform.server.serve([Listener(api), Listener(probes)]) asyncio.run uvicorn :8000 ┐ same event loop: a wedged loop fails liveness (ADR-0009) uvicorn :8081 ┘ on SIGINT/SIGTERM -> stop both on one listener stopping -> stop the other ``` uvicorn's own per-server signal capture is turned off, because two servers in one process would chain each other's handlers. `serve` owns signal handling instead. `just dev` runs `hapi serve`. ## Evidence - **Before:** no way to run the app. **After:** `tests/test_serve.py` starts `hapi serve` as a real process on free ports: ```text probes answer on the probe port only :8081/livez -> 200, :8000/livez -> 404 PASSED sigterm stops both listeners cleanly exit code 0 PASSED ``` These tests use a subprocess rather than calling the CLI in-process (spec Seam 2), because real ports and signals are the behaviour under test. If a port is already taken, the process exits 3 with a uvicorn traceback rather than hanging. ## Merge Danger **Door:** two-way **Blast Radius:** none #77 (`/readyz` + drain) should build on the shared shutdown here. `--host` defaults to `127.0.0.1`; the image will need `0.0.0.0` (or a setting in #75). 🤖 Generated with [Claude Code](https://claude.com/claude-code)
bobby added 1 commit 2026-11-27 17:01:32 -05:00
Two uvicorn servers, the public API on :8000 and the internal probe
listener on :8081, share one event loop (ADR-0009), so a wedged loop
fails liveness. SIGINT/SIGTERM stops both; if one listener stops or
fails to start, the other stops with it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
You are not authorized to merge this pull request.
This pull request can be merged automatically.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 74-2-serve:74-2-serve
git checkout 74-2-serve
Sign in to join this conversation.