Skip to content
Open
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
4 changes: 2 additions & 2 deletions src/pycharting/core/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def find_free_port(start_port: int | None = None, end_port: int | None = None) -
if start_port is None:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(("", 0))
s.bind(("127.0.0.1", 0))
return s.getsockname()[1]
Comment on lines 78 to 82

if end_port is None:
Expand All @@ -87,7 +87,7 @@ def find_free_port(start_port: int | None = None, end_port: int | None = None) -
for port in range(start_port, end_port):
try:
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(("", port))
s.bind(("127.0.0.1", port))
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
return port
Comment on lines 89 to 92
except OSError:
Expand Down
4 changes: 2 additions & 2 deletions tests/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ def test_raises_on_no_free_port(self):
"""Test that RuntimeError is raised when no port is free."""
import socket

# Bind on the same interface find_free_port scans ("" / all interfaces).
# Bind on the same interface find_free_port scans ("127.0.0.1").
# On Windows, 127.0.0.1:port does not conflict with 0.0.0.0:port, so the
# occupied port must be claimed on the same address to be seen as taken.
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind(("", 0))
s.bind(("127.0.0.1", 0))
occupied_port = s.getsockname()[1]
with pytest.raises(RuntimeError, match="No free port found"):
find_free_port(occupied_port, occupied_port + 1)
Expand Down
Loading