From cda46df7557dcb8fdef0ecd844abd82ebec2de20 Mon Sep 17 00:00:00 2001 From: seem Date: Thu, 28 Nov 2024 15:02:42 +0200 Subject: [PATCH] simplify logic for switching runtimes for a notebook --- .../src/notebookController.ts | 12 +++++++++++- .../src/notebookSessionService.ts | 15 +-------------- 2 files changed, 12 insertions(+), 15 deletions(-) diff --git a/extensions/positron-notebook-controllers/src/notebookController.ts b/extensions/positron-notebook-controllers/src/notebookController.ts index aaac1770755..51b0f55555c 100644 --- a/extensions/positron-notebook-controllers/src/notebookController.ts +++ b/extensions/positron-notebook-controllers/src/notebookController.ts @@ -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); @@ -107,6 +107,16 @@ export class NotebookController implements vscode.Disposable { return this._runtimeMetadata.runtimeName; } + private async selectRuntimeSession(notebook: vscode.NotebookDocument): Promise { + // 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. * diff --git a/extensions/positron-notebook-controllers/src/notebookSessionService.ts b/extensions/positron-notebook-controllers/src/notebookSessionService.ts index c1135e9ad3f..d65894eb08e 100644 --- a/extensions/positron-notebook-controllers/src/notebookSessionService.ts +++ b/extensions/positron-notebook-controllers/src/notebookSessionService.ts @@ -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. */ @@ -146,7 +145,7 @@ export class NotebookSessionService implements vscode.Disposable { return startPromise; } - async doStartRuntimeSession(notebookUri: vscode.Uri, runtimeId: string, retry = true): Promise { + async doStartRuntimeSession(notebookUri: vscode.Uri, runtimeId: string): Promise { // If the session is still shutting down, wait for it to finish. const shuttingDownSessionPromise = this._shuttingDownSessionsByNotebookUri.get(notebookUri); if (shuttingDownSessionPromise) { @@ -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);