← Back to Pull requests

Release a failed dict-literal build's operands and half-built dict #961

Closed
unknown closed this pull request without merging on Oct 05, 2026 09:43 UTC
View on GitHub
unknown unknown commented on Oct 02, 2026 03:59 UTC
Author

Closes #718

What

VM::build_dict was the one site left in the collection-literal error-path
family from #718. It consumed its operands through a plain into_iter() and
kept the half-built Dict in a plain local, so an insertion that raises — an
unhashable key, or a colliding __eq__ — dropped both by ordinary Rust drop
instead of drop_with. The reference counts of every heap value already
inserted, 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_set in this same file, Set::from_iterable, and hr_update via
extend_from_entries). This change routes build_dict's operand iterator and
half-built dict through the same defer_drop_mut! / DropGuard pair, so all
four read alike.

Dict::set already releases the failing key and value itself, so the guards
are only responsible for what set never receives: the pairs already folded
into the dict, and the operand tail.

Test

Adds crates/monty/test_cases/refcount__dict_build_failure.py, mirroring the
existing refcount__set_build_failure.py. It covers the dict literal,
dict(pairs), dict(mapping), and a colliding __eq__ during update —
the four ways a dict build takes ownership of keys and values before an
insertion raises. As a ref-counts fixture it is Monty-only, so the CPython
dual-run skips it, matching the set fixture.

Validation

Built and run locally against main at 3f9d6ef:

  • The new fixture's counts were read out of MontyRun::run_ref_counts rather

  • than hand-written. On the unfixed VM the four operand tuples settle at
    3 instead of 1 — one reference inside the half-built dict and one in the
    unreached operand tail — so the fixture fails as intended; with the fix they
    settle at 1 and the strict reachability check reports no unreachable heap
    objects.
  • Under memory-model-checks the same repro aborts on the unfixed VM inside

  • Value::drop → vec::IntoIter<Value>::drop → VM::build_dict, which is the
    leak surfacing; it is clean with the fix.
  • cargo test -p monty --features "memory-model-checks test-hooks" — 30 test

  • binaries, 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-datatest could not be linked here: this box has no

  • libpython3.13.so and no root to install it, so the pyo3-backed harness and
    the 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_counts directly
    instead of from a harness run — same code path, minus the CPython comparison.
  • cargo clippy --workspace -- -D warnings currently fails on three

  • pre-existing must_use lints in monty-types, from a clippy newer than the
    one 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_dict where 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 through defer_drop_mut! / DropGuard, matching the other three collection sites from issue #718.

    Test

  • Adds a fixture that exercises the four entry paths (dict literal, dict(pairs), dict(mapping), and a colliding __eq__ during update) 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. -->

    Diff: 540 lines (+405 / -135) across 6 files Reviewer: davidhewitt Review duration: 4664m
    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
    0.48
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    56%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    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
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 56.8% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 48.0% (MEDIUM) · Higher = larger blast radius
    48.0%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 56.8% · Lower = rushed or shallow review
    56.8%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 20.8% · Safe to merge
    20.8%
    Repository