-
Notifications
You must be signed in to change notification settings - Fork 4.1k
fix(wrap): replace stale-proxy detection with Vite-style port fallback #1406
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
lennney
wants to merge
12
commits into
headroomlabs-ai:main
Choose a base branch
from
lennney:fix/wrap-port-conflict-v2
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+601
−167
Open
Changes from 8 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
5c33263
fix(wrap): detect stale proxy on target port and auto-cleanup before …
lennney e0d9a54
fix(wrap): replace stale-proxy detection with Vite-style port fallback
lennney 40a7a2d
fix(wrap): propagate actual_port to all callers
lennney 5c4e938
fix(codex): route port resolution through _ensure_proxy, not early _f…
lennney f27f439
ci: trigger re-run with fixed PR governance workflow
lennney dc98cba
fix(wrap): move prepare_only check before _ensure_proxy to prevent pr…
lennney 6165427
fix(wrap): resolve merge conflicts and fix codex launch-failure clean…
lennney 988851e
fix(wrap): resolve port before config injection in opencode()
lennney 7a15c2f
style: fix ruff formatting (3 files)
lennney ec2c9bd
chore: remove local Hermes workspace files from PR
lennney 4508365
fix(wrap): port overflow, marker race, type hints, test fix
lennney 884a174
docs: add boundary section, ADR directory
lennney File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| ## Description | ||
|
|
||
| Replace the 180-line process-killing approach with a 15-line Vite-style socket.bind probe: if port is busy (EADDRINUSE or EACCES), try the next available port. | ||
|
|
||
| Pure stdlib -- no /proc, no lsof, no subprocess -- works identically on Linux/macOS/Windows. | ||
|
|
||
| ## Problem | ||
|
|
||
| When `headroom wrap <agent>` is killed without proper cleanup (window close, SSH timeout, crash), the background proxy becomes orphaned and holds the port. The next `headroom wrap` on the same port would wait 30-45 seconds then fail with a confusing error. | ||
|
|
||
| This PR takes a simpler, safer approach: find the next available port. No process detection, no killing. | ||
|
|
||
| ### Related issues | ||
|
|
||
| - **#589** (Port 8787 reserved by Windows) -- partially addressed: EACCES is now skipped together with EADDRINUSE | ||
| - **#804** (Shared proxy killed by exiting session) -- already fixed upstream via `_live_proxy_clients` marker files; this PR doesn't touch that code | ||
|
|
||
| ## Type of Change | ||
|
|
||
| - [x] New feature (non-breaking change that adds functionality) | ||
| - [x] Code refactoring (no functional changes) | ||
|
|
||
| ## Changes Made | ||
|
|
||
| - `headroom/cli/wrap.py`: Add `_find_available_port()` -- socket.bind loop that skips EADDRINUSE (busy) and EACCES (reserved/privileged) ports, returns first available port in range. Replace `_ensure_proxy` port-bind check with auto-fallback call to `_find_available_port`. Remove `_ensure_port_free()` call from `_start_proxy()`. Remove 8 dead functions: `_find_process_on_port`, `_linux_find_process_on_port`, `_resolve_inode_to_pid`, `_is_headroom_proxy`, `_read_process_cmdline`, `_kill_process`, `_ensure_port_free`, `_format_unbindable_port_error`. | ||
| - `tests/test_cli/test_wrap_helpers.py`: Remove 14 old `TestEnsurePortFree` tests (mocked /proc parsing, process killing). Add 6 new `TestFindAvailablePort` tests covering: port free, first port busy, multiple busy, EACCES skipped, unexpected error propagated, range exhausted. | ||
| - `tests/test_cli/test_wrap_persistent.py`: Adapt persistence tests for `_find_available_port` mock. Rewrite unbindable-port test to use new error path. | ||
|
|
||
| Zero new dependencies. Zero changes to core proxy server, MCP, compression, or providers. | ||
|
|
||
| ## Testing | ||
|
|
||
| - [x] Unit tests pass (`python -m pytest tests/test_cli/ -v`) | ||
| - [x] New tests added for new functionality | ||
|
|
||
| ### Test Output | ||
|
|
||
| ``` | ||
| > python -m pytest tests/test_cli/test_wrap_helpers.py::TestFindAvailablePort -v --no-header | ||
| ============================= 6 passed ============================== | ||
| test_port_free_returns_same PASSED | ||
| test_port_busy_finds_next PASSED | ||
| test_multiple_busy_ports PASSED | ||
| test_propagates_unexpected_error PASSED | ||
| test_propagates_eaddrinuse_with_eacces PASSED | ||
| test_exhausts_range PASSED | ||
|
|
||
| > python -m pytest tests/test_cli/ -q | ||
| ============================= 445 passed in 8.40s ============================== | ||
| ``` | ||
|
|
||
| ## Real Behavior Proof | ||
|
|
||
| - Environment: Ubuntu 24.04 x86_64, Python 3.12.3 | ||
| - Verification: Port fallback logic verified via unit tests (all 6 `TestFindAvailablePort` tests pass). The function `_find_available_port(8787)` returns 8787 when free, 8788 when 8787 is busy. EACCES is skipped same as EADDRINUSE. Non-retryable errors (EADDRNOTAVAIL) propagate immediately. | ||
| - Cross-platform: Pure socket API -- no /proc, no lsof, no platform-specific code. Works on Linux, macOS, Windows. | ||
| - Not tested: Windows EACCES fallback (no Windows CI runner available). macOS port fallback (no macOS runner). The code path is identical across platforms since it only uses stdlib socket. | ||
|
|
||
| ## Review Readiness | ||
|
|
||
| - [x] I have performed a self-review | ||
| - [x] This PR is ready for human review | ||
|
|
||
| ## Checklist | ||
|
|
||
| - [x] My code follows the project's style guidelines | ||
| - [x] I have performed a self-review of my code | ||
| - [x] My changes generate no new warnings | ||
| - [x] I have added tests that prove my fix is effective or that my feature works | ||
| - [x] New and existing unit tests pass locally | ||
Oops, something went wrong.
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.