Repository navigation
Conversation
`gc_mark_stack` took the root count of a GC frame as a `uint32_t`, so an encoded count (`n << 2`) of 2^32 or more wrapped and only the first `n mod 2^30` roots of the frame were marked. `jl_gc_run_finalizers_in_list` roots the whole list of pending finalizers (two entries each) with a single frame. With 2^29 or more pending finalizers, a GC that ran while they were still being processed therefore freed most of the pending objects, and their finalizers then ran on freed or reused memory (segfaults in `run_finalizer`, e.g., while dispatching on a garbage type tag, or in `__gmpz_clear` freeing a garbage pointer). Keep the root count at full width. The asserts that guarded the narrowing were compiled out in release builds.
charnik
commented
Oct 23, 2026
Comment on lines
+2502
to
+2505
| // `nroots` must not be narrowed: `jl_gc_run_finalizers_in_list` roots the | ||
| // whole list of pending finalizers (two entries each) with a single frame, | ||
| // which can hold more than 2^30 roots, i.e. an encoded `nroots` that does not | ||
| // fit in 32 bits. |
Author
There was a problem hiding this comment.
Better remove these extra comments.
With the full root count, `gc_mark_stack` pushes every root of a frame onto the mark queue before draining it. For the frame that roots a list of 2^29 or more pending finalizers, the queue has to grow past 2^30 entries, and the doubling of its `int32_t` capacity overflows, so `malloc_s` aborts with "Cannot allocate memory". Mark direct frames with more than `GC_CHUNK_BATCH_SIZE` roots with `gc_mark_finlist_`, which marks the global finalizer lists in batches and pushes the remainder as a chunk, so that the mark queue stays bounded and other GC threads can steal the work.
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.
PR Description
Fixes engine segfaults that occur while the GC runs finalizers (
run_finalizercrashing in dispatch on a garbage type tag, in__gmpz_clear, or inuvfinalize), seen on ~250 engines over the past year, mostly during TPC-DS benchmarks that divideFixedDecimal{Int128}values.The GC marks a GC frame's roots using a 32-bit root count. The list of pending finalizers is rooted with a single frame, so with 2^29 or more pending finalizers the count wrapped, a GC that ran while those finalizers were still being processed freed most of the pending objects, and their finalizers then ran on freed memory. Each
BigIntregisters a finalizer, so many threads doingFixedDecimal{Int128}division (which goes throughBigInton Julia 1.10) easily exceed that number between two collections.This PR keeps the root count at full width and marks frames with more than
GC_CHUNK_BATCH_SIZEroots in chunks, like the global finalizer lists. Pushing all roots of such a frame onto the mark queue at once would otherwise overflow the queue's 32-bit capacity and abort with "Cannot allocate memory".Before this change, a single-threaded reproducer (see below) that makes N finalizers pending and triggers a GC from inside the first one crashes for N = 2^29 + 2^20 and is clean for N = 2^29 - 2^20. Upstream
masterhas the same code.Reproducer
The reproducer can be found at repro-finalizer-overflow.jl.zip. It needs a lot of memory: peak resident memory is about 72 bytes per finalizer plus ~250 MB, i.e. about 36 GiB for the default N (measured at smaller N and extrapolated), so run it on a machine with plenty of free RAM (e.g., 64 GiB or more). Expect the following output from a good run:
Checklist
Requirements for merging:
port-to-*labels that don't apply.