gc: fix the store-buffering race in the parallel pool sweep handshake #65314
All Vouch review confidence checks passed
Residual risk 0.24 = Change Risk (0.45) × (1 − Review Confidence (0.47)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.45 · 274 lines diff across 3 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.02 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Shallow Review Depth
shallow_review_depth
0.344
Diff Size
diff_size
0.336
Ai Authored
ai_authored
0.050
Real PR Conversation Timeline
1 comments
"Review active on #65314"
general
weight: 0.35
Keno
gc_sweep_wait_for_all_pagesstores NULL togc_allocd_scratchand then waits forgc_n_threads_sweeping_poolsto reach zero, whilegc_sweep_pool_parallelincrements the counter and then loads the scratch pointer. This is a store-then-load (store buffering) handshake: each side stores to one variable and then loads the other, and it is only correct if at least one side is guaranteed to observe the other's store. A seq_cst store followed by an acquire load of a different variable does not give that guarantee. On AArch64 targets with RCpc support (e.g. the macOS builds) it compiles tostlrfollowed byldapr, and the load may be satisfied before the store is visible to other threads. The GC thread running the collection could then read zero while a late GC worker still read the old scratch pointer, and that worker would go on to sweep the already swept page lists. After a full sweep those pages havehas_marked == 0, so every live page was pushed to the lazily freed pool, leading to a segfault ingc_setmark_pool_in the next collection.Both sides now have an explicit
jl_fence()between their store and their load, as is done for the scheduler's sleep check, instead of relying on seq_cst operations. The waiting side keeps its acquire load, which is what orders it after the pages the workers pushed to the scratch stacks, and the worker acquires the scratch pointer, since it may be running on a request from an earlier collection. On macOS aarch64 the waiting side now compiles tostr,dmb ish,ldapr. The remaining operations on these two variables (the stores that publish the scratch stacks and the decrement that ends a worker's sweep) are now release operations, using a newjl_atomic_fetch_add_releasewrapper, so that nothing in the handshake relies on seq_cst operations.I have not reproduced the crash locally; the fix is based on the analysis in the issue, which I checked against the source and the disassembly of the 1.13.4 macOS aarch64 binary.
This should be backported to 1.12 and 1.13.
Fixes #65301
This pull request was written with the assistance of generative AI.
🤖 Generated with Claude Code