Skip to content

Commit 025188f

Browse files
committed
Merge pull request #84 from seegno/bugfix/recursion-patch-global
Fix rare recursion condition in patchGlobal
2 parents 897c59c + 6815af6 commit 025188f

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

lib/client.js

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -160,15 +160,31 @@ module.exports.patchGlobal = function patchGlobal(client, cb) {
160160
// at the end, if we still don't have a Client, let's make one!
161161
!(client instanceof Client) && (client = new Client());
162162

163+
var called = false;
163164
process.on('uncaughtException', function(err) {
164165
if(cb) { // bind event listeners only if a callback was supplied
165-
client.once('logged', function() {
166+
var onLogged = function onLogged() {
167+
called = false;
166168
cb(true, err);
167-
});
168-
client.once('error', function() {
169+
};
170+
171+
var onError = function onError() {
172+
called = false;
169173
cb(false, err);
170-
});
174+
};
175+
176+
if(called) {
177+
client.removeListener('logged', onLogged);
178+
client.removeListener('error', onError);
179+
return cb(false, err);
180+
}
181+
182+
client.once('logged', onLogged);
183+
client.once('error', onError);
171184
}
185+
186+
called = true;
187+
172188
client.captureError(err, function(result) {
173189
node_util.log('uncaughtException: '+client.getIdent(result));
174190
});

test/raven.client.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,33 @@ describe('raven.Client', function(){
297297
});
298298
process.emit('uncaughtException', new Error('derp'));
299299
});
300+
301+
it('should not enter in recursion when an error is thrown on client request', function(done){
302+
// remove existing uncaughtException handlers
303+
var uncaughtBefore = process._events.uncaughtException;
304+
process.removeAllListeners('uncaughtException');
305+
306+
var transportBefore = client.transport.send;
307+
308+
client.transport.send = function() {
309+
throw new Error('foo');
310+
};
311+
312+
client.patchGlobal(function(success, err){
313+
success.should.eql(false);
314+
err.should.be.instanceOf(Error);
315+
err.message.should.equal('foo');
316+
317+
// restore things to how they were
318+
process._events.uncaughtException = uncaughtBefore;
319+
client.transport.send = transportBefore;
320+
321+
done();
322+
});
323+
324+
325+
process.emit('uncaughtException', new Error('derp'));
326+
});
300327
});
301328

302329
describe('#process()', function(){

0 commit comments

Comments
 (0)