Feedback: repair stop command port targeting.

This commit is contained in:
Kris Wilson 2020-04-28 23:45:08 -07:00
parent 9bda4c61b9
commit 4af22581fc
2 changed files with 92 additions and 10 deletions

View File

@ -510,21 +510,36 @@ class NbserverStopApp(JupyterApp):
def shutdown_server(self, server):
return shutdown_server(server, log=self.log)
def _shutdown_or_exit(self, target_endpoint, server):
print("Shutting down server on %s..." % target_endpoint)
if not self.shutdown_server(server):
sys.exit("Could not stop server on %s" % target_endpoint)
def start(self):
servers = list(list_running_servers(self.runtime_dir))
if not servers:
self.exit("There are no running servers")
self.exit("There are no running servers (per %s)" % self.runtime_dir)
for server in servers:
if server.get('sock') == self.sock or server['port'] == self.port:
print("Shutting down server on %s..." % self.sock or self.port)
if not self.shutdown_server(server):
sys.exit("Could not stop server")
return
if self.sock:
sock = server.get('sock', None)
if sock and sock == self.sock:
self._shutdown_or_exit(sock, server)
return
elif self.port:
port = server.get('port', None)
if port == self.port:
self._shutdown_or_exit(port, server)
return
else:
print("There is currently no server running on port {}".format(self.port), file=sys.stderr)
current_endpoint = self.sock or self.port
print(
"There is currently no server running on {}".format(current_endpoint),
file=sys.stderr
)
print("Ports/sockets currently in use:", file=sys.stderr)
for server in servers:
print(" - {}".format(server.get('sock', server['port'])), file=sys.stderr)
print(" - {}".format(server.get('sock') or server['port']), file=sys.stderr)
self.exit(1)
@ -1532,6 +1547,9 @@ class NotebookApp(JupyterApp):
_('Options --port and --sock are mutually exclusive. Aborting.'),
)
sys.exit(1)
else:
# Reset the default port if we're using a UNIX socket.
self.port = 0
if self.open_browser:
# If we're bound to a UNIX socket, we can't reliably connect from a browser.

View File

@ -4,6 +4,7 @@ import subprocess
import time
from ipython_genutils.testing.decorators import skip_win32
from notebook import DEFAULT_NOTEBOOK_PORT
from .launchnotebook import UNIXSocketNotebookTestBase
from ..utils import urlencode_unix_socket, urlencode_unix_socket_path
@ -12,7 +13,7 @@ from ..utils import urlencode_unix_socket, urlencode_unix_socket_path
@skip_win32
def test_shutdown_sock_server_integration():
sock = UNIXSocketNotebookTestBase.sock
url = urlencode_unix_socket(sock)
url = urlencode_unix_socket(sock).encode()
encoded_sock_path = urlencode_unix_socket_path(sock)
p = subprocess.Popen(
@ -21,7 +22,7 @@ def test_shutdown_sock_server_integration():
)
for line in iter(p.stderr.readline, b''):
if url.encode() in line:
if url in line:
complete = True
break
@ -37,3 +38,66 @@ def test_shutdown_sock_server_integration():
assert encoded_sock_path.encode() not in subprocess.check_output(['jupyter-notebook', 'list'])
p.wait()
def _ensure_stopped(check_msg='There are no running servers'):
try:
subprocess.check_output(
['jupyter-notebook', 'stop'],
stderr=subprocess.STDOUT
)
except subprocess.CalledProcessError as e:
assert check_msg in e.output.decode()
else:
raise AssertionError('expected all servers to be stopped')
@skip_win32
def test_stop_multi():
"""Tests lifecycle behavior for mixed-mode server types w/ default ports.
Suitable for local dev testing only due to reliance on default port binding.
"""
TEST_PORT = '9797'
MSG_TMPL = 'Shutting down server on {}...'
_ensure_stopped()
# Default port.
p1 = subprocess.Popen(
['jupyter-notebook', '--no-browser']
)
# Unix socket.
sock = UNIXSocketNotebookTestBase.sock
p2 = subprocess.Popen(
['jupyter-notebook', '--no-browser', '--sock=%s' % sock]
)
# Specified port
p3 = subprocess.Popen(
['jupyter-notebook', '--no-browser', '--port=%s' % TEST_PORT]
)
time.sleep(3)
assert MSG_TMPL.format(DEFAULT_NOTEBOOK_PORT) in subprocess.check_output(
['jupyter-notebook', 'stop']
).decode()
_ensure_stopped('There is currently no server running on 8888')
assert MSG_TMPL.format(sock) in subprocess.check_output(
['jupyter-notebook', 'stop', sock]
).decode()
assert MSG_TMPL.format(TEST_PORT) in subprocess.check_output(
['jupyter-notebook', 'stop', TEST_PORT]
).decode()
_ensure_stopped()
p1.wait()
p2.wait()
p3.wait()