Fix #840: Use SO_REUSEADDR in port probe to ignore TIME_WAIT#845
Fix #840: Use SO_REUSEADDR in port probe to ignore TIME_WAIT#845kishansaaai wants to merge 2 commits into
Conversation
On restart, sockets can remain in TIME_WAIT, preventing serve_cmd._is_port_free() from recognizing the port as available. This sets SO_REUSEADDR on POSIX systems so the probe matches uvicorn and Next.js behavior and correctly reclaims the preferred ports.
|
✅ Health: 7.6 (unchanged) 📋 At a glance Files & modules (2)
🩹 Review priority (files here with the most recent bug-fix history — defects cluster, so review these first)
🔎 More signals (2)🔥 Hotspot touched (1)
🔗 Hidden coupling (1 file)
📊 Full report · ⭐ Star Repowise · 📥 Install bot · Last updated 2026-07-16 18:25 UTC |
|
Nice fix, the root-cause analysis is spot on and guarding One thing to fix before merge: the new test fails on Windows. Please guard it so the test matches the POSIX-only scope of the fix: @pytest.mark.skipif(os.name == "nt", reason="SO_REUSEADDR probe fix is POSIX-only (issue #840)")
def test_is_port_free_ignores_time_wait() -> None:
...That also documents why the behavior is POSIX-scoped. Thanks for the clean writeup on the issue! |
Summary
This PR fixes the issue where
repowise servewould silently fallback to an alternative port (e.g.7338or3001) during quick restarts due to the preferred ports being temporarily held in aTIME_WAITstate by the operating system.Root Cause
The
_is_port_freecheck used a plainsocket.bind()probe. When a previous instance of the server exits, the listening sockets linger in aTIME_WAITstate for ~60s. A standard probe treats these ports as "in use", even though actual web servers (likeuvicornand Next.js) configureSO_REUSEADDRto safely rebind them immediately.This resulted in a silent fallback that broke the UI proxy configuration, causing an
ECONNREFUSEDerror loop as the Next.js UI continued attempting to reach the API on the preferred port (7337) while the backend API was stealthily moved to7338.Changes
_is_port_freeinpackages/cli/src/repowise/cli/commands/serve_cmd.pyto enableSO_REUSEADDRon POSIX systems (os.name != "nt"). This accurately aligns the pre-flight probe with the real bind semantics of the downstream servers.test_is_port_free_ignores_time_waitintests/unit/cli/test_serve_port_fallback.pyto assert thatTIME_WAITstates (simulated via an active close sequence) are properly ignored by the probe.Related Issues
Fixes #840
Test Plan
pytest tests/unit/cli/test_serve_port_fallback.pyto verify the regression test logic locally.