Skip to content

Commit 64f87ba

Browse files
committed
remove changes
1 parent a2ae7ce commit 64f87ba

File tree

3 files changed

+6
-104
lines changed

3 files changed

+6
-104
lines changed

src/lightning/fabric/plugins/environments/lightning.py

Lines changed: 6 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,11 @@
1515
import os
1616
import socket
1717

18-
from filelock import FileLock
1918
from typing_extensions import override
2019

2120
from lightning.fabric.plugins.environments.cluster_environment import ClusterEnvironment
2221
from lightning.fabric.utilities.rank_zero import rank_zero_only
2322

24-
BASE_PORT = 10000
25-
MAX_PORT = 65000
26-
STEP = 20
27-
LOCK_FILE = "lightning_ports.lock"
28-
2923

3024
class LightningEnvironment(ClusterEnvironment):
3125
"""The default environment used by Lightning for a single node or free cluster (not managed).
@@ -111,57 +105,15 @@ def teardown(self) -> None:
111105
del os.environ["WORLD_SIZE"]
112106

113107

114-
def find_free_network_port(base: int = BASE_PORT, step: int = STEP) -> int:
108+
def find_free_network_port() -> int:
115109
"""Finds a free port on localhost.
116110
117111
It is useful in single-node training when we don't want to connect to a real main node but have to set the
118112
`MASTER_PORT` environment variable.
119113
120114
"""
121-
PL_FORCE_DETERMINISTIC_PORTS = os.environ.get("PL_FORCE_DETERMINISTIC_PORTS", "0")
122-
123-
if PL_FORCE_DETERMINISTIC_PORTS == "0":
124-
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
125-
s.bind(("", 0))
126-
port = s.getsockname()[1]
127-
s.close()
128-
return port
129-
130-
# use the last assigned port + step strategy with a file lock to avoid race conditions
131-
lock_path = os.path.join(os.getcwd(), LOCK_FILE)
132-
os.makedirs(os.path.dirname(lock_path), exist_ok=True)
133-
134-
with FileLock(lock_path + ".lock"):
135-
# read used ports
136-
if os.path.exists(lock_path):
137-
with open(lock_path) as f:
138-
used = [int(x.strip()) for x in f if x.strip()]
139-
else:
140-
used = []
141-
142-
candidate = base if not used else used[-1] + step
143-
if candidate > MAX_PORT:
144-
candidate = base
145-
146-
tries = 0
147-
max_tries = (MAX_PORT - base) // step
148-
while (not is_port_available(candidate) or candidate in used) and tries < max_tries:
149-
candidate += step
150-
if candidate > MAX_PORT:
151-
candidate = base
152-
tries += 1
153-
154-
if tries >= max_tries:
155-
raise RuntimeError("No free port found in range")
156-
157-
# write the new port to the file
158-
with open(lock_path, "a") as f:
159-
f.write(f"{candidate}\n")
160-
161-
return candidate
162-
163-
164-
def is_port_available(port: int) -> bool:
165-
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
166-
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
167-
return s.connect_ex(("localhost", port)) != 0
115+
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
116+
s.bind(("", 0))
117+
port = s.getsockname()[1]
118+
s.close()
119+
return port

tests/tests_fabric/conftest.py

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,6 @@ def restore_env_variables():
6666
"CUDA_MODULE_LOADING", # leaked by PyTorch
6767
"CRC32C_SW_MODE", # set by tensorboardX
6868
"OMP_NUM_THREADS", # set by our launchers
69-
"PL_FORCE_DETERMINISTIC_PORTS", # to force deterministic behavior in tests
7069
# set by torchdynamo
7170
"TRITON_CACHE_DIR",
7271
"TORCHINDUCTOR_CACHE_DIR",
@@ -137,28 +136,6 @@ def reset_deterministic_algorithm():
137136
torch.use_deterministic_algorithms(False)
138137

139138

140-
@pytest.fixture(scope="session", autouse=True)
141-
def cleanup_lightning_ports():
142-
"""Delete Lightning deterministic port files after all tests."""
143-
from lightning.fabric.plugins.environments.lightning import LOCK_FILE
144-
145-
# Ensure deterministic ports in tests
146-
os.environ["PL_FORCE_DETERMINISTIC_PORTS"] = "1"
147-
148-
yield
149-
150-
files_to_remove = [
151-
LOCK_FILE, # the port storage file
152-
LOCK_FILE + ".lock", # the filelock lock file
153-
]
154-
for f in files_to_remove:
155-
try:
156-
if os.path.exists(f):
157-
os.remove(f)
158-
except Exception as e:
159-
print(f"Warning: failed to remove {f}: {e}")
160-
161-
162139
@pytest.fixture
163140
def reset_cudnn_benchmark():
164141
"""Ensures that the `torch.backends.cudnn.benchmark` setting gets reset before the next test runs."""
@@ -221,8 +198,6 @@ def leave_no_artifacts_behind():
221198
yield
222199
files_after = {p for p in tests_root.rglob("*") if "__pycache__" not in p.parts}
223200
difference = files_after - files_before
224-
# ignore lightning port files
225-
difference = {f for f in difference if not f.name.startswith("lightning_ports.lock")}
226201
difference = {str(f.relative_to(tests_root)) for f in difference}
227202
# ignore the .coverage files
228203
difference = {f for f in difference if not f.endswith(".coverage")}

tests/tests_pytorch/conftest.py

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ def restore_env_variables():
9494
"TF_CPP_MIN_LOG_LEVEL",
9595
"TF_GRPC_DEFAULT_OPTIONS",
9696
"XLA_FLAGS",
97-
"PL_FORCE_DETERMINISTIC_PORTS", # to force deterministic behavior in tests
9897
"TORCHINDUCTOR_CACHE_DIR", # leaked by torch.compile
9998
# TensorFlow and TPU related variables
10099
"TF2_BEHAVIOR",
@@ -139,28 +138,6 @@ def reset_deterministic_algorithm():
139138
torch.use_deterministic_algorithms(False)
140139

141140

142-
@pytest.fixture(scope="session", autouse=True)
143-
def cleanup_lightning_ports():
144-
"""Delete Lightning deterministic port files after all tests."""
145-
from lightning.fabric.plugins.environments.lightning import LOCK_FILE
146-
147-
# Ensure deterministic ports in tests
148-
os.environ["PL_FORCE_DETERMINISTIC_PORTS"] = "1"
149-
150-
yield
151-
152-
files_to_remove = [
153-
LOCK_FILE, # the port storage file
154-
LOCK_FILE + ".lock", # the filelock lock file
155-
]
156-
for f in files_to_remove:
157-
try:
158-
if os.path.exists(f):
159-
os.remove(f)
160-
except Exception as e:
161-
print(f"Warning: failed to remove {f}: {e}")
162-
163-
164141
@pytest.fixture(autouse=True)
165142
def thread_police_duuu_daaa_duuu_daaa():
166143
"""Attempts to stop left-over threads to avoid test interactions."""
@@ -349,8 +326,6 @@ def leave_no_artifacts_behind():
349326
yield
350327
files_after = {p for p in tests_root.rglob("*") if "__pycache__" not in p.parts}
351328
difference = files_after - files_before
352-
# ignore lightning port files
353-
difference = {f for f in difference if not f.name.startswith("lightning_ports.lock")}
354329
difference = {str(f.relative_to(tests_root)) for f in difference}
355330
# ignore the .coverage files
356331
difference = {f for f in difference if not f.endswith(".coverage")}

0 commit comments

Comments
 (0)