From db8088793f6a6ce27967068d661c9810cfc076e0 Mon Sep 17 00:00:00 2001 From: Tomasz Ciborski Date: Tue, 16 Nov 2021 11:05:40 +0100 Subject: [PATCH] fix: prevent double callback in _executeRequest --- lib/oauth2.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/lib/oauth2.js b/lib/oauth2.js index 77241c4..c7c8982 100644 --- a/lib/oauth2.js +++ b/lib/oauth2.js @@ -158,8 +158,13 @@ exports.OAuth2.prototype._executeRequest= function( http_library, options, post_ }); }); request.on('error', function(e) { - callbackCalled= true; - callback(e); + // `www.googleapis.com` does `ECONNRESET` just after data is received in `passBackControl` + // this prevents the callback from being called twice, first in passBackControl and second time in here + // see also NodeJS Stream documentation: "The 'error' event may be emitted by a Readable implementation at any time" + if(!callbackCalled) { + callbackCalled= true; + callback(e); + } }); if( (options.method == 'POST' || options.method == 'PUT') && post_body ) {