From 217714ca2da37ce06f0a64033978be7ff32172b4 Mon Sep 17 00:00:00 2001 From: fengmk2 Date: Mon, 4 Mar 2013 10:07:47 +0800 Subject: [PATCH] domain.dispose() before handle next(err) --- lib/connect-domain.js | 6 +++++- test/domain.test.js | 19 ++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/lib/connect-domain.js b/lib/connect-domain.js index 17b7fac..0693c78 100644 --- a/lib/connect-domain.js +++ b/lib/connect-domain.js @@ -8,7 +8,11 @@ module.exports = function () { reqDomain.dispose(); }); - reqDomain.on('error', next); + reqDomain.on('error', function (err) { + // Once a domain is disposed, further errors from the emitters in that set will be ignored. + reqDomain.dispose(); + next(err); + }); reqDomain.run(next); }; diff --git a/test/domain.test.js b/test/domain.test.js index a5fc8d2..6ebdfa6 100644 --- a/test/domain.test.js +++ b/test/domain.test.js @@ -26,6 +26,15 @@ describe('domain.test.js', function () { }); return; } + if (req.url === '/async_error_twice') { + setTimeout(function () { + ff.foo(); + }, 100); + setTimeout(function () { + bar.bar(); + }, 210); + return; + } res.end(req.url); }; @@ -60,9 +69,10 @@ describe('domain.test.js', function () { before(function () { mochaHandler = process.listeners('uncaughtException').pop(); }); - after(function () { + after(function (done) { // ...but be sure to re-enable mocha's error handler process.on('uncaughtException', mochaHandler); + setTimeout(done, 500); }); it('should GET /async_error status 500', function (done) { @@ -71,6 +81,13 @@ describe('domain.test.js', function () { .expect('ff is not defined') .expect(500, done); }); + + it('should GET /async_error_twice status 500', function (done) { + request(app) + .get('/async_error_twice') + .expect('ff is not defined') + .expect(500, done); + }); }); }); \ No newline at end of file