Skip to content

Fix #840: Use SO_REUSEADDR in port probe to ignore TIME_WAIT#845

Open
kishansaaai wants to merge 1 commit into
repowise-dev:mainfrom
kishansaaai:fix-840-time-wait-port
Open

Fix #840: Use SO_REUSEADDR in port probe to ignore TIME_WAIT#845
kishansaaai wants to merge 1 commit into
repowise-dev:mainfrom
kishansaaai:fix-840-time-wait-port

Conversation

@kishansaaai

Copy link
Copy Markdown
Contributor

Summary

This PR fixes the issue where repowise serve would silently fallback to an alternative port (e.g. 7338 or 3001) during quick restarts due to the preferred ports being temporarily held in a TIME_WAIT state by the operating system.

Root Cause

The _is_port_free check used a plain socket.bind() probe. When a previous instance of the server exits, the listening sockets linger in a TIME_WAIT state for ~60s. A standard probe treats these ports as "in use", even though actual web servers (like uvicorn and Next.js) configure SO_REUSEADDR to safely rebind them immediately.

This resulted in a silent fallback that broke the UI proxy configuration, causing an ECONNREFUSED error loop as the Next.js UI continued attempting to reach the API on the preferred port (7337) while the backend API was stealthily moved to 7338.

Changes

  • Modified _is_port_free in packages/cli/src/repowise/cli/commands/serve_cmd.py to enable SO_REUSEADDR on POSIX systems (os.name != "nt"). This accurately aligns the pre-flight probe with the real bind semantics of the downstream servers.
  • Appended a regression test test_is_port_free_ignores_time_wait in tests/unit/cli/test_serve_port_fallback.py to assert that TIME_WAIT states (simulated via an active close sequence) are properly ignored by the probe.

Related Issues

Fixes #840

Test Plan

  • Run pytest tests/unit/cli/test_serve_port_fallback.py to verify the regression test logic locally.
  • Tested clean binding behavior when forcefully stopping and starting the server consecutively.

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.
@repowise-bot

repowise-bot Bot commented Jul 15, 2026

Copy link
Copy Markdown

✅ Health: 7.6 (unchanged)

📋 At a glance
1 hotspot touched · 5 co-change pairs left out · 2 files with recent fix history.

Files & modules (2)
  • packages (1 file)
    • .../commands/serve_cmd.py
  • tests (1 file)
    • .../cli/test_serve_port_fallback.py

🩹 Review priority (files here with the most recent bug-fix history — defects cluster, so review these first)

🔎 More signals (2)

🔥 Hotspot touched (1)

  • .../commands/serve_cmd.py — 10 commits/90d, 2 dependents · primary owner: Raghav Chamadiya (75%)

🔗 Hidden coupling (1 file)

  • .../commands/serve_cmd.py co-changes with these files (not in this PR):
    • .../cli/helpers.py (4× — 🟢 routine)
    • pyproject.toml (4× — 🟢 routine)
    • .../server/provider_config.py (3× — 🟢 routine)
    • .../routers/git.py (3× — 🟢 routine)
    • .../server/app.py (3× — 🟢 routine)

📊 Full report · ⭐ Star Repowise · 📥 Install bot · Last updated 2026-07-15 07:29 UTC
Silence on a single PR with [skip repowise] in the title · Per-repo toggle on repowise.dev/settings?tab=bot

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] serve: preferred ports (7337/3000) not reclaimed on restart — TIME_WAIT trips a silent fallback that breaks the web UI

1 participant