Sitelet https://github.com/nodejs/node/issues/32184
Skip to content

Regression: socket hang up when uploading and streaming with pipeline #32184

Description

@rtbo

What steps will reproduce the bug?

git clone git@github.com:rtbo/node-failed-upload
cd node-failed-upload
npm install
npm run test

How often does it reproduce? Is there a required condition?

Everytime

What is the expected behavior?

With node v13.9.0:

> NODE_ENV=test mocha

  Upload test
    ✓ should work

  1 passing (30ms)

What do you see instead?

With node v13.10.0 and higher:

> NODE_ENV=test mocha

  Upload test
    1) should work

  0 passing (25ms)
  1 failing

  1) Upload test
       should work:
     Error: socket hang up
      at connResetException (internal/errors.js:613:14)
      at Socket.socketOnEnd (_http_client.js:463:23)
      at endReadableNT (_stream_readable.js:1201:12)
      at processTicksAndRejections (internal/process/task_queues.js:84:21)

Activity

  1. changed the title [-]Regression: socket hang up when uploading with custom headers[/-] [+]Regression: socket hang up when uploading and streaming with pipeline[/+] on Mar 10, 2020
  2. rtbo commented on Mar 10, 2020

    @rtbo
    Author

    TL;DR;

    Client receive "socket hang up" error when reaching this middleware:

    const Koa = require("koa");
    const fs = require("fs");
    const util = require("util");
    const stream = require("stream");
    
    const app = new Koa();
    
    const pipeline = util.promisify(stream.pipeline);
    
    app.use(async ctx => {
      const { "content-length": length } = ctx.request.headers;
    
      const openFlags = fs.constants.O_WRONLY | fs.constants.O_CREAT;
    
      const ws = fs.createWriteStream("test.bin", {
        flags: openFlags,
        encoding: "binary"
      });
    
      /*
        !!! commenting the following line disable the "socket hang up" error !!!
       */ 
    
      await pipeline(ctx.req, ws);
    
      ctx.status = 200;
      ctx.response.body = {
        length: parseInt(length)
      };
    });
    
    module.exports = app;
  3. rtbo commented on Mar 10, 2020

    @rtbo
    Author

    Quite hard to debug, because the pipeline call succeeds. The file is actually written, but there seems to be side effect on the http socket.

  4. added
    streamIssues and PRs related to Node.js streams.
    on Mar 11, 2020
  5. addaleax commented on Mar 11, 2020

    @addaleax
    Member

    Bisecting points to 8a2b62e

    /cc @ronag

  6. ronag commented on Mar 11, 2020

    @ronag
    Member

    Yea, there is an unfortunate coupling between the req and res objects... should probably be decoupled when the req object 'finish'es, but that's semver major.

    I'll try to prepare 13.x fix later today.

  7. ronag commented on Mar 11, 2020

    @ronag
  8. szmarczak commented on Mar 11, 2020

    @szmarczak
    Member

    @ronag I thought that the IncomingMessage pipeline fix was tagged as a major change (which I strongly disagree with the choice).

  9. ronag commented on Mar 11, 2020

    @ronag
    Member

    @ronag I thought that the IncomingMessage pipeline fix was tagged as a major change (which I strongly disagree with the choice).

    Sorry, I'm confused? You link to two different PR's there.

  10. szmarczak commented on Mar 11, 2020

    @szmarczak
    Member

    This solution does the same as #32153 because you don't call the destroyer if the stream is an IncomingMessage.

  11. ronag commented on Mar 11, 2020

    @ronag
    Member

    #32153 will set socket = null which is a potentially breaking change for code that assumes that socket is not null. They do not do the same thing (even if they solve the same problem for you).

  12. ronag commented on Mar 11, 2020

    @ronag
    Member

    I was just working on backporting the other one #32212 (actually the same minute you commented). Maybe that's why I got confused by your comment.

  13. szmarczak commented on Mar 11, 2020

    @szmarczak
    Member

    Ok, I get it now, thanks. Keep up the good work!

  14. added a commit that references this issue on Jul 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    streamIssues and PRs related to Node.js streams.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions