diff --git a/src/debugAdapter/goDebug.ts b/src/debugAdapter/goDebug.ts index 0f3fd14b8..e9cc6f7e4 100644 --- a/src/debugAdapter/goDebug.ts +++ b/src/debugAdapter/goDebug.ts @@ -479,12 +479,11 @@ class Delve { }); } - callPromise(command: string, args: any[]): Thenable { + public callPromise(command: string, args: any[]): Thenable { return new Promise((resolve, reject) => { this.connection.then(conn => { - conn.call('RPCServer.' + command, args, (err, res) => { - if (err) return reject(err); - resolve(res); + conn.call(`RPCServer.${command}`, args, (err, res) => { + return err ? reject(err) : resolve(res); }); }, err => { reject(err); @@ -492,52 +491,51 @@ class Delve { }); } - close(): Thenable { + public close(): Thenable { log('HaltRequest'); - return new Promise(resolve => { - let timeoutToken: NodeJS.Timer; - if (this.debugProcess) { - timeoutToken = setTimeout(() => { - log('Killing debug process manually as we could not halt and detach delve in time'); - killTree(this.debugProcess.pid); - resolve(); - }, 1000); + return new Promise(async resolve => { + const timeoutToken: NodeJS.Timer = this.debugProcess && setTimeout(() => { + log('Killing debug process manually as we could not halt and detach delve in time'); + killTree(this.debugProcess.pid); + resolve(); + }, 1000); + + try { + await this.callPromise('Command', [{ name: 'halt' }]); + } + catch (err) { + const errMsg = err ? err.toString() : ''; + log(`Failed to halt - ${errMsg}`); + } + + if (timeoutToken) { + clearTimeout(timeoutToken); } - this.callPromise('Command', [{ name: 'halt' }]).then(() => { - if (timeoutToken) { - clearTimeout(timeoutToken); + log('HaltResponse'); + if (!this.debugProcess) { + log('RestartRequest'); + try { + await this.callPromise('Restart', this.isApiV1 ? [] : [{ position: '', resetArgs: false, newArgs: [] }]); } - log('HaltResponse'); - if (!this.debugProcess) { - log('RestartRequest'); - return this.callPromise('Restart', this.isApiV1 ? [] : [{ position: '', resetArgs: false, newArgs: [] }]) - .then(null, err => { - log('RestartResponse'); - logError(`Failed to restart - ${(err || '').toString()}`); - }) - .then(() => resolve()); - } else { - log('DetachRequest'); - return this.callPromise('Detach', [this.isApiV1 ? true : { Kill: true }]) - .then(null, err => { - log('DetachResponse'); - logError(`Killing debug process manually as we failed to detach - ${(err || '').toString()}`); - killTree(this.debugProcess.pid); - }) - .then(() => resolve()); + catch (err) { + log('RestartResponse'); + logError(`Failed to restart - ${(err || '').toString()}`); } - }, err => { - const errMsg = err ? err.toString() : ''; - log('Failed to halt - ' + errMsg.toString()); - if (errMsg.endsWith('has exited with status 0')) { - if (timeoutToken) { - clearTimeout(timeoutToken); - } - return resolve(); + return resolve(); + } else { + log('DetachRequest'); + try { + await this.callPromise('Detach', [this.isApiV1 ? true : { Kill: true }]); } - }); + catch (err) { + log('DetachResponse'); + logError(`Killing debug process manually as we failed to detach - ${(err.toString() || '')}`); + killTree(this.debugProcess.pid); + } + return resolve(); + } }); } }