Skip to content

gh-160535: Keep the ZstdDict alive until the zstd context is freed - #160536

Merged
gpshead merged 2 commits into
python:mainfrom
gpshead:scanfix-zstd-ct3p1csj
Oct 25, 2026
Merged

gpshead merged 2 commits into
python:mainfrom
gpshead:scanfix-zstd-ct3p1csj

Conversation

@gpshead

@gpshead gpshead commented Oct 23, 2026 •

Copy link
Copy Markdown
Member

ZstdCompressor and ZstdDecompressor no longer have a tp_clear, and ZstdDict no longer exposes its cache of digested dictionaries to the garbage collector, so the dictionary and its ZSTD_CDict instances are only released in the deallocators, after ZSTD_freeCCtx()/ZSTD_freeDCtx() has stopped the worker threads. None of these objects can close a reference cycle on its own (the only route back is through a mutable object reachable from the zstd_dict tuple, which has its own tp_clear), so tp_traverse alone is enough. The decompressor has no worker threads and is changed only to keep the same ordering rule in both types. The new test runs in a subprocess because detecting the freed read depends on the allocator; it fails on Linux/glibc without the fix and may pass vacuously elsewhere.

Fixes #160535

ANT-2026-CT3P1CSJ

The garbage collector could release a ZstdCompressor's dictionary, or
the digested dictionaries cached by the ZstdDict, while worker threads
of the compression context were still reading them.  They are now only
released on deallocation, after the context has been freed.

ANT-2026-CT3P1CSJ
@gpshead
gpshead marked this pull request as ready for review October 25, 2026 05:29
@gpshead
gpshead merged commit b949033 into python:main Oct 25, 2026
58 checks passed
@github-project-automation github-project-automation Bot moved this from Todo to Done in Sprint Oct 25, 2026
@miss-islington-app

Copy link
Copy Markdown

Thanks @gpshead for the PR 🌮🎉.. I'm working now to backport this PR to: 3.14, 3.15.
🐍🍒⛏🤖

@bedevere-app

bedevere-app Bot commented Oct 25, 2026

Copy link
Copy Markdown

GH-160635 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 bugs and security fixes label Oct 25, 2026
@bedevere-app

bedevere-app Bot commented Oct 25, 2026

Copy link
Copy Markdown

GH-160636 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.14 bugs and security fixes label Oct 25, 2026
gpshead added a commit that referenced this pull request Oct 25, 2026
…reed (GH-160536) (#160636)

gh-160535: Keep the ZstdDict alive until the zstd context is freed (GH-160536)

The garbage collector could release a ZstdCompressor's dictionary, or
the digested dictionaries cached by the ZstdDict, while worker threads
of the compression context were still reading them.  They are now only
released on deallocation, after the context has been freed.

ANT-2026-CT3P1CSJ

+ Visit the type in the compressor and decompressor traverse; move NEWS to Library
(cherry picked from commit b949033)

Co-authored-by: Gregory P. Smith <68491+gpshead@users.noreply.github.com>
gpshead added a commit that referenced this pull request Oct 25, 2026
…reed (GH-160536) (#160635)

gh-160535: Keep the ZstdDict alive until the zstd context is freed (GH-160536)

The garbage collector could release a ZstdCompressor's dictionary, or
the digested dictionaries cached by the ZstdDict, while worker threads
of the compression context were still reading them.  They are now only
released on deallocation, after the context has been freed.

ANT-2026-CT3P1CSJ

+ Visit the type in the compressor and decompressor traverse; move NEWS to Library
(cherry picked from commit b949033)

Co-authored-by: Gregory P. Smith <68491+gpshead@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

Crash when the GC collects a multi-threaded ZstdCompressor together with its ZstdDict

2 participants