@@ -221,10 +221,9 @@ export class NotebookSessionService implements vscode.Disposable {
221
221
// Construct a wrapping promise that resolves/rejects after the session maps have been updated.
222
222
const shutdownPromise = ( async ( ) => {
223
223
try {
224
- const session = await this . doShutdownRuntimeSession ( notebookUri ) ;
224
+ await this . doShutdownRuntimeSession ( notebookUri ) ;
225
225
this . _shuttingDownSessionsByNotebookUri . delete ( notebookUri ) ;
226
226
this . setNotebookSession ( notebookUri , undefined ) ;
227
- log . info ( `Session ${ session . metadata . sessionId } is shutdown` ) ;
228
227
} catch ( err ) {
229
228
this . _startingSessionsByNotebookUri . delete ( notebookUri ) ;
230
229
throw err ;
@@ -236,27 +235,12 @@ export class NotebookSessionService implements vscode.Disposable {
236
235
return shutdownPromise ;
237
236
}
238
237
239
- async doShutdownRuntimeSession ( notebookUri : vscode . Uri ) : Promise < positron . LanguageRuntimeSession > {
238
+ async doShutdownRuntimeSession ( notebookUri : vscode . Uri ) : Promise < void > {
240
239
// Get the notebook's session.
241
- let session = this . _notebookSessionsByNotebookUri . get ( notebookUri ) ;
242
-
240
+ const session = await this . getExistingOrPendingSession ( notebookUri ) ;
243
241
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 ;
260
244
}
261
245
262
246
// Start the shutdown sequence.
@@ -288,7 +272,29 @@ export class NotebookSessionService implements vscode.Disposable {
288
272
throw err ;
289
273
}
290
274
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 ;
292
298
}
293
299
294
300
/**
0 commit comments