Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

Commit

Permalink
Update logic per latest feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
Vlad Barosan committed Mar 9, 2019
1 parent 1eb5e96 commit 6836de6
Showing 1 changed file with 26 additions and 23 deletions.
49 changes: 26 additions & 23 deletions src/debugAdapter/goDebug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -512,12 +512,14 @@ class Delve {
log('HaltRequest');

const isLocalDebugging = this.debugProcess;

const forceCleanup = async () => {
killTree(this.debugProcess.pid);
await removeFile(this.localDebugeePath);
};
return new Promise(async resolve => {
const timeoutToken: NodeJS.Timer = isLocalDebugging && setTimeout(() => {
const timeoutToken: NodeJS.Timer = isLocalDebugging && setTimeout(async () => {
log('Killing debug process manually as we could not halt delve in time');
killTree(this.debugProcess.pid);
this.ensureDebugeeExecutableIsRemoved();
await forceCleanup();
resolve();
}, 1000);

Expand All @@ -531,20 +533,21 @@ class Delve {
}
clearTimeout(timeoutToken);

const targetHasExited = errMsg.endsWith('has exited with status 0');
const shouldDetach = !errMsg || targetHasExited;

if (shouldDetach(errMsg)) {
const targetHasExited: boolean = errMsg.endsWith('has exited with status 0');
const shouldDetach: boolean = !errMsg || targetHasExited;
let shouldForceClean: boolean = !shouldDetach;
if (shouldDetach) {
log('DetachRequest');
try {
await this.callPromise('Detach', [this.isApiV1 ? true : { Kill: true }]);
} catch (err) {
log('DetachResponse');
logError(`Failed to detach - ${(err.toString() || '')}`);
shouldForceClean = true;
}
}
if (isLocalDebugging) {
await this.ensureDebugeeExecutableIsRemoved();
if (isLocalDebugging && shouldForceClean) {
await forceCleanup();
}
return resolve();
});
Expand All @@ -556,19 +559,6 @@ class Delve {
? configOutput
: path.resolve(this.program, configOutput);
}

private async ensureDebugeeExecutableIsRemoved(): Promise<void> {
try {
const fileExists = await access(this.localDebugeePath)
.then(() => true)
.catch(() => false);
if (this.localDebugeePath && fileExists) {
await unlink(this.localDebugeePath);
}
} catch (e) {
logError(`Failed to potentially remove leftover debug file ${this.localDebugeePath} - ${e.toString() || ''}`);
}
}
}

class GoDebugSession extends LoggingDebugSession {
Expand Down Expand Up @@ -1431,4 +1421,17 @@ function killTree(processId: number): void {
}
}

async function removeFile(filePath: string): Promise<void> {
try {
const fileExists = await access(filePath)
.then(() => true)
.catch(() => false);
if (filePath && fileExists) {
await unlink(filePath);
}
} catch (e) {
logError(`Potentially failed remove file: ${filePath} - ${e.toString() || ''}`);
}
}

DebugSession.run(GoDebugSession);

0 comments on commit 6836de6

Please sign in to comment.