From cbaa725697796e712f31a82653774ebe133f96e9 Mon Sep 17 00:00:00 2001 From: davidreis97 Date: Wed, 18 Mar 2020 21:38:50 +0000 Subject: [PATCH] Prevent crash if docker stats api call fails Ensure that performance packets sent to the extension are always valid --- dockerfile-utils/src/dynamicAnalysis.ts | 78 +++++++++++++++---------- 1 file changed, 47 insertions(+), 31 deletions(-) diff --git a/dockerfile-utils/src/dynamicAnalysis.ts b/dockerfile-utils/src/dynamicAnalysis.ts index 5a4cf1c..5a112b5 100644 --- a/dockerfile-utils/src/dynamicAnalysis.ts +++ b/dockerfile-utils/src/dynamicAnalysis.ts @@ -216,6 +216,7 @@ export class DynamicAnalysis { this.addDiagnostic(DiagnosticSeverity.Error, this.dockerfile.getENTRYPOINTs()[0].getRange(), "Container Exited with code (" + data.StatusCode + ") - See Logs"); this.publishDiagnostics(); } + this.destroy(); }); }); }); @@ -677,30 +678,38 @@ export class DynamicAnalysis { //Based on https://github.com/moby/moby/blob/eb131c5383db8cac633919f82abad86c99bffbe5/cli/command/container/stats_helpers.go#L175-L188 private calculateCPUPercent(stats) { - let cpuPercent = 0; - let cpuDelta = stats.cpu_stats.cpu_usage.total_usage - stats.precpu_stats.cpu_usage.total_usage; - let systemDelta = stats.cpu_stats.system_cpu_usage - stats.precpu_stats.system_cpu_usage; - let cpuCount = stats.cpu_stats.cpu_usage.percpu_usage.length; + try{ + let cpuPercent = 0; + let cpuDelta = stats.cpu_stats.cpu_usage.total_usage - stats.precpu_stats.cpu_usage.total_usage; + let systemDelta = stats.cpu_stats.system_cpu_usage - stats.precpu_stats.system_cpu_usage; + let cpuCount = stats.cpu_stats.cpu_usage.percpu_usage.length; - if (systemDelta && cpuDelta) { - cpuPercent = (cpuDelta / systemDelta) * cpuCount * 100; - } + if (systemDelta && cpuDelta) { + cpuPercent = (cpuDelta / systemDelta) * cpuCount * 100; + } - return cpuPercent; + return !isNaN(cpuPercent) ? cpuPercent : 0; + }catch(e){ + return 0; + } } private calculateNetworks(stats) { let rawNetworks = stats.networks; let finalNetworks = {}; - for (let key of Object.keys(rawNetworks)) { - finalNetworks[key] = { - input: rawNetworks[key].rx_bytes, - output: rawNetworks[key].tx_bytes - }; - } - - return finalNetworks; + try{ + for (let key of Object.keys(rawNetworks)) { + finalNetworks[key] = { + input: rawNetworks[key].rx_bytes, + output: rawNetworks[key].tx_bytes + }; + } + + return finalNetworks; + }catch(e){ + return {}; + } } //Based on https://github.com/moby/moby/blob/eb131c5383db8cac633919f82abad86c99bffbe5/cli/command/container/stats_helpers.go#L106-L125 @@ -708,22 +717,29 @@ export class DynamicAnalysis { let readBytes = 0; let writeBytes = 0; - if (process.platform === "win32") { - readBytes = stats.storage_stats.read_size_bytes || 0; - writeBytes = stats.storage_stats.write_size_bytes || 0; - } else { - for (let entry of stats.blkio_stats.io_service_bytes_recursive) { - if (entry.op == "read") { - readBytes += entry.value; - } else if (entry.op == "write") { - writeBytes += entry.value; + try{ + if (process.platform === "win32") { + readBytes = stats.storage_stats.read_size_bytes || 0; + writeBytes = stats.storage_stats.write_size_bytes || 0; + } else { + for (let entry of stats.blkio_stats.io_service_bytes_recursive) { + if (entry.op == "read") { + readBytes += entry.value; + } else if (entry.op == "write") { + writeBytes += entry.value; + } } } - } - return { - readBytes: readBytes, - writeBytes: writeBytes + return { + readBytes: !isNaN(readBytes) ? readBytes : 0, + writeBytes: !isNaN(writeBytes) ? writeBytes : 0 + } + }catch(e){ + return { + readBytes: 0, + writeBytes: 0 + } } } @@ -754,8 +770,8 @@ export class DynamicAnalysis { percentage: this.calculateCPUPercent(parsedData) }, memory: { - usage: parsedData.memory_stats.usage, - limit: parsedData.memory_stats.limit + usage: parsedData.memory_stats.usage || 0, + limit: parsedData.memory_stats.limit || 0 }, networks: this.calculateNetworks(parsedData), storage: this.calculateStorage(parsedData)