Skip to content

fix(workspace): report an out-of-range port as not available - #6587

Merged
VascoSch92 merged 1 commit into
mainfrom
vasco/no-free-port-error
Nov 7, 2026
Merged

VascoSch92 merged 1 commit into
mainfrom
vasco/no-free-port-error

Conversation

@VascoSch92

@VascoSch92 VascoSch92 commented Nov 7, 2026 •

Copy link
Copy Markdown
Member

HUMAN:

just fixing a bug


AGENT:

Why

find_available_tcp_port() returns -1 when none of the ports it tries is free. _start_container then calls check_port_available(-1), and socket.bind raises OverflowError: bind(): port must be 0-65535., which is not an OSError, so it escapes check_port_available and the user sees that low-level error instead of the workspace's own RuntimeError("Port ... is not available"). The same happens for a host_port outside 0-65535.

Summary

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

uv run pytest tests/workspace/test_docker_workspace.py

test_no_free_port_raises_runtime_error fails on main (OverflowError) and passes here (RuntimeError: Port -1 is not available); the file passes (24).

Type

  • Bug fix
  • Feature
  • Refactor
  • Breaking change
  • Docs / chore

🐳 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

Variant Architectures Base Image Docs / Tags
java amd64, arm64 eclipse-temurin:17-jdk Link
python-slim amd64, arm64 python-node-runtime Link
python-minimal amd64, arm64 python-node-runtime Link
python amd64, arm64 python-node-runtime Link
golang amd64, arm64 golang:1.21-bookworm Link

Pull (multi-arch manifest)

# Each variant is a multi-arch manifest supporting both amd64 and arm64
docker pull ghcr.io/openhands/agent-server:6bdda74-python

Run

docker run -it --rm \
  -p 8000:8000 \
  --name agent-server-6bdda74-python \
  ghcr.io/openhands/agent-server:6bdda74-python

All tags pushed for this build

ghcr.io/openhands/agent-server:6bdda74-golang-amd64
ghcr.io/openhands/agent-server:6bdda74e93066379472196ac3009f2e53f2dd50f-golang-amd64
ghcr.io/openhands/agent-server:vasco-no-free-port-error-golang-amd64
ghcr.io/openhands/agent-server:6bdda74-golang_tag_1.21-bookworm-amd64
ghcr.io/openhands/agent-server:6bdda74-golang-arm64
ghcr.io/openhands/agent-server:6bdda74e93066379472196ac3009f2e53f2dd50f-golang-arm64
ghcr.io/openhands/agent-server:vasco-no-free-port-error-golang-arm64
ghcr.io/openhands/agent-server:6bdda74-golang_tag_1.21-bookworm-arm64
ghcr.io/openhands/agent-server:6bdda74-java-amd64
ghcr.io/openhands/agent-server:6bdda74e93066379472196ac3009f2e53f2dd50f-java-amd64
ghcr.io/openhands/agent-server:vasco-no-free-port-error-java-amd64
ghcr.io/openhands/agent-server:6bdda74-eclipse-temurin_tag_17-jdk-amd64
ghcr.io/openhands/agent-server:6bdda74-java-arm64
ghcr.io/openhands/agent-server:6bdda74e93066379472196ac3009f2e53f2dd50f-java-arm64
ghcr.io/openhands/agent-server:vasco-no-free-port-error-java-arm64
ghcr.io/openhands/agent-server:6bdda74-eclipse-temurin_tag_17-jdk-arm64
ghcr.io/openhands/agent-server:6bdda74-python-amd64
ghcr.io/openhands/agent-server:6bdda74e93066379472196ac3009f2e53f2dd50f-python-amd64
ghcr.io/openhands/agent-server:vasco-no-free-port-error-python-amd64
ghcr.io/openhands/agent-server:6bdda74-python-node-runtime-amd64
ghcr.io/openhands/agent-server:6bdda74-python-arm64
ghcr.io/openhands/agent-server:6bdda74e93066379472196ac3009f2e53f2dd50f-python-arm64
ghcr.io/openhands/agent-server:vasco-no-free-port-error-python-arm64
ghcr.io/openhands/agent-server:6bdda74-python-node-runtime-arm64
ghcr.io/openhands/agent-server:6bdda74-python-minimal-amd64
ghcr.io/openhands/agent-server:6bdda74e93066379472196ac3009f2e53f2dd50f-python-minimal-amd64
ghcr.io/openhands/agent-server:vasco-no-free-port-error-python-minimal-amd64
ghcr.io/openhands/agent-server:6bdda74-python-node-runtime-minimal-amd64
ghcr.io/openhands/agent-server:6bdda74-python-minimal-arm64
ghcr.io/openhands/agent-server:6bdda74e93066379472196ac3009f2e53f2dd50f-python-minimal-arm64
ghcr.io/openhands/agent-server:vasco-no-free-port-error-python-minimal-arm64
ghcr.io/openhands/agent-server:6bdda74-python-node-runtime-minimal-arm64
ghcr.io/openhands/agent-server:6bdda74-python-slim-amd64
ghcr.io/openhands/agent-server:6bdda74e93066379472196ac3009f2e53f2dd50f-python-slim-amd64
ghcr.io/openhands/agent-server:vasco-no-free-port-error-python-slim-amd64
ghcr.io/openhands/agent-server:6bdda74-python-node-runtime-slim-amd64
ghcr.io/openhands/agent-server:6bdda74-python-slim-arm64
ghcr.io/openhands/agent-server:6bdda74e93066379472196ac3009f2e53f2dd50f-python-slim-arm64
ghcr.io/openhands/agent-server:vasco-no-free-port-error-python-slim-arm64
ghcr.io/openhands/agent-server:6bdda74-python-node-runtime-slim-arm64
ghcr.io/openhands/agent-server:6bdda74-golang
ghcr.io/openhands/agent-server:6bdda74e93066379472196ac3009f2e53f2dd50f-golang
ghcr.io/openhands/agent-server:vasco-no-free-port-error-golang
ghcr.io/openhands/agent-server:6bdda74-golang_tag_1.21-bookworm
ghcr.io/openhands/agent-server:6bdda74-java
ghcr.io/openhands/agent-server:6bdda74e93066379472196ac3009f2e53f2dd50f-java
ghcr.io/openhands/agent-server:vasco-no-free-port-error-java
ghcr.io/openhands/agent-server:6bdda74-eclipse-temurin_tag_17-jdk
ghcr.io/openhands/agent-server:6bdda74-python-minimal
ghcr.io/openhands/agent-server:6bdda74e93066379472196ac3009f2e53f2dd50f-python-minimal
ghcr.io/openhands/agent-server:vasco-no-free-port-error-python-minimal
ghcr.io/openhands/agent-server:6bdda74-python-node-runtime-minimal
ghcr.io/openhands/agent-server:6bdda74-python-slim
ghcr.io/openhands/agent-server:6bdda74e93066379472196ac3009f2e53f2dd50f-python-slim
ghcr.io/openhands/agent-server:vasco-no-free-port-error-python-slim
ghcr.io/openhands/agent-server:6bdda74-python-node-runtime-slim
ghcr.io/openhands/agent-server:6bdda74-python
ghcr.io/openhands/agent-server:6bdda74e93066379472196ac3009f2e53f2dd50f-python
ghcr.io/openhands/agent-server:vasco-no-free-port-error-python
ghcr.io/openhands/agent-server:6bdda74-python-node-runtime

