Fix #840: Use SO_REUSEADDR in port probe to ignore TIME_WAIT#845
Open
kishansaaai wants to merge 1 commit into
Open
Fix #840: Use SO_REUSEADDR in port probe to ignore TIME_WAIT#845kishansaaai wants to merge 1 commit into
kishansaaai wants to merge 1 commit 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-15 07:29 UTC |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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.