From 6815af65d9ebda12222a94066f171480dfb29ea0 Mon Sep 17 00:00:00 2001 From: Rui Marinho Date: Sun, 27 Jul 2014 02:03:46 +0100 Subject: [PATCH] Fix rare recursion condition in patchGlobal --- lib/client.js | 24 ++++++++++++++++++++---- test/raven.client.js | 27 +++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/lib/client.js b/lib/client.js index 4147d43289b5..706c781f2e9e 100644 --- a/lib/client.js +++ b/lib/client.js @@ -157,15 +157,31 @@ module.exports.patchGlobal = function patchGlobal(client, cb) { // at the end, if we still don't have a Client, let's make one! !(client instanceof Client) && (client = new Client()); + var called = false; process.on('uncaughtException', function(err) { if(cb) { // bind event listeners only if a callback was supplied - client.once('logged', function() { + var onLogged = function onLogged() { + called = false; cb(true, err); - }); - client.once('error', function() { + }; + + var onError = function onError() { + called = false; cb(false, err); - }); + }; + + if(called) { + client.removeListener('logged', onLogged); + client.removeListener('error', onError); + return cb(false, err); + } + + client.once('logged', onLogged); + client.once('error', onError); } + + called = true; + client.captureError(err, function(result) { node_util.log('uncaughtException: '+client.getIdent(result)); }); diff --git a/test/raven.client.js b/test/raven.client.js index bebbf6daf422..4db1e597e697 100644 --- a/test/raven.client.js +++ b/test/raven.client.js @@ -295,6 +295,33 @@ describe('raven.Client', function(){ }); process.emit('uncaughtException', new Error('derp')); }); + + it('should not enter in recursion when an error is thrown on client request', function(done){ + // remove existing uncaughtException handlers + var uncaughtBefore = process._events.uncaughtException; + process.removeAllListeners('uncaughtException'); + + var transportBefore = client.transport.send; + + client.transport.send = function() { + throw new Error('foo'); + }; + + client.patchGlobal(function(success, err){ + success.should.eql(false); + err.should.be.instanceOf(Error); + err.message.should.equal('foo'); + + // restore things to how they were + process._events.uncaughtException = uncaughtBefore; + client.transport.send = transportBefore; + + done(); + }); + + + process.emit('uncaughtException', new Error('derp')); + }); }); it('should use a custom transport', function(){