Skip to content

Instantly share code, notes, and snippets.

@dtudury
Created July 5, 2013 21:28
Show Gist options
  • Save dtudury/5937376 to your computer and use it in GitHub Desktop.
Save dtudury/5937376 to your computer and use it in GitHub Desktop.
a domain experiment related to nesting
/*
* !node nestedDomains
* ---
* error caught at 1: Error: depth reached
* error caught at 2: Error: rethrow from depth 1
* error caught at 3: Error: rethrow from depth 2
* error caught at 4: Error: rethrow from depth 3
* ---
*
* this makes sense based on the implication from 4th paragraph on implicit binding
* http://nodejs.org/api/domain.html#domain_implicit_binding
* "If you want to nest Domain objects as children of a parent Domain, then you must explicitly add them."
*
* however...
*
* !node nestedDomains unsafe
* ---
* ...
* ...
* error caught at 3: Error: rethrow from depth 4
* error caught at 4: Error: rethrow from depth 3
* error caught at 3: Error: rethrow from depth 4
* error caught at 4: Error: rethrow from depth 3
*
* node.js:0
* // Copyright Joyent, Inc. and other Node contributors.
* ^
* RangeError: Maximum call stack size exceeded
* ---
*/
var guardian = require('domain');
function nestAndThrow(depth, parentGuardian) {
if(depth > 0) {
var g = guardian.create();
if(parentGuardian) {
parentGuardian.add(g);
}
g.on('error', function(error) {
console.log("error caught at " + depth + ": " + error);
if(parentGuardian || process.argv.indexOf('unsafe') !== -1) {
throw new Error("rethrow from depth " + depth);
}
});
g.run(function() {
process.nextTick(function() {
nestAndThrow(depth - 1, g);
});
});
} else {
process.nextTick(function() {
throw new Error("depth reached");
});
}
}
nestAndThrow(4);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment