Pārlūkot izejas kodu

Merge pull request #6275 from fcollonval/kernel-shutdown

Kernel shutdown when closing notebook tab
Steven Silvester 6 gadi atpakaļ
vecāks
revīzija
25d8709e74

+ 10 - 0
packages/apputils/src/clientsession.tsx

@@ -199,6 +199,11 @@ export namespace IClientSession {
      */
     readonly canStart?: boolean;
 
+    /**
+     * Whether a kernel needs to be close with the associated session
+     */
+    readonly shutdownOnClose?: boolean;
+
     /**
      * Whether to auto-start the default kernel if no matching kernel is found.
      */
@@ -363,6 +368,11 @@ export class ClientSession implements IClientSession {
     }
     this._isDisposed = true;
     if (this._session) {
+      if (this.kernelPreference.shutdownOnClose) {
+        this._session.shutdown().catch(reason => {
+          console.error(`Kernel not shutdown ${reason}`);
+        });
+      }
       this._session = null;
     }
     if (this._dialog) {

+ 12 - 0
packages/docregistry/src/default.ts

@@ -287,6 +287,7 @@ export abstract class ABCWidgetFactory<
     this._modelName = options.modelName || 'text';
     this._preferKernel = !!options.preferKernel;
     this._canStartKernel = !!options.canStartKernel;
+    this._shutdownOnClose = !!options.shutdownOnClose;
     this._toolbarFactory = options.toolbarFactory;
   }
 
@@ -368,6 +369,16 @@ export abstract class ABCWidgetFactory<
     return this._canStartKernel;
   }
 
+  /**
+   * Whether the kernel should be shutdown when the widget is closed.
+   */
+  get shutdownOnClose(): boolean {
+    return this._shutdownOnClose;
+  }
+  set shutdownOnClose(value: boolean) {
+    this._shutdownOnClose = value;
+  }
+
   /**
    * Create a new widget given a document model and a context.
    *
@@ -414,6 +425,7 @@ export abstract class ABCWidgetFactory<
   private _name: string;
   private _readOnly: boolean;
   private _canStartKernel: boolean;
+  private _shutdownOnClose: boolean;
   private _preferKernel: boolean;
   private _modelName: string;
   private _fileTypes: string[];

+ 7 - 1
packages/docregistry/src/registry.ts

@@ -522,7 +522,8 @@ export class DocumentRegistry implements IDisposable {
       name,
       language,
       shouldStart: widgetFactory.preferKernel,
-      canStart: widgetFactory.canStartKernel
+      canStart: widgetFactory.canStartKernel,
+      shutdownOnClose: widgetFactory.shutdownOnClose
     };
   }
 
@@ -931,6 +932,11 @@ export namespace DocumentRegistry {
      */
     readonly canStartKernel?: boolean;
 
+    /**
+     * Whether the kernel should be shutdown when the widget is closed.
+     */
+    readonly shutdownOnClose?: boolean;
+
     /**
      * A function producing toolbar widgets, overriding the default toolbar widgets.
      */

+ 6 - 0
packages/notebook-extension/schema/tracker.json

@@ -284,6 +284,12 @@
         "codeFolding": false
       }
     },
+    "kernelShutdown": {
+      "title": "Shut down kernel",
+      "description": "Whether to shut down or not the kernel when closing a notebook.",
+      "type": "boolean",
+      "default": false
+    },
     "markdownCellConfig": {
       "title": "Markdown Cell Configuration",
       "description": "The configuration for all markdown cells.",

+ 8 - 0
packages/notebook-extension/src/index.ts

@@ -620,6 +620,8 @@ function activateNotebookHandler(
     factory.notebookConfig = notebookConfig = {
       scrollPastEnd: settings.get('scrollPastEnd').composite as boolean
     };
+    factory.shutdownOnClose = settings.get('kernelShutdown')
+      .composite as boolean;
   }
 
   /**
@@ -629,6 +631,12 @@ function activateNotebookHandler(
     tracker.forEach(widget => {
       widget.content.editorConfig = editorConfig;
       widget.content.notebookConfig = notebookConfig;
+      // Update kernel shutdown behavior
+      const kernelPreference = widget.context.session.kernelPreference;
+      widget.context.session.kernelPreference = {
+        ...kernelPreference,
+        shutdownOnClose: factory.shutdownOnClose
+      };
     });
   }