Repository navigation
Fix three reference leaks in the C API layer - #20
Merged
Merged
Conversation
The temporary memoryview passed to GraalPyPrivate_Unicode_Decode is a borrowed argument and was never released, so each call leaked the memoryview and its native stub. Extensions that decode many small strings this way, such as pandas' CSV parser and lxml, kept growing the heap until the process ended.
A Python subclass of a native type inherits the native base's tp_dealloc and does not get subtype_dealloc, and PyObject_ClearManagedDict is a no-op on GraalPy. The instance __dict__ kept in the pre-header was therefore never released: its reference count stayed above MANAGED_REFCNT, its handle table reference stayed strong, and the dict and everything reachable from it were never collected. Release it in PyObject_GC_Del, the last point where that is possible. Adds regression tests to test_gc.py.
On GraalPy get_unicode_key returns a new reference, because there is no key cache, and PyDict_SetItem takes its own references. The patched reverse_pydict_incref released only the value, so every key created by orjson.loads kept a reference count above MANAGED_REFCNT and was never collected. Applies to the patches for orjson 3.12.0 and 3.13.0.
The previous tests asserted only when RELY_ON_GC is set, so they passed with the leak present. One test now allocates an instance of a Python subclass of a native type in C, gives it a dict, drops the only reference and checks that the deallocation released exactly one reference to the dict. Also corrects the comment in PyObject_GC_Del: a dict subclass with native backing can run its tp_dealloc there.
GNU patch 2.8 rejected the changed hunk for src/ffi/pydictref.rs: with nine lines of leading and three of trailing context it takes the hunk to be anchored at the end of the file. BSD patch and git apply accepted it. The hunks are regenerated with diff -u; the patched sources are unchanged.
Merging this PR will improve performance by 44.38%1 warning
Performance Changes
Tip Curious why performance improved? Comment Comparing Footnotes |
Ivo Horák (horakivo)
changed the base branch from
ci/codspeed-packages
to
develop
November 27, 2026 23:33
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.
Fixes three reference leaks in GraalPy's C API layer. Each one is a missing release that leaves an object's reference count above
MANAGED_REFCNT, so its handle table reference stays strong and the object, with everything it references, is kept until the process ends. No collection can free it.PyUnicode_Decodeunicodeobject.cPyObject_GC_Delgcmodule.c__dict__of a Python subclass of a native typeorjson-3.12.0.patch,orjson-3.13.0.patchorjson.loadsAll three are present in upstream
masteras well.Effect on the heap
Heap after a full collection, first and last of 30 iterations of the package benchmarks, with a collection after each iteration. Measured on macOS arm64 with a native standalone:
Without a collection between iterations, which is how the benchmarks run, orjson-roundtrip still grows (97 → 1154 MB instead of 133 → 3866 MB). That remainder is not a leak: the items of a list created by C are released one nesting level per full collection. It is not addressed here.
Tests
test_gc.pyfor the instance dict. It checks reference counts inside a single C call, so it does not depend on a collection. It fails without the fix ("the instance dict lost 0 references on dealloc, expected 1") and passes with it; verified on macOS builds.Measured on the benchmark runner
One run before and one after, package benchmarks, median of the measured iterations and peak memory:
CodSpeed compares the fastest iteration of each benchmark and flags pandas-csv, pandas-str and pydantic-json as slower by that measure. Before this change, an iteration without a collection skipped the release work entirely. A pure Python benchmark that this change cannot affect (
meso/pidigits) moved by 18% between the two builds as well, so differences of that size between single runs are not reliable.Reviewed with sol-loop: 2 rounds with gpt-6.2-sol. Sol verdict: approved
🤖 Generated with Claude Code