From 64d22c320c67255b42c8185a333a92a499c95820 Mon Sep 17 00:00:00 2001 From: Denys Otrishko Date: Thu, 18 Jun 2020 22:58:42 +0300 Subject: [PATCH] timers: fix multipleResolves in promisified timeouts/immediates After successful timer finish the abort event callback would still reject (already resolved promise) upon calling abortController.abort(). Signed-off-by: Denys Otrishko PR-URL: https://github.com/nodejs/node/pull/33949 Reviewed-By: James M Snell Reviewed-By: Benjamin Gruenbaum Reviewed-By: Anna Henningsen --- lib/timers.js | 12 ++++++++---- test/parallel/test-timers-promisified.js | 19 +++++++++++++++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/lib/timers.js b/lib/timers.js index fce904019205e9..53e934f3c9ac7c 100644 --- a/lib/timers.js +++ b/lib/timers.js @@ -190,8 +190,10 @@ setTimeout[customPromisify] = function(after, value, options = {}) { insert(timeout, timeout._idleTimeout); if (signal) { signal.addEventListener('abort', () => { - clearTimeout(timeout); - reject(lazyDOMException('AbortError')); + if (!timeout._destroyed) { + clearTimeout(timeout); + reject(lazyDOMException('AbortError')); + } }, { once: true }); } }); @@ -340,8 +342,10 @@ setImmediate[customPromisify] = function(value, options = {}) { const immediate = new Immediate(resolve, [value]); if (signal) { signal.addEventListener('abort', () => { - clearImmediate(immediate); - reject(lazyDOMException('AbortError')); + if (!immediate._destroyed) { + clearImmediate(immediate); + reject(lazyDOMException('AbortError')); + } }, { once: true }); } }); diff --git a/test/parallel/test-timers-promisified.js b/test/parallel/test-timers-promisified.js index d470d2f97c79eb..f46dc3f24d20eb 100644 --- a/test/parallel/test-timers-promisified.js +++ b/test/parallel/test-timers-promisified.js @@ -10,6 +10,8 @@ const { promisify } = require('util'); const setTimeout = promisify(timers.setTimeout); const setImmediate = promisify(timers.setImmediate); +process.on('multipleResolves', common.mustNotCall()); + { const promise = setTimeout(1); promise.then(common.mustCall((value) => { @@ -66,6 +68,23 @@ const setImmediate = promisify(timers.setImmediate); assert.rejects(setImmediate(10, { signal }), /AbortError/); } +{ + // Check that aborting after resolve will not reject. + const ac = new AbortController(); + const signal = ac.signal; + setTimeout(10, undefined, { signal }).then(() => { + ac.abort(); + }); +} +{ + // Check that aborting after resolve will not reject. + const ac = new AbortController(); + const signal = ac.signal; + setImmediate(10, { signal }).then(() => { + ac.abort(); + }); +} + { Promise.all( [1, '', false, Infinity].map((i) => assert.rejects(setImmediate(10, i)), {