Skip to content

Commit 4da22ce

Browse files
authored
Restore detection of missing terminado package (#5465)
* Restore detection of missing terminado package * Properly handle terminals_enabled config * Disambiguate terminado availability from web settings * Further clarity on terminal availability * Rename terminals_in_use back to terminals_available
1 parent 66ad3f4 commit 4da22ce

File tree

2 files changed

+28
-16
lines changed

2 files changed

+28
-16
lines changed

notebook/notebookapp.py

+26-13
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,6 @@
9191
from .auth.login import LoginHandler
9292
from .auth.logout import LogoutHandler
9393
from .base.handlers import FileFindHandler
94-
from .terminal import TerminalManager
9594

9695
from traitlets.config import Config
9796
from traitlets.config.application import catch_config_error, boolean_flag
@@ -132,6 +131,13 @@
132131
except ImportError:
133132
async_kernel_mgmt_available = False
134133

134+
# Tolerate missing terminado package.
135+
try:
136+
from .terminal import TerminalManager
137+
terminado_available = True
138+
except ImportError:
139+
terminado_available = False
140+
135141
#-----------------------------------------------------------------------------
136142
# Module globals
137143
#-----------------------------------------------------------------------------
@@ -303,7 +309,7 @@ def init_settings(self, jupyter_app, kernel_manager, contents_manager,
303309
allow_password_change=jupyter_app.allow_password_change,
304310
server_root_dir=root_dir,
305311
jinja2_env=env,
306-
terminals_available=False, # Set later if terminals are available
312+
terminals_available=terminado_available and jupyter_app.terminals_enabled,
307313
)
308314

309315
# allow custom overrides for the tornado web app.
@@ -673,9 +679,12 @@ class NotebookApp(JupyterApp):
673679

674680
classes = [
675681
KernelManager, Session, MappingKernelManager, KernelSpecManager,
676-
ContentsManager, FileContentsManager, NotebookNotary, TerminalManager,
682+
ContentsManager, FileContentsManager, NotebookNotary,
677683
GatewayKernelManager, GatewayKernelSpecManager, GatewaySessionManager, GatewayClient,
678684
]
685+
if terminado_available: # Only necessary when terminado is available
686+
classes.append(TerminalManager)
687+
679688
flags = Dict(flags)
680689
aliases = Dict(aliases)
681690

@@ -1486,6 +1495,15 @@ def _update_server_extensions(self, change):
14861495
is not available.
14871496
"""))
14881497

1498+
# Since use of terminals is also a function of whether the terminado package is
1499+
# available, this variable holds the "final indication" of whether terminal functionality
1500+
# should be considered (particularly during shutdown/cleanup). It is enabled only
1501+
# once both the terminals "service" can be initialized and terminals_enabled is True.
1502+
# Note: this variable is slightly different from 'terminals_available' in the web settings
1503+
# in that this variable *could* remain false if terminado is available, yet the terminal
1504+
# service's initialization still fails. As a result, this variable holds the truth.
1505+
terminals_available = False
1506+
14891507
def parse_command_line(self, argv=None):
14901508
super(NotebookApp, self).parse_command_line(argv)
14911509

@@ -1777,7 +1795,7 @@ def init_terminals(self):
17771795
try:
17781796
from .terminal import initialize
17791797
initialize(nb_app=self)
1780-
self.web_app.settings['terminals_available'] = True
1798+
self.terminals_available = True
17811799
except ImportError as e:
17821800
self.log.warning(_("Terminals not available (error was %s)"), e)
17831801

@@ -1919,18 +1937,14 @@ def init_mime_overrides(self):
19191937
mimetypes.add_type('text/css', '.css')
19201938
mimetypes.add_type('application/javascript', '.js')
19211939

1922-
19231940
def shutdown_no_activity(self):
19241941
"""Shutdown server on timeout when there are no kernels or terminals."""
19251942
km = self.kernel_manager
19261943
if len(km) != 0:
19271944
return # Kernels still running
19281945

1929-
try:
1946+
if self.terminals_available:
19301947
term_mgr = self.web_app.settings['terminal_manager']
1931-
except KeyError:
1932-
pass # Terminals not enabled
1933-
else:
19341948
if term_mgr.terminals:
19351949
return # Terminals still running
19361950

@@ -2018,11 +2032,10 @@ def cleanup_terminals(self):
20182032
The terminals will shutdown themselves when this process no longer exists,
20192033
but explicit shutdown allows the TerminalManager to cleanup.
20202034
"""
2021-
try:
2022-
terminal_manager = self.web_app.settings['terminal_manager']
2023-
except KeyError:
2024-
return # Terminals not enabled
2035+
if not self.terminals_available:
2036+
return
20252037

2038+
terminal_manager = self.web_app.settings['terminal_manager']
20262039
n_terminals = len(terminal_manager.list())
20272040
terminal_msg = trans.ngettext('Shutting down %d terminal', 'Shutting down %d terminals', n_terminals)
20282041
self.log.info(terminal_msg % n_terminals)

notebook/terminal/__init__.py

+2-3
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
import terminado
44
from ..utils import check_version
55

6-
if not check_version(terminado.__version__, '0.8.1'):
7-
raise ImportError("terminado >= 0.8.1 required, found %s" % terminado.__version__)
6+
if not check_version(terminado.__version__, '0.8.3'):
7+
raise ImportError("terminado >= 0.8.3 required, found %s" % terminado.__version__)
88

99
from ipython_genutils.py3compat import which
10-
from tornado.log import app_log
1110
from notebook.utils import url_path_join as ujoin
1211
from .terminalmanager import TerminalManager
1312
from .handlers import TerminalHandler, TermSocket

0 commit comments

Comments
 (0)