Skip to content

Commit af160e9

Browse files
committed
zlib: fix zstd compression with nbWorkers
With ZSTD_c_nbWorkers >= 1, ZSTD_compressStream2() with ZSTD_e_continue returns as soon as it has made some progress, which can leave input unconsumed while output space remains. lib/zlib.js reads that state as the end of the stream and drops the rest of the input, so zlib.zstdCompress() returned an empty buffer and streams ended early or failed with ERR_STREAM_PUSH_AFTER_EOF. Keep calling ZSTD_compressStream2() until the input is consumed or the output buffer is full. Fixes: #67988 Refs: #59899 Signed-off-by: Nick Robertson <9286660+narobertson42@users.noreply.github.com>
1 parent d160211 commit af160e9

2 files changed

Lines changed: 63 additions & 2 deletions

File tree

‎src/node_zlib.cc‎

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1893,8 +1893,19 @@ void ZstdCompressContext::DoThreadPoolWork() {
18931893
}
18941894
// Zstd overrides a configured pledge when the first call uses ZSTD_e_end.
18951895
size_t const input_pos = input_.pos;
1896-
size_t const remaining =
1897-
ZSTD_compressStream2(cctx_.get(), &output_, &input_, flush_);
1896+
// With ZSTD_c_nbWorkers >= 1, ZSTD_e_continue can return with input left and
1897+
// output space free, which lib/zlib.js treats as the end of the stream.
1898+
size_t remaining;
1899+
while (true) {
1900+
const size_t in_pos = input_.pos;
1901+
const size_t out_pos = output_.pos;
1902+
remaining = ZSTD_compressStream2(cctx_.get(), &output_, &input_, flush_);
1903+
if (flush_ != ZSTD_e_continue || ZSTD_isError(remaining) ||
1904+
input_.pos == input_.size || output_.pos == output_.size ||
1905+
(input_.pos == in_pos && output_.pos == out_pos)) {
1906+
break;
1907+
}
1908+
}
18981909
if (consumed_src_size_.has_value()) {
18991910
*consumed_src_size_ += input_.pos - input_pos;
19001911
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
'use strict';
2+
3+
// Zstd compression with ZSTD_c_nbWorkers >= 1 must not drop input.
4+
5+
const common = require('../common');
6+
const assert = require('assert');
7+
const zlib = require('zlib');
8+
const { Readable } = require('stream');
9+
const { pipeline } = require('stream/promises');
10+
11+
const { ZSTD_c_nbWorkers, ZSTD_c_jobSize } = zlib.constants;
12+
13+
try {
14+
zlib.zstdCompressSync('', { params: { [ZSTD_c_nbWorkers]: 1 } });
15+
} catch (err) {
16+
// The bundled zstd is always built with multithreading; a shared one may not.
17+
if (!process.config.variables.node_shared_zstd) throw err;
18+
common.skip('libzstd built without multithreading support');
19+
}
20+
21+
// Smallest allowed job size; input longer than one job triggers the bug.
22+
const jobSize = 512 * 1024;
23+
const params = { [ZSTD_c_nbWorkers]: 2, [ZSTD_c_jobSize]: jobSize };
24+
const input = Buffer.alloc(3 * jobSize);
25+
for (let i = 0; i < input.length; i++) input[i] = (i * 7) ^ (i >> 10);
26+
27+
function assertRoundTrips(compressed) {
28+
const decompressed = zlib.zstdDecompressSync(compressed);
29+
assert.strictEqual(decompressed.length, input.length);
30+
assert(decompressed.equals(input));
31+
}
32+
33+
zlib.zstdCompress(input, { params }, common.mustSucceed(assertRoundTrips));
34+
35+
(async () => {
36+
const writeSize = jobSize / 2 + 1000;
37+
const chunks = [];
38+
for (let off = 0; off < input.length; off += writeSize) {
39+
chunks.push(input.subarray(off, off + writeSize));
40+
}
41+
const output = [];
42+
await pipeline(
43+
Readable.from(chunks),
44+
zlib.createZstdCompress({ params }),
45+
async function(source) {
46+
for await (const chunk of source) output.push(chunk);
47+
},
48+
);
49+
assertRoundTrips(Buffer.concat(output));
50+
})().then(common.mustCall());

0 commit comments

Comments
 (0)