From 1cbc6a215e34ed7cc60230db2609cbc461481205 Mon Sep 17 00:00:00 2001 From: davidreis97 Date: Thu, 19 Mar 2020 13:17:57 +0000 Subject: [PATCH] Start waiting for container exit on container start instead of end of attach Workaround to the issue loosely described in https://github.com/apocas/dockerode/issues/534 --- dockerfile-utils/src/dynamicAnalysis.ts | 39 ++++++++++++------------- 1 file changed, 19 insertions(+), 20 deletions(-) diff --git a/dockerfile-utils/src/dynamicAnalysis.ts b/dockerfile-utils/src/dynamicAnalysis.ts index 5a112b5..6718aa3 100644 --- a/dockerfile-utils/src/dynamicAnalysis.ts +++ b/dockerfile-utils/src/dynamicAnalysis.ts @@ -185,9 +185,26 @@ export class DynamicAnalysis { this.getPerformance(); this.getOS(); this.checkEnvVar(); + + container.wait((err, data) => { + this.sendProgress(true); + if (this.isDestroyed) { + return; + } + if (err) { + this.debugLog("ERROR GETTING CONTAINER EXIT CODE", err); + return; + } + this.log("CONTAINER CLOSED WITH CODE", data.StatusCode); + if (data.StatusCode != 0) { + this.addDiagnostic(DiagnosticSeverity.Error, this.dockerfile.getENTRYPOINTs()[0].getRange(), "Container Exited with code (" + data.StatusCode + ") - See Logs"); + this.publishDiagnostics(); + } + this.destroy(); + }); }); - container.attach({ stream: true, stdout: true, stderr: true }, (err, stream: Stream) => { + container.attach({stream: true, stdout: true, stderr: true}, (err, stream: Stream) => { if (this.isDestroyed) { this.sendProgress(true); return; @@ -199,25 +216,7 @@ export class DynamicAnalysis { this.sendProgress(true); } stream.on('data', (data) => { - this.log("CONTAINER STDOUT", data); - }); - stream.on('end', (_) => { - container.wait((err, data) => { - this.sendProgress(true); - if (this.isDestroyed) { - return; - } - if (err) { - this.debugLog("ERROR GETTING CONTAINER EXIT CODE", err); - return; - } - this.log("CONTAINER CLOSED WITH CODE", data.StatusCode); - if (data.StatusCode != 0) { - this.addDiagnostic(DiagnosticSeverity.Error, this.dockerfile.getENTRYPOINTs()[0].getRange(), "Container Exited with code (" + data.StatusCode + ") - See Logs"); - this.publishDiagnostics(); - } - this.destroy(); - }); + this.log(data); }); }); });