About Multi-Architecture Support

  • Each variant tag (e.g., 6bdda74-python) is a multi-arch manifest supporting both amd64 and arm64
  • Docker automatically pulls the correct architecture for your platform
  • Individual architecture tags (e.g., 6bdda74-python-amd64) are also available if needed

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.
@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

REST API breakage checks (OpenAPI) — ✅ PASSED

Result: ✅ PASSED

Action log

@github-actions

github-actions Bot commented Nov 7, 2026

Copy link
Copy Markdown
Contributor

Coverage

Coverage Report •
FileStmtsMissCoverMissing
openhands-workspace/openhands/workspace/docker
   workspace.py1964080%50, 148, 174, 202, 209, 221–222, 234, 261, 298, 307, 314–315, 319–320, 324, 327–328, 330–336, 339–340, 349–351, 355–357, 361, 365, 371–372, 395, 415, 432
TOTAL535672657450% 

@all-hands-bot all-hands-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)) raises OverflowError and isinstance(e, OSError) is False, so the previous except OSError let the low-level bind(): port must be 0-65535. escape check_port_available(). Catching (OSError, OverflowError) restores the intended RuntimeError("Port <port> is not available") path in _start_container.
  • Shared helper claim confirmed: check_port_available is defined once in the docker module and imported by apptainer/workspace.py (line 170, 174) and agent_sandbox/workspace.py (line 271), so all three backends get the fix from this one-line change. There are no other socket.bind call sites or other definitions of find_available_tcp_port in the repo.
  • Base vs head behavior compared (in-memory, no tracked files modified): with the old except OSError only, both find_available_tcp_port() == -1 and an explicit out-of-range host_port=99999 raise OverflowError during construction; on this head both raise RuntimeError (Port -1 is not available, Port 99999 is not available) before any docker command 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 new test_no_free_port_raises_runtime_error, which matches the issue's pinned test (finds no free port, asserts RuntimeError and that no docker command runs). Full uv run pytest tests/workspace → 253 passed.
  • Lint/types: uv run pre-commit run --files on 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

@VascoSch92
VascoSch92 merged commit 5931b5c into main Nov 7, 2026
52 of 53 checks passed
@VascoSch92
VascoSch92 deleted the vasco/no-free-port-error branch November 7, 2026 17:17
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.

2 participants