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