Repository navigation
compress: fix ZSTD_CCtx_refThreadPool() rebinding and shared pool resizing - #4917
Merged
Merged
Conversation
ZSTD_CCtx_refThreadPool() only changed cctx->pool. Once created, the MT context kept the pool it was created with, so the next frames still ran on the previous pool, including after ZSTD_CCtx_refThreadPool(cctx, NULL). If the caller then freed the previous pool, the next frame used freed memory. ZSTD_CCtx_refThreadPool() now frees the MT context when the pool changes, and the next frame creates one with the new pool. The pool can only change between frames, and ZSTDMT_freeCCtx() waits for the jobs of an abandoned frame.
When ZSTD_c_nbWorkers changed between two frames, ZSTDMT_resize() called POOL_resize() on the context's pool, even on a pool given with ZSTD_CCtx_refThreadPool(). One context could then change the number of threads of a pool shared with other contexts, although such a pool is meant to limit them. ZSTDMT_createCCtx_advanced() already leaves a provided pool unchanged. ZSTDMT_resize() now does the same.
Cyan4973
force-pushed
the
thread-pool-rebind
branch
from
November 10, 2026 20:59
91d5396 to
31db1af
Compare
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.
Two fixes for a thread pool given with
ZSTD_CCtx_refThreadPool().ZSTD_CCtx_refThreadPool()only changedcctx->pool. Once created, the multi-threaded context kept the pool it was created with, so the next frames still ran on the previous pool, including afterZSTD_CCtx_refThreadPool(cctx, NULL). If the application then freed the previous pool, the next frame used freed memory.ZSTD_CCtx_refThreadPool()now frees the multi-threaded context when the pool changes, and the next frame creates one with the new pool. This relies on #4871: the pool can only change between frames, andZSTDMT_freeCCtx()now waits for the jobs of an abandoned frame.When
ZSTD_c_nbWorkerschanged between two frames,ZSTDMT_resize()calledPOOL_resize()even on a pool given withZSTD_CCtx_refThreadPool(). One context could then change the number of threads of a pool shared with other contexts, although such a pool is meant to limit them. A provided pool now keeps its size, as it already did when the multi-threaded context is created.zstd.hnow states both behaviors.Tests: a new thread pool test in
fuzzer.cmoves a context from a pool to another, then to an internal pool, and frees the previous pool each time. It also changesZSTD_c_nbWorkerson a shared pool. Without the first fix, AddressSanitizer reports a heap-use-after-free inPOOL_tryAdd(). Without the second, the size of the shared pool changes.