multiparty: `for await ... of ...` never end after upgrade NodeJS to 12.18.3

Duplicate of https://github.com/nodejs/node/issues/35190

  • Version: ** 12.16.0 ** … ** 12.18.3
  • Platform: ** Windows 10 64
  • Subsystem (with and without it): ** Docker node:12.16.0-alpine ** … ** Docker node:12.18.3-alpine

What steps will reproduce the bug?

index.js

var multiparty = require('multiparty'); // 4.2.2
var http = require('http');
var util = require('util');

http.createServer(async function(req, res) {
  if (req.url === '/upload' && req.method === 'POST') {
    const stream = await getFile(req);

    try {
      console.log(1);
      for await (const data of stream) {
        console.log(2);
        throw new Error('1');
      }
      console.log(3);
    } catch(err) {
      console.log(4, err);
    }

    console.log(5);
    return;
  }

  // show a file upload form
  res.writeHead(200, { 'content-type': 'text/html' });
  res.end(
    '<form action="/upload" enctype="multipart/form-data" method="post">'+
    '<input type="text" name="title"><br>'+
    '<input type="file" name="upload" multiple="multiple"><br>'+
    '<input type="submit" value="Upload">'+
    '</form>'
  );
}).listen(8080);


function getFile(req) {
  return new Promise((resolve) => {
    const form = new multiparty.Form();

    form.on('part', (part) => {
      if (!part.filename) {
        part.resume();
        return;
      }
      resolve(part);
    });

    form.parse(req);
  });
}
  1. npm i multiparty@4.2.2
  2. node index.js
  3. Go to localhost:8080
  4. Download any file

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

Always in this case

What is the expected behavior?

v12.14.0 and v12.15.0:

Will print to terminal

1
2
4 Error: 1
5

What do you see instead?

v12.16.0 … v12.18.3 (Actual):

1
2

And looks like it will never end.

Additional information

I believe this is a Nodejs issue because the compatibility was broken and I could not found the cause of this bug in JS code (include multiparty and nodejs js modules). But anyway, take a look. Perhaps I wrong, and the issue in multiparty library.

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 18 (9 by maintainers)

Most upvoted comments

Hi @Mar-Ser I know this is an old issue, but I was able to track this down to a regression from the following commit in Node.js: https://github.com/nodejs/node/commit/369b7c235f7e6c6d8737812b4d6c8bd80b08353c . This affects the async await function on streams in general. It seems to behave differently based on internal timings of events within Node.js, which is why it sometimes works, but mostly does not work. The good news is that it seems to not affect Node.js 13+, even this it is landed there. It was backported to Node.js 12.16.0 and likely is conflicting with some other behavior that is in Node.js 12 that is not in 13. Unfortunately it does not seem there is any possible way to fix it from this module.