Skip to content

zlib: fix zstd compression with nbWorkers - #67990

Open
narobertson42 wants to merge 1 commit into
nodejs:mainfrom
narobertson42:zlib-zstd-nbworkers
Open

narobertson42 wants to merge 1 commit into
nodejs:mainfrom
narobertson42:zlib-zstd-nbworkers

Conversation

@narobertson42

Copy link
Copy Markdown

With ZSTD_c_nbWorkers >= 1, zstd compression drops input. zlib.zstdCompress() returns an empty buffer with no error, and streams end early or fail with ERR_STREAM_PUSH_AFTER_EOF. Details and repro in #67988.

With workers, ZSTD_compressStream2() with ZSTD_e_continue returns as soon as it has made some progress (zstd.h, "function is non-blocking"). Node made one call per write, and lib/zlib.js reads "input left, output space free" as the end of the stream.

This loops the call in ZstdCompressContext::DoThreadPoolWork() until the input is consumed, the output is full, zstd returns an error, or a call makes no progress. It only loops for ZSTD_e_continue: zstd already runs ZSTD_e_flush and ZSTD_e_end to completion. Without workers zstd consumes all input or fills the output in one call, so the loop runs once and output is unchanged.

I fixed it in C++ rather than lib/zlib.js because that loop is shared by every codec and its end-of-stream rule is right for the others. This keeps the change to zstd compression and covers the sync and async paths. ZstdDecompressContext::DoThreadPoolWork() already loops the same way.

Fixes: #67988
Refs: #59899

Testing, on macOS arm64:

  • New test/parallel/test-zlib-zstd-multithreaded.js fails on main and passes with the fix (20/20 runs, about 0.06 s). I checked each case separately: both the zstdCompress() case and the stream case fail without the fix.
  • tools/test.py 'parallel/test-zlib*' 'sequential/test-zlib*' 'pummel/test-zlib*' passes.
  • Full tools/test.py default: the only failures are fs.watch and watch-mode tests. The ones that still fail when run serially (test-fs-watch-recursive-sync-write, sequential/test-watch-mode) fail the same way on main without this change.
  • I also round-tripped about 1,200 combinations of worker count (0–256), job size, write size (1 byte to 8 MiB), chunk size, slow consumers, mid-stream ZSTD_e_flush/ZSTD_e_end, reset(), pledged sizes, dictionaries and destroy mid-write. With nbWorkers 0, output is byte-identical to before.
  • cpplint, eslint, make format-cpp and core-validate-commit are clean.

I used a coding agent to help investigate this and draft the patch and test. I reproduced the bug, built Node with the fix, and ran the tests above myself.

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: nodejs#67988
Refs: nodejs#59899
Signed-off-by: Nick Robertson <9286660+narobertson42@users.noreply.github.com>
@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. zlib Issues and PRs related to the zlib module and its compression dependencies. labels Dec 3, 2026
@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Welcome to Node.js, and thank you for your first contribution!

Before review, please take a moment to read:

Please make sure every commit is signed off. For a first pull request, GitHub Actions require collaborator approval and Jenkins CI must be started by a collaborator or triager, so an initial wait is normal.

@addaleax addaleax added the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Dec 3, 2026
@github-actions github-actions Bot removed the request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. label Dec 3, 2026
@nodejs-github-bot

Copy link
Copy Markdown
Collaborator

@codecov

codecov Bot commented Dec 4, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 75.00000% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.57%. Comparing base (9cf1a9c) to head (af160e9).
⚠️ Report is 22 commits behind head on main.

Files with missing lines Patch % Lines
src/node_zlib.cc 75.00% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67990      +/-   ##
==========================================
- Coverage   90.58%   90.57%   -0.02%     
==========================================
  Files         802      802              
  Lines      286955   286951       -4     
  Branches    55417    55408       -9     
==========================================
- Hits       259945   259897      -48     
- Misses      17107    17156      +49     
+ Partials     9903     9898       -5     
Files with missing lines Coverage Δ
src/node_zlib.cc 80.44% <75.00%> (-0.07%) ⬇️

... and 31 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. zlib Issues and PRs related to the zlib module and its compression dependencies.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

zlib: multiple worker zstd compression silently drops input

3 participants