Repository navigation
zlib: fix zstd compression with nbWorkers - #67990
Open
narobertson42 wants to merge 1 commit into
Open
narobertson42 wants to merge 1 commit into
narobertson42 wants to merge 1 commit into
Conversation
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>
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
approved these changes
Dec 3, 2026
Collaborator
Codecov Report❌ Patch coverage is
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
🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
With
ZSTD_c_nbWorkers >= 1, zstd compression drops input.zlib.zstdCompress()returns an empty buffer with no error, and streams end early or fail withERR_STREAM_PUSH_AFTER_EOF. Details and repro in #67988.With workers,
ZSTD_compressStream2()withZSTD_e_continuereturns 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 forZSTD_e_continue: zstd already runsZSTD_e_flushandZSTD_e_endto 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:
test/parallel/test-zlib-zstd-multithreaded.jsfails on main and passes with the fix (20/20 runs, about 0.06 s). I checked each case separately: both thezstdCompress()case and the stream case fail without the fix.tools/test.py 'parallel/test-zlib*' 'sequential/test-zlib*' 'pummel/test-zlib*'passes.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.ZSTD_e_flush/ZSTD_e_end,reset(), pledged sizes, dictionaries and destroy mid-write. WithnbWorkers0, output is byte-identical to before.make format-cppand 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.