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

[DRAFT] Kernel post-initialization stability improvements #16250

Closed
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
11 changes: 10 additions & 1 deletion src/kernels/kernel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ abstract class BaseKernel implements IBaseKernel {
public get restarting() {
return this._restartPromise || Promise.resolve();
}
private _postInitializingDeferred = createDeferred<void>();
public get postInitializing() {
return this._postInitializingDeferred.promise;
}
constructor(
public readonly id: string,
public readonly uri: Uri,
Expand Down Expand Up @@ -258,9 +262,14 @@ abstract class BaseKernel implements IBaseKernel {
// If we started and the UI is no longer disabled (ie., a user executed a cell)
// then we can signal that the kernel was created and can be used by third-party extensions.
// We also only want to fire off a single event here.
if (!options?.disableUI && !this._postInitializedOnStart) {
if (!this.startupUI.disableUI && !this._postInitializedOnStart) {
this._onPostInitialized.fire();
this._postInitializedOnStart = true;
// Inject a small delay to allow external event handlers a brief window
// to queue code executions.
setTimeout(() => {
this._postInitializingDeferred.resolve();
}, 30);
}
return result;
});
Expand Down
5 changes: 5 additions & 0 deletions src/kernels/kernelExecution.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,13 @@ export class NotebookKernelExecution implements INotebookKernelExecution {
const sessionPromise = this.kernel.restarting.then(() => this.kernel.start(new DisplayOptions(false)));

traceCellMessage(cell, `NotebookKernelExecution.executeCell (3), ${getDisplayPath(cell.notebook.uri)}`);

// Wait for the kernel to complete post initialization before queueing the cell in case
// we need to allow extensions to run code before the initial user-triggered execution
await this.kernel.postInitializing;
const executionQueue = this.getOrCreateCellExecutionQueue(cell.notebook, sessionPromise);
executionQueue.queueCell(cell, codeOverride);

let success = true;
try {
traceCellMessage(cell, `NotebookKernelExecution.executeCell (4), ${getDisplayPath(cell.notebook.uri)}`);
Expand Down
1 change: 1 addition & 0 deletions src/kernels/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ export interface IBaseKernel extends IAsyncDisposable {
readonly onRestarted: Event<void>;
readonly onPostInitialized: Event<void>;
readonly restarting: Promise<void>;
readonly postInitializing: Promise<void>;
readonly status: KernelMessage.Status;
readonly disposed: boolean;
readonly disposing: boolean;
Expand Down
Loading