Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Sep 5, 2011
1 parent cf5d1ba commit 74c6de1
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 8 deletions.
22 changes: 21 additions & 1 deletion lib/domain.js
Expand Up @@ -41,6 +41,19 @@ Domain.prototype.enter = function() {
};


Domain.prototype.kill = function() {
for (var i = 0; i < this.handles.length; i++) {
console.error("kill " + this.handles[i]);
if (this.handles[i].close) {
this.handles[i].close();
} else {
this.handles[i].oncomplete = null;
}
}
this.handles = [];
};


Domain.prototype.exit = function() {
assert.ok(currentDomain == this);
currentDomain = null;
Expand All @@ -55,7 +68,14 @@ exports.pollNewDomains = function() {
var d;
while ((d = createQueue.shift())) {
d.enter();
d.cb(d.arg);

try {
d.cb(d.arg);
} catch (e) {
d.emit('error', e);
d.kill();
}

d.cb = null;
d.arg = null;
d.exit();
Expand Down
16 changes: 11 additions & 5 deletions lib/timers_uv.js
Expand Up @@ -31,6 +31,12 @@ if (process.env.NODE_DEBUG && /timer/.test(process.env.NODE_DEBUG)) {
}


function closeTimer(t) {
t.close();
require('domain').remove(t);
}


// IDLE TIMEOUTS
//
// Because often many sockets will have the same idle timeout we will not
Expand Down Expand Up @@ -89,7 +95,7 @@ function insert(item, msecs) {

debug(msecs + ' list empty');
assert(L.isEmpty(list));
list.close();
closeTimer(list);
delete lists[msecs];
};
}
Expand All @@ -107,7 +113,7 @@ var unenroll = exports.unenroll = function(item) {
debug('unenroll');
if (list && L.isEmpty(list)) {
debug('unenroll: list empty');
list.close();
closeTimer(list);
delete lists[item._idleTimeout];
}
};
Expand Down Expand Up @@ -158,7 +164,7 @@ exports.setTimeout = function(callback, after) {
args = Array.prototype.slice.call(arguments, 2);
timer._onTimeout = function() {
callback.apply(timer, args);
timer.close();
closeTimer(timer);
}

timer.start(0, 0);
Expand Down Expand Up @@ -196,7 +202,7 @@ exports.clearTimeout = function(timer) {
if (timer && (timer.ontimeout || timer._onTimeout)) {
timer.ontimeout = timer._onTimeout = null;
if (timer instanceof Timer) {
timer.close(); // for after === 0
closeTimer(timer); // for after === 0
} else {
exports.unenroll(timer);
}
Expand All @@ -221,6 +227,6 @@ exports.setInterval = function(callback, repeat) {
exports.clearInterval = function(timer) {
if (timer instanceof Timer) {
timer.ontimeout = null;
timer.close();
closeTimer(timer);
}
};
7 changes: 6 additions & 1 deletion src/node.js
Expand Up @@ -521,7 +521,12 @@
domain.enter();
}

obj[method](arg0, arg1, arg2, arg3);
try {
obj[method](arg0, arg1, arg2, arg3);
} catch (e) {
domain.emit('error', e);
domain.kill();
}

if (domain) {
domain.exit();
Expand Down
15 changes: 14 additions & 1 deletion test/simple/test-domains.js
Expand Up @@ -23,15 +23,28 @@ var common = require('../common');
var assert = require('assert');
var domain = require('domain');

var timerCalled = false;

console.log("default domain");

var d = domain.create({ hello: "world" }, function(a) {
console.log("inside the domain");
assert.deepEqual(a, { hello: "world" });

setTimeout(function() {
timerCalled = true;
}, 1000);

throw new Error("synthetic error");
});


d.on("exit", function() {
d.on("error", function(e) {
assert.equal(e.message, "synthetic error");
console.log("domain exited");
});


process.on('exit', function() {
assert.equal(false, timerCalled);
});

0 comments on commit 74c6de1

Please sign in to comment.