Skip to content

A body with a Readable can cause uncaughtException #1507

@kanongil

Description

@kanongil

Describe the bug

  • Node.js version: v14.14.0
  • OS & version: n/a

If a Readable that has been passed as a body of a request is too quick to finish, got will fail to perform the request, and instead throw an error in an end handler.

Note that in general, it is bad idea to ever throw in an event handler, since it will cause other listeners to not be called, and will likely (as in this case) cause en uncaughtException. The error should probably have been emitted/rejected on the got request instead.

Actual behavior

Process exits with uncaughtException:

<snip>/node_modules/got/dist/source/core/index.js:687
            throw new TypeError('The payload has been already provided');
            ^

TypeError: The payload has been already provided
    at Request.onLockedWrite (<snip>/node_modules/got/dist/source/core/index.js:687:19)
    at onend (_stream_readable.js:684:10)
    at processTicksAndRejections (internal/process/task_queues.js:75:11)

Expected behavior

It actually attempts to post the stream payload.

Code to reproduce

const { Readable } = require('stream');
const Got = require('got');

const stream = new Readable({ read() {} });
const request = Got.post('http://www.google.com', {
    body: stream
});

stream.push('hi');
stream.push(null);
stream.resume();

Checklist

  • I have read the documentation.
  • I have tried my code with the latest version of Node.js and Got.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions