Skip to content

Commit 6815af6

Browse files
author
Rui Marinho
committed
Fix rare recursion condition in patchGlobal
1 parent 7bbab6e commit 6815af6

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
@@ -157,15 +157,31 @@ module.exports.patchGlobal = function patchGlobal(client, cb) {
157157
// at the end, if we still don't have a Client, let's make one!
158158
!(client instanceof Client) && (client = new Client());
159159

160+
var called = false;
160161
process.on('uncaughtException', function(err) {
161162
if(cb) { // bind event listeners only if a callback was supplied
162-
client.once('logged', function() {
163+
var onLogged = function onLogged() {
164+
called = false;
163165
cb(true, err);
164-
});
165-
client.once('error', function() {
166+
};
167+
168+
var onError = function onError() {
169+
called = false;
166170
cb(false, err);
167-
});
171+
};
172+
173+
if(called) {
174+
client.removeListener('logged', onLogged);
175+
client.removeListener('error', onError);
176+
return cb(false, err);
177+
}
178+
179+
client.once('logged', onLogged);
180+
client.once('error', onError);
168181
}
182+
183+
called = true;
184+
169185
client.captureError(err, function(result) {
170186
node_util.log('uncaughtException: '+client.getIdent(result));
171187
});

test/raven.client.js

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

300327
it('should use a custom transport', function(){

0 commit comments

Comments
 (0)