From 735aec90e883b9c62b8696a684b1519863a64acc Mon Sep 17 00:00:00 2001 From: Scott Nonnenberg Date: Fri, 20 Oct 2017 17:02:44 -0700 Subject: [PATCH] Add 5s timeout for all requests but attachment up/down, avatar (#1584) * Remove hang workaround in api.js since we have global workaround * Add 5s timeout for all requests exept attachment up/down, avatar --- js/libtextsecure.js | 32 ++++++++++++-------------------- libtextsecure/api.js | 32 ++++++++++++-------------------- 2 files changed, 24 insertions(+), 40 deletions(-) diff --git a/js/libtextsecure.js b/js/libtextsecure.js index cd261ba3cebd..ea08a931be34 100644 --- a/js/libtextsecure.js +++ b/js/libtextsecure.js @@ -37461,22 +37461,8 @@ var TextSecureServer = (function() { return true; } - // On Linux/Electron multiple quick web requests can result in the Node.js event - // loop getting wedged. Bug: https://github.com/electron/electron/issues/10570 - // This forces the event loop to move. - function scheduleHangWorkaround() { - setTimeout(function() { - setImmediate(function() { - // noop - }); - }, 1000); - } - function createSocket(url) { var requestOptions = { ca: window.config.certificateAuthorities }; - - scheduleHangWorkaround(); - return new nodeWebSocket(url, null, null, null, requestOptions); } @@ -37488,11 +37474,14 @@ var TextSecureServer = (function() { url = options.host + '/' + options.path; } console.log(options.type, url); + var timeout = typeof options.timeout !== 'undefined' ? options.timeout : 5000; + var fetchOptions = { method: options.type, body: options.data || null, headers: { 'X-Signal-Agent': 'OWD' }, - agent: new httpsAgent({ca: options.certificateAuthorities}) + agent: new httpsAgent({ca: options.certificateAuthorities}), + timeout: timeout }; if (fetchOptions.body instanceof ArrayBuffer) { @@ -37531,7 +37520,7 @@ var TextSecureServer = (function() { } } } - if ( 0 <= response.status && response.status < 400) { + if (0 <= response.status && response.status < 400) { console.log(options.type, url, response.status, 'Success'); resolve(result, response.status); } else { @@ -37544,7 +37533,6 @@ var TextSecureServer = (function() { console.log(e); reject(HTTPError(0, e.toString(), options.stack)); }); - scheduleHangWorkaround(); }); } @@ -37620,7 +37608,8 @@ var TextSecureServer = (function() { user : this.username, password : this.password, validateResponse: param.validateResponse, - certificateAuthorities: window.config.certificateAuthorities + certificateAuthorities: window.config.certificateAuthorities, + timeout : param.timeout }).catch(function(e) { var code = e.code; if (code === 200) { @@ -37669,7 +37658,8 @@ var TextSecureServer = (function() { type : "GET", responseType: "arraybuffer", contentType : "application/octet-stream", - certificateAuthorities: window.config.certificateAuthorities + certificateAuthorities: window.config.certificateAuthorities, + timeout: 0 }); }, requestVerificationSMS: function(number) { @@ -37827,7 +37817,8 @@ var TextSecureServer = (function() { httpType : 'GET', urlParameters : '/' + id, responseType : 'json', - validateResponse : {location: 'string'} + validateResponse : {location: 'string'}, + timeout : 0 }).then(function(response) { return ajax(response.location, { type : "GET", @@ -37841,6 +37832,7 @@ var TextSecureServer = (function() { call : 'attachment', httpType : 'GET', responseType : 'json', + timeout : 0 }).then(function(response) { return ajax(response.location, { type : "PUT", diff --git a/libtextsecure/api.js b/libtextsecure/api.js index fc2e12f98334..0ada610c43e0 100644 --- a/libtextsecure/api.js +++ b/libtextsecure/api.js @@ -24,22 +24,8 @@ var TextSecureServer = (function() { return true; } - // On Linux/Electron multiple quick web requests can result in the Node.js event - // loop getting wedged. Bug: https://github.com/electron/electron/issues/10570 - // This forces the event loop to move. - function scheduleHangWorkaround() { - setTimeout(function() { - setImmediate(function() { - // noop - }); - }, 1000); - } - function createSocket(url) { var requestOptions = { ca: window.config.certificateAuthorities }; - - scheduleHangWorkaround(); - return new nodeWebSocket(url, null, null, null, requestOptions); } @@ -51,11 +37,14 @@ var TextSecureServer = (function() { url = options.host + '/' + options.path; } console.log(options.type, url); + var timeout = typeof options.timeout !== 'undefined' ? options.timeout : 5000; + var fetchOptions = { method: options.type, body: options.data || null, headers: { 'X-Signal-Agent': 'OWD' }, - agent: new httpsAgent({ca: options.certificateAuthorities}) + agent: new httpsAgent({ca: options.certificateAuthorities}), + timeout: timeout }; if (fetchOptions.body instanceof ArrayBuffer) { @@ -94,7 +83,7 @@ var TextSecureServer = (function() { } } } - if ( 0 <= response.status && response.status < 400) { + if (0 <= response.status && response.status < 400) { console.log(options.type, url, response.status, 'Success'); resolve(result, response.status); } else { @@ -107,7 +96,6 @@ var TextSecureServer = (function() { console.log(e); reject(HTTPError(0, e.toString(), options.stack)); }); - scheduleHangWorkaround(); }); } @@ -183,7 +171,8 @@ var TextSecureServer = (function() { user : this.username, password : this.password, validateResponse: param.validateResponse, - certificateAuthorities: window.config.certificateAuthorities + certificateAuthorities: window.config.certificateAuthorities, + timeout : param.timeout }).catch(function(e) { var code = e.code; if (code === 200) { @@ -232,7 +221,8 @@ var TextSecureServer = (function() { type : "GET", responseType: "arraybuffer", contentType : "application/octet-stream", - certificateAuthorities: window.config.certificateAuthorities + certificateAuthorities: window.config.certificateAuthorities, + timeout: 0 }); }, requestVerificationSMS: function(number) { @@ -390,7 +380,8 @@ var TextSecureServer = (function() { httpType : 'GET', urlParameters : '/' + id, responseType : 'json', - validateResponse : {location: 'string'} + validateResponse : {location: 'string'}, + timeout : 0 }).then(function(response) { return ajax(response.location, { type : "GET", @@ -404,6 +395,7 @@ var TextSecureServer = (function() { call : 'attachment', httpType : 'GET', responseType : 'json', + timeout : 0 }).then(function(response) { return ajax(response.location, { type : "PUT",