Skip to content

Commit

Permalink
timer: don't reschedule timer bucket in a domain
Browse files Browse the repository at this point in the history
If two timers run on the same tick, and the first timer uses a domain,
and then catches an exception and disposes of the domain, then the
second timer never runs. (And even if the first timer does not dispose
of the domain, the second timer could run under the wrong domain.)

This happens because timer.js uses "process.nextTick()" to schedule
continued processing of the timers for that tick. However, there was
an exception inside a domain, then "process.nextTick()" runs under
the domain of the first timer function, and will do nothing if
the domain has been disposed.

To avoid this, we temporarily save the value of "process.domain"
before calling nextTick so that it does not run inside any domain.
  • Loading branch information
Greg Brail authored and tjfontaine committed Mar 4, 2014
1 parent 06453a9 commit 6eb4d1d
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/timers.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,15 @@ function listOnTimeout() {
threw = false;
} finally {
if (threw) {
// We need to continue processing after domain error handling
// is complete, but not by using whatever domain was left over
// when the timeout threw its exception.
var oldDomain = process.domain;
process.domain = null;
process.nextTick(function() {
list.ontimeout();
});
process.domain = oldDomain;

This comment has been minimized.

Copy link
@Fishrock123

Fishrock123 Nov 23, 2015

Contributor

@misterdjules What does this actually do? (And how? The domain swap is lost because nextTick is fired later..)

The test passes with or without this...

This comment has been minimized.

Copy link
@Fishrock123

Fishrock123 Nov 23, 2015

Contributor

Ah Trevor pointed me to this: https://github.com/nodejs/node/blob/master/src/node.js#L482-L504

I guess we're trying to make it have no domain, but that doesn't seem to change anything...

This comment has been minimized.

Copy link
@misterdjules

misterdjules Nov 23, 2015

@Fishrock123

What does this actually do? (And how? The domain swap is lost because nextTick is fired later..)

The domain swap is not lost, because when the domain module is loaded, the handler for callbacks scheduled with process.nextTick is replaced with process._nextDomainTick, and process._nextDomainTick captures the value of process.domain at the time of the call to process.nextTick.

The rationale behind this change is that, if a timer's callback throws an error, the rest of the timers in the list have to be processed asynchronously with process.nextTick, and process.nextTick should not use the domain of the timer that threw an error as the process' domain.

The test in this commit tests this by attaching timer A to a domain, and throwing an error in timer A's callback. In timer A's domain error handler, that domain is disposed. When timer B is scheduled with process.nextTick, without that fix the nextTick callbacks handler would find that the process' domain is disposed, and would not schedule the timer's callback](https://github.com/nodejs/node/blob/v0.10/src/node.js#L487), with the fix the second timer's callback is called.

The test passes with or without this...

That's not what I'm seeing:

➜  v0.10 git:(v0.10) ✗ git rev-parse --short HEAD
6ac47aa
➜  v0.10 git:(v0.10) ✗ make -j 6               
/Applications/Xcode.app/Contents/Developer/usr/bin/make -C out BUILDTYPE=Release V=1
make[1]: Nothing to be done for `all'.
ln -fs out/Release/node node
➜  v0.10 git:(v0.10) ✗ git diff
diff --git a/lib/timers.js b/lib/timers.js
index 83493eb..44f2136 100644
--- a/lib/timers.js
+++ b/lib/timers.js
@@ -127,12 +127,12 @@ function listOnTimeout() {
           // We need to continue processing after domain error handling
           // is complete, but not by using whatever domain was left over
           // when the timeout threw its exception.
-          var oldDomain = process.domain;
-          process.domain = null;
+          //var oldDomain = process.domain;
+          //process.domain = null;
           process.nextTick(function() {
             list.ontimeout();
           });
-          process.domain = oldDomain;
+          //process.domain = oldDomain;
         }
       }
     }
➜  v0.10 git:(v0.10) ✗ ./node test/simple/test-domain-exit-dispose-again.js
0 null
1 null
2 null
{ [ReferenceError: err3 is not defined]
  domain: 
   { domain: null,
     _events: { error: [Function] },
     _maxListeners: 10,
     members: [],
     _disposed: true },
  domainThrown: true }
in handler undefined false
3 null
4 null
5 null
6 null
7 null
8 null
9 null

assert.js:93
  throw new assert.AssertionError({
        ^
AssertionError: false == true
    at process.<anonymous> (/Users/JulienGilli/dev/node/v0.10/test/simple/test-domain-exit-dispose-again.js:74:3)
    at process.emit (events.js:117:20)
➜  v0.10 git:(v0.10) ✗

This comment has been minimized.

Copy link
@Fishrock123

Fishrock123 Nov 23, 2015

Contributor

It appears to pass without it on master (and almost certainly v4 / v5).

This comment has been minimized.

Copy link
@misterdjules

misterdjules Nov 23, 2015

Just for the record, if anyone else reads that discussion, the test passes on master/v4/v5 because nextTick callbacks still run even if their domain is disposed, whereas they do not run on v0.10 if their domain is disposed.

This comment has been minimized.

Copy link
@misterdjules

misterdjules Nov 23, 2015

Basically, the following code:

var domain = require('domain');
var assert = require('assert');

var d = domain.create();
d.run(function() {
    process.nextTick(function shouldNotRun() {
        assert(false);
    });

    d.dispose();
})

asserts on node v4.x/v5.x and master, and exits successfully with node v0.10.

}
}
}
Expand Down
76 changes: 76 additions & 0 deletions test/simple/test-domain-exit-dispose-again.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
// Copyright Joyent, Inc. and other Node contributors.
//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

var common = require('../common.js');
var assert = require('assert');
var domain = require('domain');
var disposalFailed = false;

// no matter what happens, we should increment a 10 times.
var a = 0;
log();
function log(){
console.log(a++, process.domain);
if (a < 10) setTimeout(log, 20);
}

var secondTimerRan = false;

// in 50ms we'll throw an error.
setTimeout(err, 50);
setTimeout(secondTimer, 50);
function err(){
var d = domain.create();
d.on('error', handle);
d.run(err2);

function err2() {
// this timeout should never be called, since the domain gets
// disposed when the error happens.
setTimeout(function() {
console.error('This should not happen.');
disposalFailed = true;
process.exit(1);
});

// this function doesn't exist, and throws an error as a result.
err3();
}

function handle(e) {
// this should clean up everything properly.
d.dispose();
console.error(e);
console.error('in handler', process.domain, process.domain === d);
}
}

function secondTimer() {
console.log('In second timer');
secondTimerRan = true;
}

process.on('exit', function() {
assert.equal(a, 10);
assert.equal(disposalFailed, false);
assert(secondTimerRan);
console.log('ok');
});

0 comments on commit 6eb4d1d

Please sign in to comment.