@@ -242,7 +242,10 @@ export class QLDebugSession extends LoggingDebugSession implements Disposable {
242242 /** A `BaseLogger` that sends output to the debug console. */
243243 private readonly logger : BaseLogger = {
244244 log : async ( message : string , _options : LogOptions ) : Promise < void > => {
245- this . sendEvent ( new OutputEvent ( message , "console" ) ) ;
245+ // Only send the output event if we're still connected to the query evaluation.
246+ if ( this . runningQuery !== undefined ) {
247+ this . sendEvent ( new OutputEvent ( message , "console" ) ) ;
248+ }
246249 } ,
247250 } ;
248251 private state : State = "uninitialized" ;
@@ -317,21 +320,35 @@ export class QLDebugSession extends LoggingDebugSession implements Disposable {
317320 _args : Protocol . DisconnectArguments ,
318321 _request ?: Protocol . Request ,
319322 ) : void {
320- this . terminateOrDisconnect ( response ) ;
323+ // The client is forcing a disconnect. We'll signal cancellation, but since this request means
324+ // that the debug session itself is about to go away, we'll stop processing events from the
325+ // evaluation to avoid sending them to the client that is no longer interested in them.
326+ this . terminateOrDisconnect ( response , true ) ;
321327 }
322328
323329 protected terminateRequest (
324330 response : Protocol . TerminateResponse ,
325331 _args : Protocol . TerminateArguments ,
326332 _request ?: Protocol . Request ,
327333 ) : void {
328- this . terminateOrDisconnect ( response ) ;
334+ // The client is requesting a graceful termination. This will signal the cancellation token of
335+ // any in-progress evaluation, but that evaluation will continue to report events (like
336+ // progress) until the cancellation takes effect.
337+ this . terminateOrDisconnect ( response , false ) ;
329338 }
330339
331- private terminateOrDisconnect ( response : Protocol . Response ) : void {
332- if ( this . runningQuery !== undefined ) {
340+ private terminateOrDisconnect (
341+ response : Protocol . Response ,
342+ force : boolean ,
343+ ) : void {
344+ const runningQuery = this . runningQuery ;
345+ if ( force ) {
346+ // Disconnect from the running query so that we stop processing its progress events.
347+ this . runningQuery = undefined ;
348+ }
349+ if ( runningQuery !== undefined ) {
333350 this . terminateOnComplete = true ;
334- this . runningQuery . cancel ( ) ;
351+ runningQuery . cancel ( ) ;
335352 } else if ( this . state === "stopped" ) {
336353 this . terminateAndExit ( ) ;
337354 }
@@ -537,7 +554,13 @@ export class QLDebugSession extends LoggingDebugSession implements Disposable {
537554 quickEvalContext ,
538555 this . queryStorageDir ,
539556 this . logger ,
540- ( event ) => this . sendEvent ( event ) ,
557+ ( event ) => {
558+ // If `this.runningQuery` is undefined, it means that we've already disconnected from this
559+ // evaluation, and do not want any further events.
560+ if ( this . runningQuery !== undefined ) {
561+ this . sendEvent ( event ) ;
562+ }
563+ } ,
541564 ) ;
542565 this . runningQuery = runningQuery ;
543566 this . state = "running" ;
0 commit comments