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

ERR_STREAM_PREMATURE_CLOSE on early destroy() #2041

Closed
2 tasks done
longnguyen2004 opened this issue May 14, 2022 · 3 comments
Closed
2 tasks done

ERR_STREAM_PREMATURE_CLOSE on early destroy() #2041

longnguyen2004 opened this issue May 14, 2022 · 3 comments

Comments

@longnguyen2004
Copy link

longnguyen2004 commented May 14, 2022

Describe the bug

  • Node.js version: v18.1.0
  • OS & version: Windows 10 21H1, got v12.0.4

Actual behavior

When a Got stream is destroyed before it's ended (which can happen with a read timeout), an ERR_STREAM_PREMATURE_CLOSE error is thrown.
Digging through the git history, it seems to have been fixed in a previous version, but the fix was lost at some point.

Expected behavior

No error is thrown

Code to reproduce

import got from "got";
import devnull from "dev-null";
import { pipeline } from "stream/promises";

const gotStream = got("https://google.com", { isStream: true });
const nullStream = devnull();
gotStream.on("response", () => gotStream.destroy());
pipeline(gotStream, nullStream);

Checklist

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

Hello, I am also encountering this ERR_STREAM_PREMATURE_CLOSE error on early destroy() with Node.js version v18.12.0 and got version 11.8.6 when used as a dependency in a TypeScript project (typescript version 4.4.2). I do not experience this error with version 11.8.5 but I run into a separate onCancel error that likely gets thrown before this error would be thrown, so I am unsure if this was introduced with the recent release. Please let me know if you would like to be provided with additional code to reproduce and/or a stack trace.

@longnguyen2004
Copy link
Author

@szmarczak
Copy link
Collaborator

szmarczak commented Jan 22, 2023

Digging through the git history, it seems to have been fixed in a previous version, but the fix was lost at some point.

Those errors were unhandled errors.

The error is correct in this case. https://nodejs.org/api/stream.html#streamfinishedstream-options-callback

Emitting 'close' before 'end' on a Readable stream will cause an ERR_STREAM_PREMATURE_CLOSE error.

This is exactly what happens here. pipeline uses finished under the hood which throws ERR_STREAM_PREMATURE_CLOSE since the stream got closed before fully read.

When a Got stream is destroyed before it's ended (which can happen with a read timeout)

This is a separate case where TimeoutError is thrown.

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

No branches or pull requests

3 participants