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
10 changes: 10 additions & 0 deletions lib/iris/src/iris/cluster/platform/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,11 @@

import datetime
import logging
import os
import socket
import threading
import uuid
from pathlib import Path
from collections.abc import Callable
from contextlib import AbstractContextManager
from dataclasses import dataclass, field
Expand Down Expand Up @@ -95,9 +97,17 @@ def find_free_port(start: int = -1) -> int:
s.bind(("", 0))
return s.getsockname()[1]
for port in range(start, start + 1000):
lock = Path(f"/tmp/iris/port_{port}")
try:
os.kill(int(lock.read_text()), 0)
continue # port locked by a live process
except (FileNotFoundError, ValueError, ProcessLookupError, PermissionError):
pass
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
try:
s.bind(("", port))
lock.parent.mkdir(parents=True, exist_ok=True)
lock.write_text(str(os.getpid()))
return port
except OSError:
continue
Expand Down
2 changes: 1 addition & 1 deletion lib/iris/src/iris/cluster/platform/coreweave.py
Original file line number Diff line number Diff line change
Expand Up @@ -1500,7 +1500,7 @@ def _coreweave_tunnel(
exits (e.g. konnectivity timeout), it is relaunched automatically.
"""
if local_port is None:
local_port = find_free_port()
local_port = find_free_port(start=10000)

proc: subprocess.Popen | None = None

Expand Down
2 changes: 1 addition & 1 deletion lib/iris/src/iris/cluster/platform/gcp.py
Original file line number Diff line number Diff line change
Expand Up @@ -1609,7 +1609,7 @@ def _gcp_tunnel(
Picks a free port automatically if none is specified.
"""
if local_port is None:
local_port = find_free_port()
local_port = find_free_port(start=10000)

labels = Labels(label_prefix)
label_filter = f"labels.{labels.iris_controller}=true AND status=RUNNING"
Expand Down
Loading