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

Merged
YI
2mo ago
+83-9

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.


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.