Repository navigation
node:zlib: reset a zstd stream in place, keep its dictionary and parameters #44261
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
robobun
wants to merge
4
commits into
main
Choose a base branch
from
robobun/55a00183/zstd-reset-keeps-dictionary-and-params
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
01ebf56
node:zlib: zstd reset() keeps the dictionary and the parameters
robobun ce71f71
test: a Node older than v26.10.0 skips the zstd reset tests
robobun 1b18197
node:zlib: shorten the comments in zstd reset()
robobun 6bf1d1a
test: the zstd reset tests do not depend on the scheduler or on an ol…
robobun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| Clear jobReady when the job table is erased. | ||
|
|
||
| With nbWorkers >= 1, ZSTDMT_createCompressionJob() sets mtctx->jobReady when | ||
| it has prepared a job and no worker is free to take it. | ||
| ZSTDMT_releaseAllJobResources() erases every job description, the prepared one | ||
| too, and leaves the flag set. ZSTDMT_initCStream_internal() does not clear it | ||
| either. | ||
|
|
||
| The next frame on that context then skips the preparation and posts the | ||
| erased description. The worker calls ZSTDMT_getCCtx(NULL) and the process gets | ||
| SIGSEGV (zstdmt_compress.c:697). | ||
|
|
||
| The caller does not have to reset anything to get there. zstd erases the table | ||
| and starts a new session by itself when a job fails, and when | ||
| ZSTDMT_compressStream_generic() returns stage_wrong. A frame on the same | ||
| context after one of those, while a job waited for a worker, crashes. | ||
|
|
||
| ZSTD_CCtx_reset(cctx, ZSTD_reset_session_only) in an open frame is one more | ||
| way in. reset() of a node:zlib zstd stream makes that call, and Node v26.10.0 | ||
| exits with SIGSEGV there. | ||
|
|
||
| facebook/zstd has the same code on its dev branch, and no issue there reports | ||
| it (checked 2026-09-29). | ||
|
|
||
| --- a/lib/compress/zstdmt_compress.c | ||
| +++ b/lib/compress/zstdmt_compress.c | ||
| @@ -1023,6 +1023,8 @@ | ||
| } | ||
| mtctx->inBuff.buffer = g_nullBuffer; | ||
| mtctx->inBuff.filled = 0; | ||
| + /* Bun: the loop above erased the job that jobReady refers to. */ | ||
| + mtctx->jobReady = 0; | ||
| mtctx->allJobsCompleted = 1; | ||
| } | ||
|
|
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| 'use strict'; | ||
|
|
||
| require('../common'); | ||
| const assert = require('assert'); | ||
| const { finished } = require('stream/promises'); | ||
| const test = require('node:test'); | ||
| const zlib = require('zlib'); | ||
|
|
||
| const dictionary = Buffer.from( | ||
| 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. ' + | ||
| 'Sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.', | ||
| ); | ||
| const input = Buffer.from( | ||
| 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. '.repeat(100), | ||
| ); | ||
|
|
||
| async function collect(stream, ...data) { | ||
| const chunks = []; | ||
| stream.on('data', (chunk) => chunks.push(chunk)); | ||
| for (let i = 0; i < data.length - 1; i++) { | ||
| stream.write(data[i]); | ||
| } | ||
| stream.end(data[data.length - 1]); | ||
| await finished(stream); | ||
| return Buffer.concat(chunks); | ||
| } | ||
|
|
||
| test('ZstdCompress reset preserves its initial options', async () => { | ||
| const options = { | ||
| dictionary, | ||
| pledgedSrcSize: input.length, | ||
| params: { | ||
| [zlib.constants.ZSTD_c_compressionLevel]: 19, | ||
| [zlib.constants.ZSTD_c_checksumFlag]: 1, | ||
| }, | ||
| }; | ||
| const expected = await collect(zlib.createZstdCompress(options), input); | ||
| const reset = zlib.createZstdCompress(options); | ||
| reset.reset(); | ||
|
|
||
| assert.deepStrictEqual(await collect(reset, input), expected); | ||
| }); | ||
|
|
||
| test('ZstdDecompress reset preserves its dictionary', async () => { | ||
| const compressed = zlib.zstdCompressSync(input, { dictionary }); | ||
| const decompress = zlib.createZstdDecompress({ dictionary }); | ||
| decompress.reset(); | ||
|
|
||
| assert.deepStrictEqual(await collect(decompress, compressed), input); | ||
| }); | ||
|
|
||
| test('ZstdDecompress reset preserves its parameters', async () => { | ||
| const compressed = await collect(zlib.createZstdCompress({ | ||
| params: { | ||
| [zlib.constants.ZSTD_c_windowLog]: 11, | ||
| }, | ||
| }), Buffer.alloc(2048), Buffer.alloc(2048)); | ||
| const decompress = zlib.createZstdDecompress({ | ||
| params: { | ||
| [zlib.constants.ZSTD_d_windowLogMax]: 10, | ||
| }, | ||
| }); | ||
| decompress.reset(); | ||
|
|
||
| await assert.rejects(collect(decompress, compressed), { | ||
| code: 'ZSTD_error_frameParameter_windowTooLarge', | ||
| }); | ||
| }); |
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 nit (optional): Maintainers get a vendored zstd bug-fix patch with no upstream issue to track and no self-obsoleting check, so it can outlive the upstream fix silently. scripts/build/deps/zstd.ts:53 registers mt-clear-job-ready.patch with only a comment saying "Not reported upstream yet", and nothing is added to scripts/build/workarounds.ts. Fix: file the upstream facebook/zstd issue (the PR already has a standalone C repro) and link it from the patch header and this comment, and add a workarounds.ts entry whose expectedToBeFixed trips when ZSTD_COMMIT moves past the pinned commit, with a cleanup string naming the patch to drop.
Why this was flagged
The diff adds patches/zstd/mt-clear-job-ready.patch and lists it in the
patchesarray at scripts/build/deps/zstd.ts:49-53; the patch header (patches/zstd/mt-clear-job-ready.patch:22-23) says no upstream issue exists and the zstd.ts comment says "Not reported upstream yet". scripts/build/CLAUDE.md "Adding a workaround" says every temporary fix waiting on an upstream release registers an entry in scripts/build/workarounds.ts with an expectedToBeFixed predicate, and workarounds.ts:9-11 explicitly lists "vendored dep bump" as such a case; the registry currently has only two entries (workarounds.ts:67-119), none for this patch. The consequence is operational for maintainers: when ZSTD_COMMIT is bumped later, the one-line hunk will still apply cleanly on top of an upstream fix (or a refactor that moved the bug), so nothing tells the developer to re-evaluate or drop the patch, and there is no upstream reference to check against. On the base branch this patch and this class of tracking gap do not exist.Verification: nit. Triggering condition: a future zstd bump where upstream fixes jobReady differently (so the Bun hunk still applies) — nothing then tells anyone the patch is obsolete. Verified facts: (1)
scripts/build/deps/zstd.ts:49-53(diff) adds"patches/zstd/mt-clear-job-ready.patch"with the comment "... Not reported upstream yet."; (2) the patch header…There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No code change for this one.
The upstream issue. There is none yet, and I did not open one on facebook/zstd. The Notes in the description have what a report needs: a C program with no bun in it, its result on v1.5.7 and on
devat 01b7154f (SIGSEGV in 20 of 20 runs), and its result with the one line of the patch (0 of 20). When an issue exists, its link belongs in the header of the patch and in the comment here.The
workarounds.tsentry. None of the other 30 patches inpatches/has one. The two entries there are for the toolchain and for thelibccrate. A check onZSTD_COMMITstops every zstd update at configure, also when upstream has no fix. The test in this PR covers the other direction: without the patch, the handle case ofzlib-zstd-reset.test.tscrashes inZSTDMT_compressionJobwhile the bug is there. If a maintainer wants the entry, I will add it.