11import { inject , injectable , named } from 'inversify' ;
22import * as path from 'path' ;
3- import { DebugConfiguration , l10n , Uri , WorkspaceFolder } from 'vscode' ;
3+ import { DebugConfiguration , l10n , Uri , WorkspaceFolder , DebugSession } from 'vscode' ;
44import { IApplicationShell , IDebugService } from '../../common/application/types' ;
55import { EXTENSION_ROOT_DIR } from '../../common/constants' ;
66import * as internalScripts from '../../common/process/internal/scripts' ;
@@ -9,7 +9,7 @@ import { DebuggerTypeName, PythonDebuggerTypeName } from '../../debugger/constan
99import { IDebugConfigurationResolver } from '../../debugger/extension/configuration/types' ;
1010import { DebugPurpose , LaunchRequestArguments } from '../../debugger/types' ;
1111import { IServiceContainer } from '../../ioc/types' ;
12- import { traceError } from '../../logging' ;
12+ import { traceError , traceVerbose } from '../../logging' ;
1313import { TestProvider } from '../types' ;
1414import { ITestDebugLauncher , LaunchOptions } from './types' ;
1515import { getConfigurationsForWorkspace } from '../../debugger/extension/configuration/launch.json/launchJsonReader' ;
@@ -34,12 +34,20 @@ export class DebugLauncher implements ITestDebugLauncher {
3434
3535 public async launchDebugger ( options : LaunchOptions , callback ?: ( ) => void ) : Promise < void > {
3636 const deferred = createDeferred < void > ( ) ;
37+ let hasCallbackBeenCalled = false ;
3738 if ( options . token && options . token . isCancellationRequested ) {
39+ hasCallbackBeenCalled = true ;
3840 return undefined ;
3941 deferred . resolve ( ) ;
4042 callback ?.( ) ;
4143 }
4244
45+ options . token ?. onCancellationRequested ( ( ) => {
46+ deferred . resolve ( ) ;
47+ callback ?.( ) ;
48+ hasCallbackBeenCalled = true ;
49+ } ) ;
50+
4351 const workspaceFolder = DebugLauncher . resolveWorkspaceFolder ( options . cwd ) ;
4452 const launchArgs = await this . getLaunchArgs (
4553 options ,
@@ -48,11 +56,23 @@ export class DebugLauncher implements ITestDebugLauncher {
4856 ) ;
4957 const debugManager = this . serviceContainer . get < IDebugService > ( IDebugService ) ;
5058
51- debugManager . onDidTerminateDebugSession ( ( ) => {
52- deferred . resolve ( ) ;
53- callback ?.( ) ;
59+ let activatedDebugSession : DebugSession | undefined ;
60+ debugManager . startDebugging ( workspaceFolder , launchArgs ) . then ( ( ) => {
61+ // Save the debug session after it is started so we can check if it is the one that was terminated.
62+ activatedDebugSession = debugManager . activeDebugSession ;
63+ } ) ;
64+ debugManager . onDidTerminateDebugSession ( ( session ) => {
65+ traceVerbose ( `Debug session terminated. sessionId: ${ session . id } ` ) ;
66+ // Only resolve no callback has been made and the session is the one that was started.
67+ if (
68+ ! hasCallbackBeenCalled &&
69+ activatedDebugSession !== undefined &&
70+ session . id === activatedDebugSession ?. id
71+ ) {
72+ deferred . resolve ( ) ;
73+ callback ?.( ) ;
74+ }
5475 } ) ;
55- debugManager . startDebugging ( workspaceFolder , launchArgs ) ;
5676 return deferred . promise ;
5777 }
5878
0 commit comments