From b4d93bd2039ad439cb3e173c8d185262df3a4e82 Mon Sep 17 00:00:00 2001 From: DigitalBrainJS Date: Fri, 12 Nov 2021 17:55:47 +0200 Subject: [PATCH 1/2] Fixes #4260: fixed race condition on immediate requests cancellation --- lib/cancel/CancelToken.js | 5 ++--- test/unit/adapters/http.js | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/lib/cancel/CancelToken.js b/lib/cancel/CancelToken.js index 089d6b9034..bbd976eb27 100644 --- a/lib/cancel/CancelToken.js +++ b/lib/cancel/CancelToken.js @@ -25,10 +25,9 @@ function CancelToken(executor) { this.promise.then(function(cancel) { if (!token._listeners) return; - var i; - var l = token._listeners.length; + var i = token._listeners.length; - for (i = 0; i < l; i++) { + while (i-- > 0) { token._listeners[i](cancel); } token._listeners = null; diff --git a/test/unit/adapters/http.js b/test/unit/adapters/http.js index 4a9258927b..0d31384637 100644 --- a/test/unit/adapters/http.js +++ b/test/unit/adapters/http.js @@ -1034,5 +1034,32 @@ describe('supports http with nodejs', function () { }); }); + it('should able to cancel multiple requests with CancelToken', function(done){ + server = http.createServer(function (req, res) { + res.end('ok'); + }).listen(4444, function () { + var CancelToken = axios.CancelToken; + var source = CancelToken.source(); + var canceledStack = []; + + var requests = [1, 2, 3, 4, 5].map(function(id){ + return axios + .get('/foo/bar', { cancelToken: source.token }) + .catch(function (e) { + if (!axios.isCancel(e)) { + throw e; + } + + canceledStack.push(id); + }); + }); + + source.cancel("Aborted by user"); + + Promise.all(requests).then(function () { + assert.deepStrictEqual(canceledStack.sort(), [1, 2, 3, 4, 5]) + }).then(done, done); + }); + }); }); From d362ac3e2606105c79cfcacb25992216616bef8b Mon Sep 17 00:00:00 2001 From: Jay Date: Mon, 9 May 2022 18:10:07 +0200 Subject: [PATCH 2/2] Update http.js --- test/unit/adapters/http.js | 1 - 1 file changed, 1 deletion(-) diff --git a/test/unit/adapters/http.js b/test/unit/adapters/http.js index 5ce6dba94b..05adfea251 100644 --- a/test/unit/adapters/http.js +++ b/test/unit/adapters/http.js @@ -1193,7 +1193,6 @@ describe('supports http with nodejs', function () { }).then(done, done); }); }); -}); it('should allow passing FormData', function (done) { var form = new FormData();