Skip to content

Prevent load of disabled (or disabling) extension modules #1178

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

Closed
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
8 changes: 7 additions & 1 deletion jupyter_server/extension/manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,10 @@ def __init__(self, *args, **kwargs):
"""Initialize an extension package."""
# Store extension points that have been linked.
self._linked_points = {}
self._extension_points = {}
self._module = None
self._metadata = []
self._action = kwargs.pop("action", "")
super().__init__(*args, **kwargs)

_linked_points: dict = {}
Expand All @@ -172,7 +176,9 @@ def _validate_name(self, proposed):
name = proposed["value"]
self._extension_points = {}
try:
self._module, self._metadata = get_metadata(name, self.log)
# Perform load if we're enabling, or it's already enabled and not disabling the module
if (self.enabled and self._action != "disabling") or self._action == "enabling":
self._module, self._metadata = get_metadata(name, self.log)
except ImportError as e:
msg = (
f"The module '{name}' could not be found ({e}). Are you "
Expand Down
48 changes: 27 additions & 21 deletions jupyter_server/extension/serverextension.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,14 +246,17 @@ def toggle_server_extension(self, import_name):
)
try:
self.log.info(f"{self._toggle_pre_message.capitalize()}: {import_name}")
self.log.info(f"- Writing config: {config_dir}")
# Validate the server extension.
self.log.info(f" - Validating {import_name}...")
# Interface with the Extension Package and validate.
extpkg = ExtensionPackage(name=import_name)
extpkg.validate()
version = extpkg.version
self.log.info(f" {import_name} {version} {GREEN_OK}")
self.log.info(f" - Writing config: {config_dir}")
# If enabling, validate the server extension.
if self._toggle_pre_message == "enabling":
self.log.info(f" - Validating {import_name}...")
# Interface with the Extension Package and validate.
extpkg = ExtensionPackage(name=import_name, action=self._toggle_pre_message)
extpkg.validate()
version = extpkg.version
self.log.info(f" {import_name} {version} {GREEN_OK}")
else:
self.log.info(f" - Validation of {import_name} skipped due to acton to disable.")

# Toggle extension config.
config = extension_manager.config_manager
Expand Down Expand Up @@ -335,19 +338,22 @@ def list_server_extensions(self):
for name, enabled in jpserver_extensions.items():
# Attempt to get extension metadata
self.log.info(f" {name} {GREEN_ENABLED if enabled else RED_DISABLED}")
try:
self.log.info(f" - Validating {name}...")
extension = ExtensionPackage(name=name, enabled=enabled)
if not extension.validate():
msg = "validation failed"
raise ValueError(msg)
version = extension.version
self.log.info(f" {name} {version} {GREEN_OK}")
except Exception as err:
exc_info = False
if int(self.log_level) <= logging.DEBUG:
exc_info = True
self.log.warning(f" {RED_X} {err}", exc_info=exc_info)
if enabled:
try:
self.log.info(f" - Validating {name}...")
extension = ExtensionPackage(name=name, enabled=enabled)
if not extension.validate():
msg = "validation failed"
raise ValueError(msg)
version = extension.version
self.log.info(f" {name} {version} {GREEN_OK}")
except Exception as err:
exc_info = False
if int(self.log_level) <= logging.DEBUG:
exc_info = True
self.log.warning(f" {RED_X} {err}", exc_info=exc_info)
else:
self.log.info(f" - Validation of {name} skipped.")
# Add a blank line between paths.
self.log.info("")

Expand Down
2 changes: 1 addition & 1 deletion jupyter_server/extension/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def get_metadata(package_name, logger=None):
# each module took to import. This makes it much easier for users to report
# slow loading modules upstream, as slow loading modules will block server startup
if logger:
logger.info(f"Package {package_name} took {duration:.4f}s to import")
logger.info(f" - Package {package_name} took {duration:.4f}s to import")

try:
return module, module._jupyter_server_extension_points()
Expand Down