Skip to content
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

Kernel shutdown when closing notebook tab #6275

Merged
merged 4 commits into from
Apr 29, 2019
Merged
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
10 changes: 10 additions & 0 deletions packages/apputils/src/clientsession.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down Expand Up @@ -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) {
Expand Down
12 changes: 12 additions & 0 deletions packages/docregistry/src/default.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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[];
Expand Down
8 changes: 7 additions & 1 deletion packages/docregistry/src/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,8 @@ export class DocumentRegistry implements IDisposable {
name,
language,
shouldStart: widgetFactory.preferKernel,
canStart: widgetFactory.canStartKernel
canStart: widgetFactory.canStartKernel,
shutdownOnClose: widgetFactory.shutdownOnClose
};
}

Expand Down Expand Up @@ -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.
*/
Expand Down
6 changes: 6 additions & 0 deletions packages/notebook-extension/schema/tracker.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.",
Expand Down
8 changes: 8 additions & 0 deletions packages/notebook-extension/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -620,6 +620,8 @@ function activateNotebookHandler(
factory.notebookConfig = notebookConfig = {
scrollPastEnd: settings.get('scrollPastEnd').composite as boolean
};
factory.shutdownOnClose = settings.get('kernelShutdown')
.composite as boolean;
}

/**
Expand All @@ -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
};
});
}

Expand Down