Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

protect against double callbacks on error path #444

Merged
merged 1 commit into from
Feb 25, 2013

Conversation

spollack
Copy link
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 added a commit that referenced this pull request Feb 25, 2013
protect against double callbacks on error path
@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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants