protect against double callbacks on error path #444

Merged
merged 1 commit into from Feb 25, 2013

Projects

None yet

2 participants

@spollack
Contributor

In the case that the client callback passed into request throws an error, the request callback may get called twice. Setting request's _callbackCalled flag before invoking the callback protects against this.

See here for some background:
mochajs/mocha#755
#330

Here is a simple mocha testcase that shows one example of this happening. In this case, i'm forcing the issue by using a url that request will fail to fetch, and then throwing an error in the callback.

var request = require('request');

function testFunction(requestOptions, onCompletion) {

    var hasBeenCalled = false;

    request(requestOptions, function (error) {
        if (hasBeenCalled) {
            console.log('WARNING multiple callback');
        }
        hasBeenCalled = true;

        throw new Error('whoops');

        onCompletion(error);
    });
}

describe('testFunction', function () {
    it('should not invoke the request callback twice', function (done) {
        testFunction({url:'http://localhost:6789'}, done);
    });
});
@mikeal mikeal merged commit 05de348 into request:master Feb 25, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment