From 6ffba37822425870dcc5316bafe7f7f9dc06d980 Mon Sep 17 00:00:00 2001 From: tsvetie Date: Fri, 8 Mar 2019 17:23:20 +0200 Subject: [PATCH] Handle stuck cloud build requests as well PUT and POST requests may hang as well. --- lib/common/http-client.ts | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/lib/common/http-client.ts b/lib/common/http-client.ts index 281ef86082..e9ce382367 100644 --- a/lib/common/http-client.ts +++ b/lib/common/http-client.ts @@ -1,5 +1,6 @@ import * as url from "url"; import { EOL } from "os"; +import { TLSSocket } from "tls"; import * as helpers from "./helpers"; import * as zlib from "zlib"; import * as util from "util"; @@ -105,8 +106,6 @@ export class HttpClient implements Server.IHttpClient { const result = new Promise((resolve, reject) => { let timerId: number; - let stuckRequestTimerId: number; - let hasResponse = false; const cleanupRequestData: ICleanupRequestData = Object.create({ timers: [] }); this.cleanupData.push(cleanupRequestData); @@ -133,17 +132,6 @@ export class HttpClient implements Server.IHttpClient { const requestObj = request(options); cleanupRequestData.req = requestObj; - if (options.method !== "PUT" && options.method !== "POST") { - stuckRequestTimerId = setTimeout(() => { - clearTimeout(stuckRequestTimerId); - stuckRequestTimerId = null; - if (!hasResponse) { - this.setResponseResult(promiseActions, cleanupRequestData, { err: new Error(HttpClient.STUCK_REQUEST_ERROR_MESSAGE) }); - } - }, options.timeout || HttpClient.STUCK_REQUEST_TIMEOUT); - cleanupRequestData.timers.push(stuckRequestTimerId); - } - requestObj .on("error", (err: IHttpRequestError) => { this.$logger.trace("An error occurred while sending the request:", err); @@ -158,9 +146,22 @@ export class HttpClient implements Server.IHttpClient { err.message = errorMessage || err.message; this.setResponseResult(promiseActions, cleanupRequestData, { err }); }) + .on("socket", (s: TLSSocket) => { + let stuckRequestTimerId: number; + + stuckRequestTimerId = setTimeout(() => { + this.setResponseResult(promiseActions, cleanupRequestData, { err: new Error(HttpClient.STUCK_REQUEST_ERROR_MESSAGE) }); + }, options.timeout || HttpClient.STUCK_REQUEST_TIMEOUT); + + cleanupRequestData.timers.push(stuckRequestTimerId); + + s.once("secureConnect", () => { + clearTimeout(stuckRequestTimerId); + stuckRequestTimerId = null; + }); + }) .on("response", (response: Server.IRequestResponseData) => { cleanupRequestData.res = response; - hasResponse = true; let lastChunkTimestamp = Date.now(); cleanupRequestData.stuckResponseIntervalId = setInterval(() => { if (Date.now() - lastChunkTimestamp > HttpClient.STUCK_RESPONSE_CHECK_INTERVAL) {