nodejs / nodejs/node

Stream.pipeline always throw [ERR_STREAM_PREMATURE_CLOSE]

Open
#36,760 3 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stream
Dominant language
JavaScript
Stars
122k
Forks
37.3k
Avg merge
4d 2h
Merged PRs (30d)
283

Description

Hello.
Stream.pipeline throws error [ERR_STREAM_PREMATURE_CLOSE]: Closed prematurely, but the stream throw error new Error('destroy').

In a real application, a large file is uploaded, sometimes the server timed out the connection, and when using stream.pipeline it is not clear what error occurred, ERR_STREAM_PREMATURE_CLOSE is always returned.

Version: 15.5.0
Platform: Ubuntu 20.10 x64

/* eslint-disable */
import http from 'https';
import { createWriteStream } from 'fs';
import { pipeline } from 'stream/promises';

const httpRequest = url => new Promise(resolve => http.get(url, res => resolve(res)));

const FILENAME_TMP = './1.tmp'
const linkFile = 'https://nodejs.org/dist/v15.5.0/node-v15.5.0-linux-x64.tar.xz';

const downloadStream = await httpRequest(linkFile, { responseType: 'stream' });

// view error
downloadStream.on('error', err => console.log('onError', err));

// break download
setTimeout(() => downloadStream.destroy(new Error('destroy')), 1000);

try {
  await pipeline(
    downloadStream,
    createWriteStream(FILENAME_TMP)
  );

  console.log('Done');
} catch (err) {
  console.log('onPipeline', err);
}
onError Error: destroy
    at Timeout._onTimeout (file:///home/user/Project/parser/3.js:17:41)
    at listOnTimeout (node:internal/timers:556:17)
    at processTimers (node:internal/timers:499:7)
onPipeline Error [ERR_STREAM_PREMATURE_CLOSE]: Premature close
    at new NodeError (node:internal/errors:278:15)
    at IncomingMessage.onclose (node:internal/streams/end-of-stream:117:38)
    at IncomingMessage.emit (node:events:376:20)
    at IncomingMessage._destroy (node:_http_incoming:169:10)
    at _destroy (node:internal/streams/destroy:67:23)
    at IncomingMessage.destroy (node:internal/streams/destroy:59:5)
    at Timeout._onTimeout (file:///home/user/Project/parser/3.js:17:33)
    at listOnTimeout (node:internal/timers:556:17)
    at processTimers (node:internal/timers:499:7) {
  code: 'ERR_STREAM_PREMATURE_CLOSE'
}

Thank's

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Use the supplied Node.js 15.5.0 reproduction as the starting point; inspect stream/promises and the end-of-stream and destroy paths shown in the stack trace. Compare the source stream's destroy error with pipeline's ERR_STREAM_PREMATURE_CLOSE result, and define done as a regression test or documented behavior that makes the intended error propagation explicit.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, nodejs
Domain
backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.