fix: release the dev backend socket when no worker can serve it
Merged
MA
24d ago+366+366+012345678901234567890123456789-0-0-0123456789
All Submissions:
- Have you followed the guidelines stated in CONTRIBUTING.md file?
- Have you checked to ensure there aren't any other open Pull Requests for the desired changed?
Type of change
- Bug fix (non-breaking change which fixes an issue)
Changes To Core Features:
- Have you added an explanation of what your changes do and why you'd like us to include them?
- Have you written new tests for your core changes, as applicable?
- Have you successfully ran tests with your changes locally?
Summary
closes #7213
#7114 moved the dev backend's listening socket into the granian supervisor (ParentBoundGranian._init_shared_socket) so the port stays bound across hot reloads. When a worker dies on its own — the app module raises on import after a save — granian's watcher only logs under reload_ignore_worker_failure=True, the supervisor keeps the socket, and the kernel completes handshakes into a backlog nobody accepts from: every request hangs for the client's full timeout. 0.9.11.post1 refused instantly. Granian's shutdown also only detaches the socket object, so the port stayed bound while the reflex process lingered after a shutdown.
What changed (reflex/utils/exec.py, dev path only)
ParentBoundGraniancloses the listening socket when a worker the supervisor did not stop exits and no live worker remains (guarded byinterrupt_by_parent, a live-worker check and a spawn-generation counter under anRLock), re-creates it in_spawn_workerfor the next spawn, and closes it inshutdown().- The closed socket object is kept in
_ssobecause granian 2.7.4 (the declared minimum) calls_sso.detach()unguarded;_init_shared_socketpinsbind_portso a re-created socket reclaims the same port. - Workers stopped by the supervisor (hot reload) keep the socket bound, so #7114's behaviour is unchanged.
Tests
tests/units/utils/test_exec.py: five tests — socket released when the worker dies, kept across a supervisor-driven restart, re-bound for the next spawn, released on shutdown, and a real-supervisor test that a broken app refuses connections. Four fail onmainand pass here (the restart one is the #7114 guard and passes on both).- pyright 0 errors; ruff / format clean;
tests/units/utils714 passed; full units 9137 passed together with the other campaign fixes.
Verification against the real breakage
- Campaign probe (raw sockets, granian 2.8.3 in both venvs so reflex is the only delta): published 0.9.12a1 →
TIMEOUT_NO_REPLY_6sat +10 s and +24 s after the break; this branch →CONNECTION_REFUSEDin 0.0 s at both points and 200 again after the file is restored. - Hot reload still holds the port: 543 pings at 20 Hz across two reloads, 0 refused, 0 errors; the reviewer's own stress run (858 samples, steady → harmless edit → break → fix) shows 0 refusals during reload and refusals stopping 1.0 s after the fix is saved.
- The SIGTERM port probe on this branch matches the 0.9.11.post1 baseline (refused at +8 s / +18 s).
Notes for review
- Relies on the same granian internals #7114 already used (
_watcher,interrupt_by_parent,wrks,_spawn_worker,_sso).SocketSpec.build()setsSO_REUSEADDR/SO_REUSEPORT, so the re-bind survives the TIME_WAIT sockets a dead worker leaves;SocketHolderdoes not close the fd when dropped, so there is no double-close hazard (both verified by the reviewer). - New, unlikely failure mode: if another process grabs the port during the broken-app window,
_init_shared_socket()raisesOSErrorout of granian's reload loop andreflex runexits with a traceback where it used to hang. reflex runignoring SIGTERM sent to its pid alone (the other half of the campaign's observation) is untouched; only its port-level symptom is gone.- Independent adversarial review: approved, no blocking issues. Report: fixes/f017/REPORT.md.
🤖 Generated with Claude Code
https://claude.ai/code/session_01NeyxWsuC9hqSKq8YcEwZyY
Generated by Claude Code