Hold the dev backend listen socket in the granian supervisor

Merged
FA
3mo ago
+197-1

Problem

On Linux, granian 2.8 gives each worker a bare socket spec. The worker binds with SO_REUSEPORT only after it has imported and compiled the app, and the file-watch reload stops the old worker before it spawns the new one. Nothing listens during the whole worker boot, so every request made while hot reload runs gets connection refused. Granian 2.7.4, the minimum version we pin, bound the socket in the supervisor, so this is a behaviour change we inherited.

Change

run_granian_backend (dev only) subclasses the granian server and overrides _init_shared_socket to bind once in the supervisor and pass the inheritable descriptor down to workers. This is the path granian already takes on macOS and Windows. Requests during a reload now wait in the kernel accept backlog and are answered by the new worker. Production (run_granian_backend_prod) is untouched.

Measurements

Probing /ping every 50 ms across two edits, 60-page app, full dev mode:

refused probesheld probeslongest wait
before21 to 22 (from +0.20 s to +1.26 s)0-
after022 to 241.3 s

Blank app, backend-only: 0 refused, longest wait 0.5 s. Reload cycle length is unchanged; only the failure mode changes.

Single-worker throughput, blank app /ping, best of 3: 4071 vs 4017 req/s keep-alive, 3097 vs 3059 req/s new connection per request. Noise.

Trade-off

When an edit has a syntax error the worker dies and nothing accepts. Before, requests in that state were refused immediately. Now they wait until the client times out, and once the file is fixed the queued request is answered about 0.6 s later.

https://claude.ai/code/session_01CtZAjq1esmYw7iRHd5TAbG