Skip to content

Commit 1ffe307

Browse files
committed
Restore detection of missing terminado package
* 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 91bce70 commit 1ffe307

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

jupyter_server/serverapp.py

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,6 @@
8383
from .auth.login import LoginHandler
8484
from .auth.logout import LogoutHandler
8585
from .base.handlers import FileFindHandler
86-
from .terminal import TerminalManager
8786

8887
from traitlets.config import Config
8988
from traitlets.config.application import catch_config_error, boolean_flag
@@ -117,6 +116,13 @@
117116
from jupyter_server.extension.config import ExtensionConfigManager
118117
from jupyter_server.traittypes import TypeFromClasses
119118

119+
# Tolerate missing terminado package.
120+
try:
121+
from .terminal import TerminalManager
122+
terminado_available = True
123+
except ImportError:
124+
terminado_available = False
125+
120126
#-----------------------------------------------------------------------------
121127
# Module globals
122128
#-----------------------------------------------------------------------------
@@ -285,7 +291,7 @@ def init_settings(self, jupyter_app, kernel_manager, contents_manager,
285291
allow_password_change=jupyter_app.allow_password_change,
286292
server_root_dir=root_dir,
287293
jinja2_env=env,
288-
terminals_available=False, # Set later if terminals are available
294+
terminals_available=terminado_available and jupyter_app.terminals_enabled,
289295
serverapp=jupyter_app
290296
)
291297

@@ -588,8 +594,10 @@ class ServerApp(JupyterApp):
588594
classes = [
589595
KernelManager, Session, MappingKernelManager, KernelSpecManager, AsyncMappingKernelManager,
590596
ContentsManager, FileContentsManager, AsyncContentsManager, AsyncFileContentsManager, NotebookNotary,
591-
TerminalManager, GatewayKernelSpecManager, GatewaySessionManager, GatewayClient
597+
GatewayKernelManager, GatewayKernelSpecManager, GatewaySessionManager, GatewayClient
592598
]
599+
if terminado_available: # Only necessary when terminado is available
600+
classes.append(TerminalManager)
593601

594602
subcommands = dict(
595603
list=(JupyterServerListApp, JupyterServerListApp.description.splitlines()[0]),
@@ -1330,6 +1338,15 @@ def _update_server_extensions(self, change):
13301338
is not available.
13311339
"""))
13321340

1341+
# Since use of terminals is also a function of whether the terminado package is
1342+
# available, this variable holds the "final indication" of whether terminal functionality
1343+
# should be considered (particularly during shutdown/cleanup). It is enabled only
1344+
# once both the terminals "service" can be initialized and terminals_enabled is True.
1345+
# Note: this variable is slightly different from 'terminals_available' in the web settings
1346+
# in that this variable *could* remain false if terminado is available, yet the terminal
1347+
# service's initialization still fails. As a result, this variable holds the truth.
1348+
terminals_available = False
1349+
13331350
authenticate_prometheus = Bool(
13341351
True,
13351352
help=""""
@@ -1548,7 +1565,7 @@ def init_terminals(self):
15481565
try:
15491566
from .terminal import initialize
15501567
initialize(server_app=self)
1551-
self.web_app.settings['terminals_available'] = True
1568+
self.terminals_available = True
15521569
except ImportError as e:
15531570
self.log.warning(_i18n("Terminals not available (error was %s)"), e)
15541571

@@ -1694,11 +1711,8 @@ def shutdown_no_activity(self):
16941711
if len(km) != 0:
16951712
return # Kernels still running
16961713

1697-
try:
1714+
if self.terminals_available:
16981715
term_mgr = self.web_app.settings['terminal_manager']
1699-
except KeyError:
1700-
pass # Terminals not enabled
1701-
else:
17021716
if term_mgr.terminals:
17031717
return # Terminals still running
17041718

@@ -1853,11 +1867,10 @@ def cleanup_terminals(self):
18531867
The terminals will shutdown themselves when this process no longer exists,
18541868
but explicit shutdown allows the TerminalManager to cleanup.
18551869
"""
1856-
try:
1857-
terminal_manager = self.web_app.settings['terminal_manager']
1858-
except KeyError:
1859-
return # Terminals not enabled
1870+
if not self.terminals_available:
1871+
return
18601872

1873+
terminal_manager = self.web_app.settings['terminal_manager']
18611874
n_terminals = len(terminal_manager.list())
18621875
terminal_msg = trans.ngettext('Shutting down %d terminal', 'Shutting down %d terminals', n_terminals)
18631876
self.log.info(terminal_msg % n_terminals)

0 commit comments

Comments
 (0)