| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent 403d7ee commit f6b0933
2 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -481,31 +481,36 @@ function _makeTimerTimeout(timer) { | |||
| 481 | 481 | var domain = timer.domain; | |
| 482 | 482 | var msecs = timer._idleTimeout; | |
| 483 | 483 | ||
| 484 | + L.remove(timer); | ||
| 485 | + | ||
| 484 | 486 | // Timer has been unenrolled by another timer that fired at the same time, | |
| 485 | 487 | // so don't make it timeout. | |
| 486 | - if (!msecs || msecs < 0) | ||
| 488 | + if (msecs <= 0) | ||
| 487 | 489 | return; | |
| 488 | 490 | ||
| 489 | 491 | if (!timer._onTimeout) | |
| 490 | 492 | return; | |
| 491 | 493 | ||
| 492 | - if (domain && domain._disposed) | ||
| 493 | - return; | ||
| 494 | + if (domain) { | ||
| 495 | + if (domain._disposed) | ||
| 496 | + return; | ||
| 494 | 497 | ||
| 495 | - try { | ||
| 496 | - var threw = true; | ||
| 498 | + domain.enter(); | ||
| 499 | + } | ||
| 497 | 500 | ||
| 498 | - if (domain) domain.enter(); | ||
| 501 | + debug('unreftimer firing timeout'); | ||
| 502 | + timer._called = true; | ||
| 503 | + _runOnTimeout(timer); | ||
| 499 | 504 | ||
| 500 | - debug('unreftimer firing timeout'); | ||
| 501 | - L.remove(timer); | ||
| 502 | - timer._called = true; | ||
| 503 | - timer._onTimeout(); | ||
| 505 | + if (domain) | ||
| 506 | + domain.exit(); | ||
| 507 | + } | ||
| 504 | 508 | ||
| 509 | + function _runOnTimeout(timer) { | ||
| 510 | + var threw = true; | ||
| 511 | + try { | ||
| 512 | + timer._onTimeout(); | ||
| 505 | 513 | threw = false; | |
| 506 | - | ||
| 507 | - if (domain) | ||
| 508 | - domain.exit(); | ||
| 509 | 514 | } finally { | |
| 510 | 515 | if (threw) process.nextTick(unrefTimeout); | |
| 511 | 516 | } | |
@@ -519,7 +524,7 @@ function unrefTimeout() { | |||
| 519 | 524 | var timeSinceLastActive; | |
| 520 | 525 | var nextTimeoutTime; | |
| 521 | 526 | var nextTimeoutDuration; | |
| 522 | - var minNextTimeoutTime; | ||
| 527 | + var minNextTimeoutTime = TIMEOUT_MAX; | ||
| 523 | 528 | var timersToTimeout = []; | |
| 524 | 529 | ||
| 525 | 530 | // The actual timer fired and has not yet been rearmed, | |
@@ -534,7 +539,7 @@ function unrefTimeout() { | |||
| 534 | 539 | // and rearm the actual timer if the next timeout to expire | |
| 535 | 540 | // will expire before the current actual timer. | |
| 536 | 541 | var cur = unrefList._idlePrev; | |
| 537 | - while (cur != unrefList) { | ||
| 542 | + while (cur !== unrefList) { | ||
| 538 | 543 | timeSinceLastActive = now - cur._idleStart; | |
| 539 | 544 | ||
| 540 | 545 | if (timeSinceLastActive < cur._idleTimeout) { | |
@@ -543,7 +548,7 @@ function unrefTimeout() { | |||
| 543 | 548 | ||
| 544 | 549 | nextTimeoutDuration = cur._idleTimeout - timeSinceLastActive; | |
| 545 | 550 | nextTimeoutTime = now + nextTimeoutDuration; | |
| 546 | - if (minNextTimeoutTime == null || | ||
| 551 | + if (minNextTimeoutTime === TIMEOUT_MAX || | ||
| 547 | 552 | (nextTimeoutTime < minNextTimeoutTime)) { | |
| 548 | 553 | // We found a timeout that will expire earlier, | |
| 549 | 554 | // store its next timeout time now so that we | |
@@ -569,7 +574,7 @@ function unrefTimeout() { | |||
| 569 | 574 | ||
| 570 | 575 | // Rearm the actual timer with the timeout delay | |
| 571 | 576 | // of the earliest timeout found. | |
| 572 | - if (minNextTimeoutTime != null) { | ||
| 577 | + if (minNextTimeoutTime !== TIMEOUT_MAX) { | ||
| 573 | 578 | unrefTimer.start(minNextTimeoutTime - now, 0); | |
| 574 | 579 | unrefTimer.when = minNextTimeoutTime; | |
| 575 | 580 | debug('unrefTimer rescheduled'); | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,46 @@ | |||
| 1 | + 'use strict'; | ||
| 2 | + | ||
| 3 | + // https://github.com/nodejs/node/pull/2540/files#r38231197 | ||
| 4 | + | ||
| 5 | + const common = require('../common'); | ||
| 6 | + const timers = require('timers'); | ||
| 7 | + const assert = require('assert'); | ||
| 8 | + const domain = require('domain'); | ||
| 9 | + | ||
| 10 | + // Crazy stuff to keep the process open, | ||
| 11 | + // then close it when we are actually done. | ||
| 12 | + const TEST_DURATION = common.platformTimeout(100); | ||
| 13 | + const keepOpen = setTimeout(function() { | ||
| 14 | + throw new Error('Test timed out. keepOpen was not canceled.'); | ||
| 15 | + }, TEST_DURATION); | ||
| 16 | + | ||
| 17 | + const endTest = makeTimer(2); | ||
| 18 | + | ||
| 19 | + const someTimer = makeTimer(1); | ||
| 20 | + someTimer.domain = domain.create(); | ||
| 21 | + someTimer.domain.dispose(); | ||
| 22 | + someTimer._onTimeout = function() { | ||
| 23 | + throw new Error('someTimer was not supposed to fire!'); | ||
| 24 | + }; | ||
| 25 | + | ||
| 26 | + endTest._onTimeout = common.mustCall(function() { | ||
| 27 | + assert.strictEqual(someTimer._idlePrev, null); | ||
| 28 | + assert.strictEqual(someTimer._idleNext, null); | ||
| 29 | + clearTimeout(keepOpen); | ||
| 30 | + }); | ||
| 31 | + | ||
| 32 | + const cancelsTimer = makeTimer(1); | ||
| 33 | + cancelsTimer._onTimeout = common.mustCall(function() { | ||
| 34 | + someTimer._idleTimeout = 0; | ||
| 35 | + }); | ||
| 36 | + | ||
| 37 | + timers._unrefActive(cancelsTimer); | ||
| 38 | + timers._unrefActive(someTimer); | ||
| 39 | + timers._unrefActive(endTest); | ||
| 40 | + | ||
| 41 | + function makeTimer(msecs) { | ||
| 42 | + const timer = {}; | ||
| 43 | + timers.unenroll(timer); | ||
| 44 | + timers.enroll(timer, msecs); | ||
| 45 | + return timer; | ||
| 46 | + } | ||
| Back | FazBrowse Home | New Git URL |
0 commit comments