Skip to content

Commit

Permalink
simplify logic for switching runtimes for a notebook
Browse files Browse the repository at this point in the history
  • Loading branch information
seeM committed Nov 28, 2024
1 parent 9598da7 commit cda46df
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ export class NotebookController implements vscode.Disposable {

await Promise.all([
updateNotebookLanguage(e.notebook, _runtimeMetadata.languageId),
this.startRuntimeSession(e.notebook),
this.selectRuntimeSession(e.notebook),
]);
} else {
await this._notebookSessionService.shutdownRuntimeSession(e.notebook.uri);
Expand All @@ -107,6 +107,16 @@ export class NotebookController implements vscode.Disposable {
return this._runtimeMetadata.runtimeName;
}

private async selectRuntimeSession(notebook: vscode.NotebookDocument): Promise<void> {
// If there's an existing session from another runtime, shut it down.
if (this._notebookSessionService.hasStartingOrRunningNotebookSession(notebook.uri)) {
await this._notebookSessionService.shutdownRuntimeSession(notebook.uri);
}

// Start the new session.
await this.startRuntimeSession(notebook);
}

/**
* Start a runtime session for a notebook.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import * as vscode from 'vscode';
import * as path from 'path';
import { log } from './extension';
import { ResourceMap } from './map';
import { delay } from './utils';

export interface INotebookSessionDidChangeEvent {
/** The URI of the notebook corresponding to the session. */
Expand Down Expand Up @@ -146,7 +145,7 @@ export class NotebookSessionService implements vscode.Disposable {
return startPromise;
}

async doStartRuntimeSession(notebookUri: vscode.Uri, runtimeId: string, retry = true): Promise<positron.LanguageRuntimeSession> {
async doStartRuntimeSession(notebookUri: vscode.Uri, runtimeId: string): Promise<positron.LanguageRuntimeSession> {
// If the session is still shutting down, wait for it to finish.
const shuttingDownSessionPromise = this._shuttingDownSessionsByNotebookUri.get(notebookUri);
if (shuttingDownSessionPromise) {
Expand All @@ -158,18 +157,6 @@ export class NotebookSessionService implements vscode.Disposable {
}
}

// Ensure that we don't start a runtime for a notebook that already has one.
if (this._notebookSessionsByNotebookUri.has(notebookUri)) {
if (!retry) {
throw new Error(`Tried to start a runtime for a notebook that already has one: ${notebookUri.path}`);
}
// Notebook controllers may try to start a runtime immediately before shutting down the
// previous, due to out of order onDidChangeSelectedNotebooks events. Wait and retry once.
log.debug('Tried to start a runtime for a notebook that already has one. Waiting and retrying once...');
await delay(50);
return this.doStartRuntimeSession(notebookUri, runtimeId, false);
}

// If there's already a session for this runtime e.g. one restored after a window reload, use it.
try {
const session = await positron.runtime.getNotebookSession(notebookUri);
Expand Down

0 comments on commit cda46df

Please sign in to comment.