Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def check_port_available(port: int) -> bool:
try:
sock.bind(("0.0.0.0", port))
return True
except OSError:
except (OSError, OverflowError): # OverflowError: port outside 0-65535
time.sleep(0.1)
return False
finally:
Expand Down
16 changes: 16 additions & 0 deletions tests/workspace/test_docker_workspace.py
Original file line number Diff line number Diff line change
Expand Up @@ -387,3 +387,19 @@ def fake_execute_command(cmd, *args, **kwargs):
del excinfo
gc.collect() # let __del__ run while execute_command is still faked
assert stopped_during_startup


def test_no_free_port_raises_runtime_error(monkeypatch):
"""find_available_tcp_port() returns -1 when no port in range is free."""
monkeypatch.setattr(
"openhands.workspace.docker.workspace.find_available_tcp_port", lambda: -1
)
execute_command = MagicMock()
monkeypatch.setattr(
"openhands.workspace.docker.workspace.execute_command", execute_command
)

with pytest.raises(RuntimeError, match="Port -1 is not available"):
DockerWorkspace(server_image="test:latest")

execute_command.assert_not_called()
Loading