Repository navigation
fix(workspace): report an out-of-range port as not available - #6587
Conversation
find_available_tcp_port() returns -1 when no port in its range is free, and socket.bind raises OverflowError (not OSError) for ports outside 0-65535, so DockerWorkspace failed with "bind(): port must be 0-65535" instead of its "Port ... is not available" error. Treat OverflowError as an unavailable port.
REST API breakage checks (OpenAPI) — ✅ PASSEDResult: ✅ PASSED |
all-hands-bot
left a comment
There was a problem hiding this comment.
This review was posted by an AI agent (OpenHands).
Scope
The change touches openhands-workspace/openhands/workspace/docker/workspace.py, which this repository owns (workspace backends live here per AGENTS.md and .agents/skills/custom-codereview-guide.md). No product or architecture decision is needed; proceeding with the technical review.
Verification (current head 6bdda74)
- Root cause confirmed: reproduced in this workspace that
socket.bind(('0.0.0.0', -1))raisesOverflowErrorandisinstance(e, OSError)isFalse, so the previousexcept OSErrorlet the low-levelbind(): port must be 0-65535.escapecheck_port_available(). Catching(OSError, OverflowError)restores the intendedRuntimeError("Port <port> is not available")path in_start_container. - Shared helper claim confirmed:
check_port_availableis defined once in the docker module and imported byapptainer/workspace.py(line 170, 174) andagent_sandbox/workspace.py(line 271), so all three backends get the fix from this one-line change. There are no othersocket.bindcall sites or other definitions offind_available_tcp_portin the repo. - Base vs head behavior compared (in-memory, no tracked files modified): with the old
except OSErroronly, bothfind_available_tcp_port() == -1and an explicit out-of-rangehost_port=99999raiseOverflowErrorduring construction; on this head both raiseRuntimeError(Port -1 is not available,Port 99999 is not available) before anydockercommand is invoked. This matches the linked issue #6348 item ("an exhausted port range (find_available_tcp_port() == -1) raises OverflowError from socket.bind instead of a clear RuntimeError") and its pinned test. - Tests:
uv run pytest tests/workspace/test_docker_workspace.py→ 24 passed, including the newtest_no_free_port_raises_runtime_error, which matches the issue's pinned test (finds no free port, assertsRuntimeErrorand that no docker command runs). Fulluv run pytest tests/workspace→ 253 passed. - Lint/types:
uv run pre-commit run --fileson both changed files passes (ruff format, ruff lint, pycodestyle, pyright). The single inline comment on the except clause is appropriate — it records a non-obvious fact (OverflowError means out-of-range port) that the code alone doesn't convey. - CI on this head: all checks pass, including
workspace-tests,sdk-tests,pre-commit, and the OpenAPI/compatibility gates.
Linked issue state (re-read at review time)
Issue #6348 is open with labels bug, priority:medium, ready-for-dev; this PR addresses the pinned port item as described. The PR body's HUMAN section is present (placeholder), and CI's PR-description validation is passing.
Non-blocking note (existing behavior, not introduced here)
On the new unavailable-port path, check_port_available still sleeps 0.1s before returning False (pre-existing behavior, now also applied to the OverflowError case), so the user-visible error surfaces ~100ms later. This is negligible for a startup-time failure and does not affect the merge decision.
No material correctness, security, compatibility, or acceptance-criterion defects found.
✅ APPROVED
HUMAN:
just fixing a bug
AGENT:
Why
find_available_tcp_port()returns-1when none of the ports it tries is free._start_containerthen callscheck_port_available(-1), andsocket.bindraisesOverflowError: bind(): port must be 0-65535., which is not anOSError, so it escapescheck_port_availableand the user sees that low-level error instead of the workspace's ownRuntimeError("Port ... is not available"). The same happens for ahost_portoutside 0-65535.Summary
check_port_availablealso catchesOverflowErrorand reports the port as unavailable. Docker, Apptainer and agent-sandbox workspaces share this helper.-1and checks nodockercommand runs.Issue Number
Part of #6348 (item: "an exhausted port range (find_available_tcp_port() == -1) raises OverflowError from socket.bind instead of a clear RuntimeError").
How to Test
test_no_free_port_raises_runtime_errorfails onmain(OverflowError) and passes here (RuntimeError: Port -1 is not available); the file passes (24).Type
🐳 Agent Server images for this PR — GHCR package, pull/run commands, and all pushed tags (click to expand)
• GHCR package: https://github.com/OpenHands/agent-sdk/pkgs/container/agent-server
Variants & Base Images
eclipse-temurin:17-jdkpython-node-runtimepython-node-runtimepython-node-runtimegolang:1.21-bookwormPull (multi-arch manifest)
# Each variant is a multi-arch manifest supporting both amd64 and arm64 docker pull ghcr.io/openhands/agent-server:6bdda74-pythonRun
All tags pushed for this build
About Multi-Architecture Support
6bdda74-python) is a multi-arch manifest supporting both amd64 and arm646bdda74-python-amd64) are also available if needed