From fb50244a292932ab6114914af5784d57b9bd87e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miro=20Sp=C3=B6nemann?= Date: Wed, 22 Jan 2020 16:52:05 +0100 Subject: [PATCH] [debugger] Kill the gdb server on debug adapter process exit --- ...debug-frontend-application-contribution.ts | 2 +- .../browser/arduino-debug-session-manager.ts | 55 ------------------- .../src/browser/frontend-module.ts | 3 - .../node/debug-adapter/cmsis-debug-session.ts | 24 ++++---- 4 files changed, 11 insertions(+), 73 deletions(-) delete mode 100644 arduino-debugger-extension/src/browser/arduino-debug-session-manager.ts diff --git a/arduino-debugger-extension/src/browser/arduino-debug-frontend-application-contribution.ts b/arduino-debugger-extension/src/browser/arduino-debug-frontend-application-contribution.ts index 68737b00..490fa159 100644 --- a/arduino-debugger-extension/src/browser/arduino-debug-frontend-application-contribution.ts +++ b/arduino-debugger-extension/src/browser/arduino-debug-frontend-application-contribution.ts @@ -68,7 +68,7 @@ export class ArduinoDebugFrontendApplicationContribution extends DebugFrontendAp await this.editorManager.open(sketchFileURI); await this.manager.start(current); } else { - this.messageService.error('Please open a valid INO file.') + this.messageService.error('Please open a sketch file to start debugging.') } } else { await this.manager.start(current); diff --git a/arduino-debugger-extension/src/browser/arduino-debug-session-manager.ts b/arduino-debugger-extension/src/browser/arduino-debug-session-manager.ts deleted file mode 100644 index 027ee692..00000000 --- a/arduino-debugger-extension/src/browser/arduino-debug-session-manager.ts +++ /dev/null @@ -1,55 +0,0 @@ -import { DebugSessionManager } from "@theia/debug/lib/browser/debug-session-manager"; -import { DebugSessionOptions } from "@theia/debug/lib/browser/debug-session-options"; -import { DebugSession } from "@theia/debug/lib/browser/debug-session"; - -export class ArduinoDebugSessionManager extends DebugSessionManager { - - static readonly COOL_DOWN_TIME = 5000; - - protected arduinoSession?: Promise; - protected lastSessionStopTime?: DOMHighResTimeStamp; - - start(options: DebugSessionOptions) { - if (options.configuration.type === 'arduino') { - if (this.arduinoSession) { - this.messageService.info('A debug session is already running. You must stop the running session before starting a new one.') - return Promise.resolve(undefined); - } - const superStart = super.start.bind(this); - const promise = (async resolve => { - if (this.lastSessionStopTime) { - const now = performance.now(); - if (now - this.lastSessionStopTime < ArduinoDebugSessionManager.COOL_DOWN_TIME) { - const waitTime = ArduinoDebugSessionManager.COOL_DOWN_TIME - Math.max(now - this.lastSessionStopTime, 0); - if (waitTime > 2000) { - const userWaitTime = Math.round(waitTime / 100) / 10; - this.messageService.info(`The previous debug session is cooling down. Waiting ${userWaitTime} seconds before starting a new session...`) - } - await new Promise(resolve => setTimeout(resolve, waitTime)); - } - } - return superStart(options); - })(); - this.arduinoSession = promise; - promise.then(session => { - if (!session) - this.arduinoSession = undefined; - }); - return promise; - } - return super.start(options); - } - - destroy(sessionId?: string): void { - if (this.arduinoSession) { - this.arduinoSession.then(session => { - if (session && sessionId === session.id) { - this.arduinoSession = undefined; - this.lastSessionStopTime = performance.now(); - } - }) - } - super.destroy(sessionId); - } - -} diff --git a/arduino-debugger-extension/src/browser/frontend-module.ts b/arduino-debugger-extension/src/browser/frontend-module.ts index d02ff16e..f3223dba 100644 --- a/arduino-debugger-extension/src/browser/frontend-module.ts +++ b/arduino-debugger-extension/src/browser/frontend-module.ts @@ -1,17 +1,14 @@ import { ContainerModule } from 'inversify'; import { VariableContribution } from '@theia/variable-resolver/lib/browser'; import { ArduinoVariableResolver } from './arduino-variable-resolver'; -import { DebugSessionManager } from '@theia/debug/lib/browser/debug-session-manager'; import { DebugFrontendApplicationContribution } from '@theia/debug/lib/browser/debug-frontend-application-contribution'; import { DebugConfigurationManager } from '@theia/debug/lib/browser/debug-configuration-manager'; import { ArduinoDebugConfigurationManager } from './arduino-debug-configuration-manager'; import { ArduinoDebugFrontendApplicationContribution } from './arduino-debug-frontend-application-contribution'; -import { ArduinoDebugSessionManager } from './arduino-debug-session-manager'; export default new ContainerModule((bind, unbind, isBound, rebind) => { bind(ArduinoVariableResolver).toSelf().inSingletonScope(); bind(VariableContribution).toService(ArduinoVariableResolver); - rebind(DebugSessionManager).to(ArduinoDebugSessionManager).inSingletonScope(); rebind(DebugConfigurationManager).to(ArduinoDebugConfigurationManager).inSingletonScope(); rebind(DebugFrontendApplicationContribution).to(ArduinoDebugFrontendApplicationContribution); }); diff --git a/arduino-debugger-extension/src/node/debug-adapter/cmsis-debug-session.ts b/arduino-debugger-extension/src/node/debug-adapter/cmsis-debug-session.ts index 97e2bda1..21c5f317 100644 --- a/arduino-debugger-extension/src/node/debug-adapter/cmsis-debug-session.ts +++ b/arduino-debugger-extension/src/node/debug-adapter/cmsis-debug-session.ts @@ -258,6 +258,11 @@ export class CmsisDebugSession extends GDBDebugSession { this.progressEvent(0, 'Starting Debugger'); this.sendEvent(new OutputEvent(`Starting debugger: ${JSON.stringify(args)}`)); await this.gdbServer.spawn(args); + process.on('exit', () => { + if (this.gdbServer.isRunning) { + this.gdbServer.kill(); + } + }); await this.spawn(args); this.checkServerErrors(gdbServerErrors); @@ -425,20 +430,11 @@ export class CmsisDebugSession extends GDBDebugSession { } } - // Stop gdb client and server - we give GDB five seconds to exit orderly before we kill the GDB server - if (this.gdbServer.isRunning) { - const killPromise = new Promise(resolve => { - setTimeout(() => { - this.gdbServer.kill(); - resolve(); - }, 5000); - }); - try { - await this.gdb.sendGDBExit(); - } catch (e) { - // Need to catch here in case the connection has already been closed - } - await killPromise; + // Stop gdb client + try { + await this.gdb.sendGDBExit(); + } catch (e) { + // Need to catch here in case the connection has already been closed } }