2012-04-07 01:26:18 +02:00
|
|
|
// 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 util = require('util');
|
|
|
|
var events = require('events');
|
|
|
|
var EventEmitter = events.EventEmitter;
|
|
|
|
var inherits = util.inherits;
|
|
|
|
|
|
|
|
// methods that are called when trying to shut down expliclitly bound EEs
|
2012-04-18 00:52:57 +02:00
|
|
|
var endMethods = ['end', 'abort', 'destroy', 'destroySoon'];
|
2012-04-07 01:26:18 +02:00
|
|
|
|
|
|
|
// communicate with events module, but don't require that
|
|
|
|
// module to have to load this one, since this module has
|
|
|
|
// a few side effects.
|
|
|
|
events.usingDomains = true;
|
|
|
|
|
2013-03-26 06:32:41 +01:00
|
|
|
// let the process know we're using domains
|
|
|
|
process._usingDomains();
|
2013-02-27 20:46:35 +01:00
|
|
|
|
2012-04-07 01:26:18 +02:00
|
|
|
exports.Domain = Domain;
|
|
|
|
|
|
|
|
exports.create = exports.createDomain = function(cb) {
|
|
|
|
return new Domain(cb);
|
|
|
|
};
|
|
|
|
|
|
|
|
// it's possible to enter one domain while already inside
|
|
|
|
// another one. the stack is each entered domain.
|
|
|
|
var stack = [];
|
2012-07-17 01:38:11 +02:00
|
|
|
exports._stack = stack;
|
2012-04-07 01:26:18 +02:00
|
|
|
// the active domain is always the one that we're currently in.
|
|
|
|
exports.active = null;
|
|
|
|
|
|
|
|
|
|
|
|
inherits(Domain, EventEmitter);
|
|
|
|
|
|
|
|
function Domain() {
|
2012-12-27 16:51:31 +01:00
|
|
|
EventEmitter.call(this);
|
2012-04-07 01:26:18 +02:00
|
|
|
|
|
|
|
this.members = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
Domain.prototype.enter = function() {
|
|
|
|
if (this._disposed) return;
|
|
|
|
|
|
|
|
// note that this might be a no-op, but we still need
|
|
|
|
// to push it onto the stack so that we can pop it later.
|
|
|
|
exports.active = process.domain = this;
|
|
|
|
stack.push(this);
|
|
|
|
};
|
|
|
|
|
|
|
|
Domain.prototype.exit = function() {
|
2014-01-09 06:19:31 +01:00
|
|
|
// skip disposed domains, as usual, but also don't do anything if this
|
|
|
|
// domain is not on the stack.
|
|
|
|
var index = stack.lastIndexOf(this);
|
|
|
|
if (this._disposed || index === -1) return;
|
2012-04-07 01:26:18 +02:00
|
|
|
|
|
|
|
// exit all domains until this one.
|
2014-01-09 06:19:31 +01:00
|
|
|
stack.splice(index);
|
2012-04-07 01:26:18 +02:00
|
|
|
|
|
|
|
exports.active = stack[stack.length - 1];
|
|
|
|
process.domain = exports.active;
|
|
|
|
};
|
|
|
|
|
|
|
|
// note: this works for timers as well.
|
|
|
|
Domain.prototype.add = function(ee) {
|
|
|
|
// disposed domains can't be used for new things.
|
|
|
|
if (this._disposed) return;
|
|
|
|
|
|
|
|
// already added to this domain.
|
|
|
|
if (ee.domain === this) return;
|
|
|
|
|
|
|
|
// has a domain already - remove it first.
|
|
|
|
if (ee.domain) {
|
|
|
|
ee.domain.remove(ee);
|
|
|
|
}
|
|
|
|
|
|
|
|
// check for circular Domain->Domain links.
|
|
|
|
// This causes bad insanity!
|
|
|
|
//
|
|
|
|
// For example:
|
|
|
|
// var d = domain.create();
|
|
|
|
// var e = domain.create();
|
|
|
|
// d.add(e);
|
|
|
|
// e.add(d);
|
|
|
|
// e.emit('error', er); // RangeError, stack overflow!
|
|
|
|
if (this.domain && (ee instanceof Domain)) {
|
|
|
|
for (var d = this.domain; d; d = d.domain) {
|
|
|
|
if (ee === d) return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ee.domain = this;
|
|
|
|
this.members.push(ee);
|
|
|
|
};
|
|
|
|
|
|
|
|
Domain.prototype.remove = function(ee) {
|
|
|
|
ee.domain = null;
|
|
|
|
var index = this.members.indexOf(ee);
|
|
|
|
if (index !== -1) {
|
|
|
|
this.members.splice(index, 1);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Domain.prototype.run = function(fn) {
|
2012-06-08 20:06:10 +02:00
|
|
|
return this.bind(fn)();
|
2012-04-07 01:26:18 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
Domain.prototype.intercept = function(cb) {
|
|
|
|
return this.bind(cb, true);
|
|
|
|
};
|
|
|
|
|
|
|
|
Domain.prototype.bind = function(cb, interceptError) {
|
|
|
|
// if cb throws, catch it here.
|
|
|
|
var self = this;
|
|
|
|
var b = function() {
|
|
|
|
// disposing turns functions into no-ops
|
|
|
|
if (self._disposed) return;
|
|
|
|
|
|
|
|
if (this instanceof Domain) {
|
|
|
|
return cb.apply(this, arguments);
|
|
|
|
}
|
|
|
|
|
|
|
|
// only intercept first-arg errors if explicitly requested.
|
|
|
|
if (interceptError && arguments[0] &&
|
|
|
|
(arguments[0] instanceof Error)) {
|
|
|
|
var er = arguments[0];
|
2012-06-08 19:59:35 +02:00
|
|
|
util._extend(er, {
|
2012-12-26 21:31:27 +01:00
|
|
|
domainBound: cb,
|
|
|
|
domainThrown: false,
|
2012-04-07 01:26:18 +02:00
|
|
|
domain: self
|
|
|
|
});
|
|
|
|
self.emit('error', er);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-06-06 17:17:01 +02:00
|
|
|
// remove first-arg if intercept as assumed to be the error-arg
|
|
|
|
if (interceptError) {
|
|
|
|
var len = arguments.length;
|
|
|
|
var args;
|
|
|
|
switch (len) {
|
|
|
|
case 0:
|
|
|
|
case 1:
|
|
|
|
// no args that we care about.
|
|
|
|
args = [];
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
// optimization for most common case: cb(er, data)
|
|
|
|
args = [arguments[1]];
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// slower for less common case: cb(er, foo, bar, baz, ...)
|
|
|
|
args = new Array(len - 1);
|
|
|
|
for (var i = 1; i < len; i++) {
|
2012-07-08 02:30:07 +02:00
|
|
|
args[i - 1] = arguments[i];
|
2012-06-06 17:17:01 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
self.enter();
|
|
|
|
var ret = cb.apply(this, args);
|
|
|
|
self.exit();
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2012-04-07 01:26:18 +02:00
|
|
|
self.enter();
|
|
|
|
var ret = cb.apply(this, arguments);
|
|
|
|
self.exit();
|
|
|
|
return ret;
|
|
|
|
};
|
|
|
|
b.domain = this;
|
|
|
|
return b;
|
|
|
|
};
|
|
|
|
|
|
|
|
Domain.prototype.dispose = function() {
|
|
|
|
if (this._disposed) return;
|
|
|
|
|
domain: Properly exit() on domain disposal
This addresses #4034. There are two problems happening:
1. The domain is not exited automatically when calling dispose() on it.
Then, since the domain is disposed, attempting to exit it again will do
nothing.
2. The active domain is stored on process.domain. Since thrown errors
call `process.emit('uncaughtException', er)`, and the process is an
event emitter with a `.domain` member, it re-enters the domain a second
time before calling the error handler, pushing it onto the stack again.
Thus, if the handler calls `domain.dispose()`, then the domain is now on
the stack twice, and cannot be exited properly. Since the domain is
disposed, any subsequent IO will be no-op'ed, since we've declared that
this context is done and best forgotten.
The solution here is twofold:
1. In EventEmitter.emit, do not enter the domain if `this===process`.
2. Automatically exit the domain when calling `domain.dispose()`.
2012-09-21 00:13:36 +02:00
|
|
|
// if we're the active domain, then get out now.
|
|
|
|
this.exit();
|
|
|
|
|
2012-04-07 01:26:18 +02:00
|
|
|
this.emit('dispose');
|
|
|
|
|
|
|
|
// remove error handlers.
|
|
|
|
this.removeAllListeners();
|
|
|
|
this.on('error', function() {});
|
|
|
|
|
|
|
|
// try to kill all the members.
|
|
|
|
// XXX There should be more consistent ways
|
|
|
|
// to shut down things!
|
|
|
|
this.members.forEach(function(m) {
|
|
|
|
// if it's a timeout or interval, cancel it.
|
|
|
|
clearTimeout(m);
|
|
|
|
|
|
|
|
// drop all event listeners.
|
|
|
|
if (m instanceof EventEmitter) {
|
|
|
|
m.removeAllListeners();
|
|
|
|
// swallow errors
|
|
|
|
m.on('error', function() {});
|
|
|
|
}
|
|
|
|
|
|
|
|
// Be careful!
|
|
|
|
// By definition, we're likely in error-ridden territory here,
|
|
|
|
// so it's quite possible that calling some of these methods
|
|
|
|
// might cause additional exceptions to be thrown.
|
|
|
|
endMethods.forEach(function(method) {
|
|
|
|
if (typeof m[method] === 'function') {
|
|
|
|
try {
|
|
|
|
m[method]();
|
|
|
|
} catch (er) {}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
// remove from parent domain, if there is one.
|
|
|
|
if (this.domain) this.domain.remove(this);
|
|
|
|
|
|
|
|
// kill the references so that they can be properly gc'ed.
|
|
|
|
this.members.length = 0;
|
|
|
|
|
|
|
|
// finally, mark this domain as 'no longer relevant'
|
|
|
|
// so that it can't be entered or activated.
|
|
|
|
this._disposed = true;
|
|
|
|
};
|