-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
domain: error handler runs outside of its domain #26211
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
'use strict'; | ||
|
||
const assert = require('assert'); | ||
const common = require('../common'); | ||
const domain = require('domain'); | ||
const EventEmitter = require('events'); | ||
|
||
/* | ||
* Make sure that the domains stack and the active domain is setup properly when | ||
* a domain's error handler is called due to an error event being emitted. | ||
* More specifically, we want to test that: | ||
* - the active domain in the domain's error handler is *not* that domain, *but* | ||
* the active domain is a any direct parent domain at the time the error was | ||
* emitted. | ||
* - the domains stack in the domain's error handler does *not* include that | ||
* domain, *but* it includes all parents of that domain when the error was | ||
* emitted. | ||
*/ | ||
const d1 = domain.create(); | ||
const d2 = domain.create(); | ||
const d3 = domain.create(); | ||
|
||
function checkExpectedDomains(err) { | ||
// First, check that domains stack and active domain is as expected when the | ||
// event handler is called synchronously via ee.emit('error'). | ||
if (domain._stack.length !== err.expectedStackLength) { | ||
console.error('expected domains stack length of %d, but instead is %d', | ||
err.expectedStackLength, domain._stack.length); | ||
process.exit(1); | ||
} | ||
|
||
if (process.domain !== err.expectedActiveDomain) { | ||
console.error('expected active domain to be %j, but instead is %j', | ||
err.expectedActiveDomain, process.domain); | ||
process.exit(1); | ||
} | ||
|
||
// Then make sure that the domains stack and active domain is setup as | ||
// expected when executing a callback scheduled via nextTick from the error | ||
// handler. | ||
process.nextTick(() => { | ||
const expectedStackLengthInNextTickCb = | ||
err.expectedStackLength > 0 ? 1 : 0; | ||
if (domain._stack.length !== expectedStackLengthInNextTickCb) { | ||
console.error('expected stack length in nextTick cb to be %d, ' + | ||
'but instead is %d', expectedStackLengthInNextTickCb, | ||
domain._stack.length); | ||
process.exit(1); | ||
} | ||
|
||
const expectedActiveDomainInNextTickCb = | ||
expectedStackLengthInNextTickCb === 0 ? undefined : | ||
err.expectedActiveDomain; | ||
if (process.domain !== expectedActiveDomainInNextTickCb) { | ||
console.error('expected active domain in nextTick cb to be %j, ' + | ||
'but instead is %j', expectedActiveDomainInNextTickCb, | ||
process.domain); | ||
process.exit(1); | ||
} | ||
}); | ||
} | ||
|
||
d1.on('error', common.mustCall((err) => { | ||
checkExpectedDomains(err); | ||
}, 2)); | ||
|
||
d2.on('error', common.mustCall((err) => { | ||
checkExpectedDomains(err); | ||
}, 2)); | ||
|
||
d3.on('error', common.mustCall((err) => { | ||
checkExpectedDomains(err); | ||
}, 1)); | ||
|
||
d1.run(() => { | ||
const ee = new EventEmitter(); | ||
assert.strictEqual(process.domain, d1); | ||
assert.strictEqual(domain._stack.length, 1); | ||
|
||
const err = new Error('oops'); | ||
err.expectedStackLength = 0; | ||
err.expectedActiveDomain = null; | ||
ee.emit('error', err); | ||
|
||
assert.strictEqual(process.domain, d1); | ||
assert.strictEqual(domain._stack.length, 1); | ||
}); | ||
|
||
d1.run(() => { | ||
d1.run(() => { | ||
const ee = new EventEmitter(); | ||
|
||
assert.strictEqual(process.domain, d1); | ||
assert.strictEqual(domain._stack.length, 2); | ||
|
||
const err = new Error('oops'); | ||
err.expectedStackLength = 0; | ||
err.expectedActiveDomain = null; | ||
ee.emit('error', err); | ||
|
||
assert.strictEqual(process.domain, d1); | ||
assert.strictEqual(domain._stack.length, 2); | ||
}); | ||
}); | ||
|
||
d1.run(() => { | ||
d2.run(() => { | ||
const ee = new EventEmitter(); | ||
|
||
assert.strictEqual(process.domain, d2); | ||
assert.strictEqual(domain._stack.length, 2); | ||
|
||
const err = new Error('oops'); | ||
err.expectedStackLength = 1; | ||
err.expectedActiveDomain = d1; | ||
ee.emit('error', err); | ||
|
||
assert.strictEqual(process.domain, d2); | ||
assert.strictEqual(domain._stack.length, 2); | ||
}); | ||
}); | ||
|
||
d1.run(() => { | ||
d2.run(() => { | ||
d2.run(() => { | ||
const ee = new EventEmitter(); | ||
|
||
assert.strictEqual(process.domain, d2); | ||
assert.strictEqual(domain._stack.length, 3); | ||
|
||
const err = new Error('oops'); | ||
err.expectedStackLength = 1; | ||
err.expectedActiveDomain = d1; | ||
ee.emit('error', err); | ||
|
||
assert.strictEqual(process.domain, d2); | ||
assert.strictEqual(domain._stack.length, 3); | ||
}); | ||
}); | ||
}); | ||
|
||
d3.run(() => { | ||
d1.run(() => { | ||
d3.run(() => { | ||
d3.run(() => { | ||
const ee = new EventEmitter(); | ||
|
||
assert.strictEqual(process.domain, d3); | ||
assert.strictEqual(domain._stack.length, 4); | ||
|
||
const err = new Error('oops'); | ||
err.expectedStackLength = 2; | ||
err.expectedActiveDomain = d1; | ||
ee.emit('error', err); | ||
|
||
assert.strictEqual(process.domain, d3); | ||
assert.strictEqual(domain._stack.length, 4); | ||
}); | ||
}); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const domain = require('domain'); | ||
|
||
/* | ||
* Make sure that when an erorr is thrown from a nested domain, its error | ||
* handler runs outside of that domain, but within the context of any parent | ||
* domain. | ||
*/ | ||
|
||
const d = domain.create(); | ||
const d2 = domain.create(); | ||
|
||
d2.on('error', common.mustCall((err) => { | ||
if (domain._stack.length !== 1) { | ||
console.error('domains stack length should be 1 but is %d', | ||
domain._stack.length); | ||
process.exit(1); | ||
} | ||
|
||
if (process.domain !== d) { | ||
console.error('active domain should be %j but is %j', d, process.domain); | ||
process.exit(1); | ||
} | ||
|
||
process.nextTick(() => { | ||
if (domain._stack.length !== 1) { | ||
console.error('domains stack length should be 1 but is %d', | ||
domain._stack.length); | ||
process.exit(1); | ||
} | ||
|
||
if (process.domain !== d) { | ||
console.error('active domain should be %j but is %j', d, | ||
process.domain); | ||
process.exit(1); | ||
} | ||
}); | ||
})); | ||
|
||
d.run(() => { | ||
d2.run(() => { | ||
throw new Error('oops'); | ||
}); | ||
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.