Release a failed dict-literal build's operands and half-built dict #961
All Vouch review confidence checks passed
Residual risk 0.21 = Change Risk (0.48) × (1 − Review Confidence (0.57)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.48 · 540 lines diff across 6 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 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
Diff Size
diff_size
0.377
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #961"
general
weight: 0.35
davidhewitt
Closes #718
What
VM::build_dictwas the one site left in the collection-literal error-pathfamily from #718. It consumed its operands through a plain
into_iter()andkept the half-built
Dictin a plain local, so an insertion that raises — anunhashable key, or a colliding
__eq__— dropped both by ordinary Rust dropinstead of
drop_with. The reference counts of every heap value alreadyinserted, and of every pair the loop never reached, stayed elevated for the
rest of the run.
The other three sites named in that report already carry the guard pattern
(
build_setin this same file,Set::from_iterable, andhr_updateviaextend_from_entries). This change routesbuild_dict's operand iterator andhalf-built dict through the same
defer_drop_mut!/DropGuardpair, so allfour read alike.
Dict::setalready releases the failing key and value itself, so the guardsare only responsible for what
setnever receives: the pairs already foldedinto the dict, and the operand tail.
Test
Adds
crates/monty/test_cases/refcount__dict_build_failure.py, mirroring theexisting
refcount__set_build_failure.py. It covers the dict literal,dict(pairs),dict(mapping), and a colliding__eq__duringupdate—the four ways a dict build takes ownership of keys and values before an
insertion raises. As a
ref-countsfixture it is Monty-only, so the CPythondual-run skips it, matching the set fixture.
Validation
Built and run locally against
mainat3f9d6ef:MontyRun::run_ref_countsratherthan hand-written. On the unfixed VM the four operand tuples settle at
3instead of1— one reference inside the half-built dict and one in theunreached operand tail — so the fixture fails as intended; with the fix they
settle at
1and the strict reachability check reports no unreachable heapobjects.
memory-model-checksthe same repro aborts on the unfixed VM insideValue::drop→vec::IntoIter<Value>::drop→VM::build_dict, which is theleak surfacing; it is clean with the fix.
cargo test -p monty --features "memory-model-checks test-hooks"— 30 testbinaries, all passing, no failures.
cargo clippy -p monty --features "memory-model-checks test-hooks" --lib—clean, no warnings in
vm/collections.rs.cargo +nightly fmt --check -p monty— clean.Two environment notes, neither caused by this branch:
monty-datatestcould not be linked here: this box has nolibpython3.13.soand no root to install it, so the pyo3-backed harness andthe two test targets that pull it in (
abort,binary_serde) do not build.That is why the fixture's numbers were taken from
run_ref_countsdirectlyinstead of from a harness run — same code path, minus the CPython comparison.
cargo clippy --workspace -- -D warningscurrently fails on threepre-existing
must_uselints inmonty-types, from a clippy newer than theone on this box. Unrelated to this change; left alone.
AI assistance was used to prepare this change.
<!-- This is an auto-generated description by cubic. -->
---
Summary by cubic
Fixes a reference-count leak in
build_dictwhere raising during an insertion (unhashable key or colliding__eq__) left the operand tail and half-built dict with elevated refcounts. Operands and the dict now travel throughdefer_drop_mut!/DropGuard, matching the other three collection sites from issue #718.Test
dict(pairs),dict(mapping), and a colliding__eq__duringupdate) and asserts the operand refcounts settle at 1.<sup>Written for commit 6d65ffa533520bd065adf6b50a5cc5d83625dc95. Summary will update on new commits.</sup>
<a href="https://cubic.dev/pr/pydantic/monty/pull/961?utm_source=github" target="_blank" rel="noopener noreferrer" data-no-image-dialog="true"><picture><source media="(prefers-color-scheme: dark)" srcset="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"><source media="(prefers-color-scheme: light)" srcset="https://www.cubic.dev/buttons/review-in-cubic-light.svg"><img alt="Review in cubic" src="https://www.cubic.dev/buttons/review-in-cubic-dark.svg"></picture></a>
<!-- End of auto-generated description by cubic. -->