Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix port selection #1229

Merged
merged 7 commits into from
Mar 6, 2023
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
18 changes: 6 additions & 12 deletions jupyter_server/serverapp.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
from tornado import httpserver, ioloop, web
from tornado.httputil import url_concat
from tornado.log import LogFormatter, access_log, app_log, gen_log
from tornado.netutil import bind_sockets

if not sys.platform.startswith("win"):
from tornado.netutil import bind_unix_socket
Expand Down Expand Up @@ -2410,17 +2411,12 @@ def _bind_http_server_tcp(self):

def _find_http_port(self):
"""Find an available http port."""
pat = re.compile("([a-f0-9:]+:+)+[a-f0-9]*")
success = None
success = False
port = self.port
for port in random_ports(self.port, self.port_retries + 1):
tmp_sock = (
socket.socket()
if pat.match(self.ip) is None
else socket.socket(family=socket.AF_INET6)
)
try:
tmp_sock.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, b"\0" * 8)
tmp_sock.bind((self.ip, port))
sockets = bind_sockets(port, self.ip)
sockets[0].close()
except OSError as e:
if e.errno == errno.EADDRINUSE:
if self.port_retries:
Expand All @@ -2439,11 +2435,9 @@ def _find_http_port(self):
else:
raise
else:
self.port = port
success = True
self.port = port
break
finally:
tmp_sock.close()
if not success:
if self.port_retries:
self.log.critical(
Expand Down
4 changes: 3 additions & 1 deletion tests/unix_sockets/test_serverapp_integration.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import os
import platform
import shlex
import stat
import subprocess
Expand All @@ -17,7 +18,8 @@

# Skip this module if on Windows. Unix sockets are not available on Windows.
pytestmark = pytest.mark.skipif(
sys.platform.startswith("win"), reason="Unix sockets are not available on Windows."
sys.platform.startswith("win") or platform.python_implementation() == "PyPy",
reason="Unix sockets are not supported.",
)


Expand Down