Skip to content

zlib: prevent concurrent parameter changes - #67609

Open
ShogunPanda wants to merge 1 commit into
nodejs:mainfrom
ShogunPanda:zlib-ordered-params
Open

ShogunPanda wants to merge 1 commit into
nodejs:mainfrom
ShogunPanda:zlib-ordered-params

Conversation

@ShogunPanda

Copy link
Copy Markdown
Contributor

zlib.params() only deferred the parameter change behind a flush. When writes were queued behind that flush, the next write could be dispatched to the threadpool before the parameters were applied on the main thread, so deflateParams() and deflate() could run concurrently on the same z_stream.

Parameter changes are now applied as an ordered operation before the next queued write starts, and the native params() binding throws if a write is in progress, like reset() already does. A change that is still pending when the stream is destroyed or errors is not applied. Consecutive params() calls are compared against the last requested values, so a change back to the original values is no longer skipped.

Adds regression tests for the public API race, the native guard, a parameter change with no following write, flush/params/write ordering and destroying a stream with a pending change.

This touches lib/zlib.js and src/node_zlib.cc like #67603; I'm happy to rebase on top of it or the other way around, whichever lands first.

AI-assisted work. All changes reviewed by a human.

@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 Nov 19, 2026
@codecov

codecov Bot commented Nov 19, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.93939% with 2 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.59%. Comparing base (d9e18d5) to head (58cbb83).
⚠️ Report is 368 commits behind head on main.

Files with missing lines Patch % Lines
src/node_zlib.cc 50.00% 0 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #67609      +/-   ##
==========================================
- Coverage   90.72%   90.59%   -0.14%     
==========================================
  Files         801      802       +1     
  Lines      280809   287062    +6253     
  Branches    54212    55437    +1225     
==========================================
+ Hits       254776   260057    +5281     
- Misses      16209    17100     +891     
- Partials     9824     9905      +81     
Files with missing lines Coverage Δ
lib/zlib.js 98.73% <100.00%> (+0.31%) ⬆️
src/node_zlib.cc 80.85% <50.00%> (+0.43%) ⬆️

... and 212 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.

@ShogunPanda ShogunPanda 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
Comment thread lib/zlib.js Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the usage of bind() I think it's still a bit faster due to the non-allocation of closure.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. I restored the top-level paramsAfterFlushCallback() helper and _transform() now passes paramsAfterFlushCallback.bind(this, level, strategy, cb) to processChunk(), so there is no per-call closure anymore.

Apply `params()` changes as an ordered operation before the next queued
write starts, so `deflateParams()` can no longer run on the threadpool
concurrently with `deflate()` on the same stream, and make the native
`params()` binding throw while a write is in progress. Skip the
change if the stream was destroyed or errored in the meantime. Add
regression tests.

Assisted-By: OpenAI GPT-5.6 Sol <openai/gpt-5.6-sol>
Signed-off-by: Paolo Insogna <paolo@cowtech.it>
@ShogunPanda

Copy link
Copy Markdown
Contributor Author

I also added tests for the paths Codecov reported as uncovered: destroying the stream while the flush carrying a parameter change is still running on the threadpool (the change is not applied to the closed handle), and requesting values that are already applied or already queued (no extra write is queued and the callback is still called asynchronously).

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. request-ci Add this label to start a Jenkins CI on a PR. Only starts once the PR has an approving review. 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.

3 participants