Skip to content

Commit 31d4460

Browse files
authored
Fix shutting down a notebook when no session is running (#5560)
Addresses #4043. ### QA Notes I don't know if we'll be able to repro this one, so we might just have to review the code changes.
1 parent ac16c5d commit 31d4460

File tree

1 file changed

+28
-22
lines changed

1 file changed

+28
-22
lines changed

extensions/positron-notebook-controllers/src/notebookSessionService.ts

+28-22
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,9 @@ export class NotebookSessionService implements vscode.Disposable {
221221
// Construct a wrapping promise that resolves/rejects after the session maps have been updated.
222222
const shutdownPromise = (async () => {
223223
try {
224-
const session = await this.doShutdownRuntimeSession(notebookUri);
224+
await this.doShutdownRuntimeSession(notebookUri);
225225
this._shuttingDownSessionsByNotebookUri.delete(notebookUri);
226226
this.setNotebookSession(notebookUri, undefined);
227-
log.info(`Session ${session.metadata.sessionId} is shutdown`);
228227
} catch (err) {
229228
this._startingSessionsByNotebookUri.delete(notebookUri);
230229
throw err;
@@ -236,27 +235,12 @@ export class NotebookSessionService implements vscode.Disposable {
236235
return shutdownPromise;
237236
}
238237

239-
async doShutdownRuntimeSession(notebookUri: vscode.Uri): Promise<positron.LanguageRuntimeSession> {
238+
async doShutdownRuntimeSession(notebookUri: vscode.Uri): Promise<void> {
240239
// Get the notebook's session.
241-
let session = this._notebookSessionsByNotebookUri.get(notebookUri);
242-
240+
const session = await this.getExistingOrPendingSession(notebookUri);
243241
if (!session) {
244-
// If the notebook's session is still starting, wait for it to finish.
245-
const startingSessionPromise = this._startingSessionsByNotebookUri.get(notebookUri) ||
246-
this._restartingSessionsByNotebookUri.get(notebookUri);
247-
if (startingSessionPromise) {
248-
try {
249-
session = await startingSessionPromise;
250-
} catch (err) {
251-
log.error(`Waiting for notebook runtime to start before shutting down failed. Reason ${err}`);
252-
throw err;
253-
}
254-
}
255-
}
256-
257-
// Ensure that we have a session.
258-
if (!session) {
259-
throw new Error(`Tried to shutdown runtime for notebook without a running runtime: ${notebookUri.path}`);
242+
log.debug(`Tried to shutdown runtime for notebook without a running or starting runtime: ${notebookUri.path}`);
243+
return;
260244
}
261245

262246
// Start the shutdown sequence.
@@ -288,7 +272,29 @@ export class NotebookSessionService implements vscode.Disposable {
288272
throw err;
289273
}
290274

291-
return session;
275+
log.info(`Session ${session.metadata.sessionId} is shutdown`);
276+
}
277+
278+
private async getExistingOrPendingSession(notebookUri: vscode.Uri): Promise<positron.LanguageRuntimeSession | undefined> {
279+
// Check for an active session first.
280+
const activeSession = this._notebookSessionsByNotebookUri.get(notebookUri);
281+
if (activeSession) {
282+
return activeSession;
283+
}
284+
285+
// Check for a pending session.
286+
const pendingSessionPromise = this._startingSessionsByNotebookUri.get(notebookUri) ||
287+
this._restartingSessionsByNotebookUri.get(notebookUri);
288+
if (pendingSessionPromise) {
289+
try {
290+
return await pendingSessionPromise;
291+
} catch (err) {
292+
// No need to log; the error will be handled elsewhere.
293+
}
294+
}
295+
296+
// There is no existing or pending session for the notebook.
297+
return undefined;
292298
}
293299

294300
/**

0 commit comments

Comments
 (0)