nodejs / nodejs/node

Stream.pipeline always throw [ERR_STREAM_PREMATURE_CLOSE]

未关闭
#36,760 3 条评论 4 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

stream
主要语言
JavaScript
星标
122k
派生
37.3k
平均合并
4 天 2 小时
30 天内合并 PR
283

描述

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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

以提供的 Node.js 15.5.0 复现为起点;检查 stream/promises 以及 stack trace 中显示的 end-of-stream 和 destroy 路径。比较源 stream 的 destroy 错误与 pipeline 的 ERR_STREAM_PREMATURE_CLOSE 结果,并将 done 定义为回归测试或有文档记录的行为,以明确预期的错误传播。

由索引模型根据 Issue 内容生成。

评估

技术栈
javascript, nodejs
领域
backend
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
冷清
描述清晰度
基本清楚
新手友好度
42/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。