zlib: createZstdCompress appends an empty frame when end() is called with writes still queued
还没有人认领这个 Issue。
评估
- 难度
- 4/5
- 预计耗时
- 3-5 天
- 新手友好度
- 68/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- cpp, javascript, node.js
- 领域
- backend, performance
调研方向
先从提供的 JavaScript 复现开始,然后阅读 lib/zlib.js,尤其是 ZlibBase#_transform 和 _flush。检查 src/node_zlib.cc 中的 ZstdCompressContext::DoThreadPoolWork,并将排队写入路径与 gzip 和 brotli 的行为进行比较。当 createZstdCompress 对一次或多次写入都输出一个字节完全相同的单一帧,且末尾没有额外的空帧时,即表示完成。
由索引模型根据 Issue 内容生成。
描述
Version
v26.9.0 (also v24.15.0)
Platform
Darwin 25.6.0 arm64 (also seen on Linux x64)
Subsystem
zlib
What steps will reproduce the bug?
'use strict';
const zlib = require('node:zlib');
function compress(create, writes) {
return new Promise((resolve, reject) => {
const stream = create();
const chunks = [];
stream.on('data', (chunk) => chunks.push(chunk));
stream.on('end', () => resolve(Buffer.concat(chunks)));
stream.on('error', reject);
for (const w of writes) stream.write(w);
stream.end();
});
}
(async () => {
const oneWrite = await compress(zlib.createZstdCompress, ['hello world']);
const twoWrites = await compress(zlib.createZstdCompress, ['hello ', 'world']);
console.log('one write: ', oneWrite.toString('hex'));
console.log('two writes:', twoWrites.toString('hex'));
console.log('extra bytes:', twoWrites.subarray(oneWrite.length).toString('hex'));
// Same input, queued writes: gzip and brotli are unaffected.
for (const [name, create, decompress] of [
['gzip', zlib.createGzip, zlib.gunzipSync],
['brotli', zlib.createBrotliCompress, zlib.brotliDecompressSync],
]) {
const a = await compress(create, ['hello world']);
const b = await compress(create, ['hello ', 'world']);
console.log(name, 'lengths', a.length, b.length, decompress(b).toString());
}
})();
How often does it reproduce? Is there a required condition?
Every time end() is called while at least one write is still queued in the compressor. In practice that is most pipelines whose source ends quickly, e.g. pipeline(tar.create(...), zlib.createZstdCompress(), fs.createWriteStream(...)): every archive we packed that way had the extra frame.
What is the expected behavior? Why is that the expected behavior?
One zstd frame, the same bytes whether the input arrived in one write or several:
one write: 28b52ffd005859000068656c6c6f20776f726c64
two writes: 28b52ffd005859000068656c6c6f20776f726c64
That's what gzip and brotli do. How the input was split into writes shouldn't change the compressed output.
What do you see instead?
one write: 28b52ffd005859000068656c6c6f20776f726c64
two writes: 28b52ffd005859000068656c6c6f20776f726c6428b52ffd2000010000
extra bytes: 28b52ffd2000010000
gzip lengths 31 31 hello world
brotli lengths 15 15 hello world
A second, empty zstd frame (28b52ffd 20 00 01 00 00: magic, single-segment descriptor with content size 0, one empty raw last block) is appended.
Additional information
I think the cause is in lib/zlib.js:
- In
ZlibBase#_transform, whenthis.writableEnded && this.writableLength === chunk.byteLength, the last queued chunk is processed with_finishFlushFlag, which ends the frame. ZlibBase#_flushthen calls_transformagain with an empty buffer.writableEndedis still true andwritableLengthis 0, so that call gets the finish flag too.
For deflate and brotli a second finish on a finished stream emits nothing. For zstd, ZSTD_compressStream2(..., ZSTD_e_end) on a context whose frame has just completed starts and completes a new frame. ZstdCompressContext::DoThreadPoolWork in src/node_zlib.cc doesn't guard against that. When end() is called with nothing queued, the frame is only finished once, so the output is a single frame.
The extra frame is valid zstd, but it has real consequences:
- The output depends on stream timing, not content. We hash the archive for deduplication, so identical inputs can hash differently.
- In released versions (including v26.9.0),
createZstdDecompressthrowsUnknown frame descriptorwhen a read chunk boundary falls inside those 9 bytes. Withfs.createReadStream's default 64 KiB chunks, that's about 1 archive in 8,200, and it fails the same way every time. We hit this in production on an archive thatzstd -tandzstdDecompressSyncboth accept. I believe #65865 fixes the decoding side onmain, but the compressor still writes the extra frame.
Our workaround is to strip a trailing 28b52ffd2000010000 after compressing.
- 主要语言
- JavaScript
- 星标
- 122k
- 派生
- 37.4k
- 平均合并
- 4 天 4 小时
- 30 天内合并 PR
- 276
贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
nodejs/node 的其他 Issue
-
doc
难度 2/5 1-3 小时 新手友好度 65/100
-
build
难度 1/5 1 小时以内 新手友好度 88/100
-
难度 2/5 1-3 小时 新手友好度 84/100
-
难度 1/5 1 小时以内 新手友好度 90/100
-
feature request
难度 2/5 1-3 小时 新手友好度 68/100
相似的 Issue
-
bug customer-eng Durable Agents Inngest status: needs triage
难度 2/5 1-3 小时 新手友好度 82/100
-
optimization optimization:agents-md-curator
难度 2/5 1-3 小时 新手友好度 75/100
githubnext/gh-aw-cao#13475 ·
-
[BUG]: "Clear All" in Settings doesn't clear the saved analysis, old data comes back after reload 未关闭bug
难度 2/5 1-3 小时 新手友好度 88/100
AOSSIE-Org/OrgExplorer#253 · 1 条评论 ·
-
难度 2/5 1-3 小时 新手友好度 88/100
oxc-project/oxc#26944 ·
-
ai-observability bug team/ai-observability
难度 2/5 1-3 小时 新手友好度 78/100