Skip to content

Conversation

@ngraef
Copy link
Contributor

@ngraef ngraef commented Feb 16, 2022

Node 16 includes a behavior change that breaks many stream implementation functions if they use async/await syntax. Async functions implicitly return a Promise, and node's stream internals automatically call the underlying callback if the function's result is a thenable. There is some logic to skip the automatic callback if it was already called explicitly in the function; however, this can execute before other asynchronous tasks, which results in a ERR_MULTIPLE_CALLBACK error when the explicit callback is invoked. For example, the callback in this drain event handler will often trigger the error:

async transform(chunk, encoding, cb) {
  if (!forkStream.write(chunk)) {
    forkStream.once('drain', () => {
      cb(null, chunk);
    });
  } else {
    cb(null, chunk);
  }
}

This new behavior for thenables is undocumented and has already been deprecated. Unfortunately, we need to live with it for now. The most straightforward solution is to use a Promise chain instead of async/await in NodeClam passthrough()'s Transform implementation.

This PR also makes a change to use stream.Readable in jsdoc instead of ReadableStream from the experimental Web Streams API. (Complementary types change in DefinitelyTyped/DefinitelyTyped#58698.)

Fixes #82

Refs nodejs/node#39535, nodejs/node#40773

@kylefarris
Copy link
Owner

Thanks for the PR! I'm reviewing and testing it now.

@kylefarris
Copy link
Owner

Hey man, very thorough explanation. You had to dive deep into this issue to understand it and I really appreciate the help! All the tests have passed--everything looks good. I'm going to merge this as soon as I add you as a contributor.

@kylefarris kylefarris self-requested a review February 17, 2022 16:34
@kylefarris kylefarris merged commit 1114515 into kylefarris:master Feb 17, 2022
@kylefarris
Copy link
Owner

New version: 2.0.2 has been pushed to NPM.

@ngraef
Copy link
Contributor Author

ngraef commented Feb 17, 2022

Thanks for the quick review and release!

@ngraef ngraef deleted the fix/node-16-passthrough branch February 17, 2022 23:37
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.

node 16 and aws-sdk

2 participants