Skip to content

Commit 4d311b2

Browse files
authored
Fix port selection (#1229)
1 parent b5a6306 commit 4d311b2

File tree

2 files changed

+9
-13
lines changed

2 files changed

+9
-13
lines changed

jupyter_server/serverapp.py

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
from tornado import httpserver, ioloop, web
3636
from tornado.httputil import url_concat
3737
from tornado.log import LogFormatter, access_log, app_log, gen_log
38+
from tornado.netutil import bind_sockets
3839

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

24112412
def _find_http_port(self):
24122413
"""Find an available http port."""
2413-
pat = re.compile("([a-f0-9:]+:+)+[a-f0-9]*")
2414-
success = None
2414+
success = False
2415+
port = self.port
24152416
for port in random_ports(self.port, self.port_retries + 1):
2416-
tmp_sock = (
2417-
socket.socket()
2418-
if pat.match(self.ip) is None
2419-
else socket.socket(family=socket.AF_INET6)
2420-
)
24212417
try:
2422-
tmp_sock.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, b"\0" * 8)
2423-
tmp_sock.bind((self.ip, port))
2418+
sockets = bind_sockets(port, self.ip)
2419+
sockets[0].close()
24242420
except OSError as e:
24252421
if e.errno == errno.EADDRINUSE:
24262422
if self.port_retries:
@@ -2439,11 +2435,9 @@ def _find_http_port(self):
24392435
else:
24402436
raise
24412437
else:
2442-
self.port = port
24432438
success = True
2439+
self.port = port
24442440
break
2445-
finally:
2446-
tmp_sock.close()
24472441
if not success:
24482442
if self.port_retries:
24492443
self.log.critical(

tests/unix_sockets/test_serverapp_integration.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import platform
23
import shlex
34
import stat
45
import subprocess
@@ -17,7 +18,8 @@
1718

1819
# Skip this module if on Windows. Unix sockets are not available on Windows.
1920
pytestmark = pytest.mark.skipif(
20-
sys.platform.startswith("win"), reason="Unix sockets are not available on Windows."
21+
sys.platform.startswith("win") or platform.python_implementation() == "PyPy",
22+
reason="Unix sockets are not supported.",
2123
)
2224

2325

0 commit comments

Comments
 (0)