fix: release the dev backend socket when no worker can serve it

Merged
MA
24d ago
+366-0

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)

  • ParentBoundGranian closes the listening socket when a worker the supervisor did not stop exits and no live worker remains (guarded by interrupt_by_parent, a live-worker check and a spawn-generation counter under an RLock), re-creates it in _spawn_worker for the next spawn, and closes it in shutdown().
  • The closed socket object is kept in _sso because granian 2.7.4 (the declared minimum) calls _sso.detach() unguarded; _init_shared_socket pins bind_port so 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 on main and pass here (the restart one is the #7114 guard and passes on both).
  • pyright 0 errors; ruff / format clean; tests/units/utils 714 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_6s at +10 s and +24 s after the break; this branch → CONNECTION_REFUSED in 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() sets SO_REUSEADDR/SO_REUSEPORT, so the re-bind survives the TIME_WAIT sockets a dead worker leaves; SocketHolder does 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() raises OSError out of granian's reload loop and reflex run exits with a traceback where it used to hang.
  • reflex run ignoring 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