• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2
3// This is a regression test for https://github.com/nodejs/node/issues/7722.
4//
5// When nested timers have the same timeout, calling clearTimeout on the
6// older timer after it has fired causes the list the newer timer is in
7// to be deleted. Since the newer timer was not cleared, it still blocks
8// the event loop completing for the duration of its timeout, however, since
9// no reference exists to it in its list, it cannot be canceled and its
10// callback is not called when the timeout elapses.
11
12const common = require('../common');
13
14const TIMEOUT = common.platformTimeout(100);
15
16const handle1 = setTimeout(common.mustCall(function() {
17  // Cause the old TIMEOUT list to be deleted
18  clearTimeout(handle1);
19
20  // Cause a new list with the same key (TIMEOUT) to be created for this timer
21  const handle2 = setTimeout(common.mustNotCall(), TIMEOUT);
22
23  setTimeout(common.mustCall(function() {
24    // Attempt to cancel the second timer. Fix for this bug will keep the
25    // newer timer from being dereferenced by keeping its list from being
26    // erroneously deleted. If we are able to cancel the timer successfully,
27    // the bug is fixed.
28    clearTimeout(handle2);
29  }), 1);
30
31  // When this callback completes, `listOnTimeout` should now look at the
32  // correct list and refrain from removing the new TIMEOUT list which
33  // contains the reference to the newer timer.
34}), TIMEOUT);
35