From 734eb17e5d19145c4c5a696aa94e81c9655272f5 Mon Sep 17 00:00:00 2001 From: Anatoli Papirovski Date: Wed, 11 Apr 2018 10:43:21 +0200 Subject: [PATCH] timers: fix subsequent enroll calls not working A bug was introduced in #17704 which meant that subsequent calls to enroll would unset the new _idleTimeout and the enrolled object could never again function as a timer. PR-URL: https://github.com/nodejs/node/pull/19936 Reviewed-By: Benjamin Gruenbaum Reviewed-By: Ruben Bridgewater Reviewed-By: Rich Trott Reviewed-By: Matteo Collina Reviewed-By: James M Snell Reviewed-By: Jeremiah Senkpiel --- lib/timers.js | 3 ++- test/parallel/test-timers-enroll-second-time.js | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-timers-enroll-second-time.js diff --git a/lib/timers.js b/lib/timers.js index ad00ede9748..7d3e7b50309 100644 --- a/lib/timers.js +++ b/lib/timers.js @@ -353,13 +353,14 @@ exports.unenroll = util.deprecate(unenroll, // This function does not start the timer, see `active()`. // Using existing objects as timers slightly reduces object overhead. function enroll(item, msecs) { - item._idleTimeout = validateTimerDuration(msecs); + msecs = validateTimerDuration(msecs); // if this item was already in a list somewhere // then we should unenroll it from that if (item._idleNext) unenroll(item); L.init(item); + item._idleTimeout = msecs; } exports.enroll = util.deprecate(enroll, diff --git a/test/parallel/test-timers-enroll-second-time.js b/test/parallel/test-timers-enroll-second-time.js new file mode 100644 index 00000000000..a914e6c4a18 --- /dev/null +++ b/test/parallel/test-timers-enroll-second-time.js @@ -0,0 +1,16 @@ +'use strict'; + +const common = require('../common'); + +const assert = require('assert'); +const timers = require('timers'); + +const enrollObj = { + _onTimeout: common.mustCall(), +}; + +timers.enroll(enrollObj, 1); +assert.strictEqual(enrollObj._idleTimeout, 1); +timers.enroll(enrollObj, 10); +assert.strictEqual(enrollObj._idleTimeout, 10); +timers.active(enrollObj);