Skip to content

Fix reference/resource leaks on three C-extension error paths - #1381

Merged
asvetlov merged 14 commits into
aio-libs:masterfrom
devdanzin:fix/error-path-leaks
Sep 6, 2026
Merged

asvetlov merged 14 commits into
aio-libs:masterfrom
devdanzin:fix/error-path-leaks

Conversation

@devdanzin

Copy link
Copy Markdown
Contributor

What do these changes do?

Three leaks on error paths in the C extension:

  1. items-view __contains__ (_multilib/views.h) fetched and owned element 0 of a candidate pair,
    then returned on an element-1 failure without releasing element 0.
  2. md_repr (_multilib/hashtable.h) did a bare return NULL on the "changed during iteration" guard,
    leaking its PyUnicodeWriter; it now goto fail to discard it.
  3. md_next (_multilib/hashtable.h) leaked the identity reference when key materialisation failed
    under low memory.

C-extension-only concerns (the pure-Python backend relies on the GC); no pure-Python change is needed.

Are there changes in behavior for the user?

No user-visible behavior change; these are leak fixes on error paths.

Is it a substantial burden for the maintainers to support this?

No. Small, localized changes.

Related issue number

Findings from the C-extension analysis report #1306.

Checklist

  • I think the code is well written
  • Unit tests for the changes exist (deterministic refcount test for (1), functional test for (2))
  • Documentation reflects the changes (no public API change)
  • N/A — no CONTRIBUTORS.txt in this repo
  • Add a new news fragment into the CHANGES/ folder

- items-view __contains__ (views.h): when the candidate is a generic
  sequence, element 0 was fetched and owned, then a failure reading element 1
  returned -1 without releasing element 0.
- md_repr (hashtable.h): the 'changed during iteration' guard did a bare
  return NULL, leaking the PyUnicodeWriter; use goto fail to discard it.
- md_next (hashtable.h): when _md_ensure_key() fails (low memory) after the
  identity out-param was already set, release it before cleanup nulls it.

Adds a deterministic refcount regression test for the __contains__ leak and a
functional test for the repr-during-mutation path.
@devdanzin
devdanzin requested a review from webknjaz as a code owner July 24, 2026 14:06
@devdanzin
devdanzin requested a review from asvetlov as a code owner July 24, 2026 14:06
@pyba-chronographer pyba-chronographer Bot added the bot:chronographer:provided There is a change note present in this PR label Jul 24, 2026
Comment thread tests/test_multidict.py Dismissed
@codspeed

codspeed Bot commented Jul 24, 2026 •

Copy link
Copy Markdown

Merging this PR will not alter performance

✅ 248 untouched benchmarks


Comparing devdanzin:fix/error-path-leaks (8be73a3) with master (afe123b)

Open in CodSpeed

@Vizonex
Vizonex self-requested a review July 24, 2026 18:11

@Vizonex Vizonex left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@devdanzin Seems the workflow fails with spelling issues. A helpful reminder would be to
pip install pre-commit or uv pip install pre-commit and then run pre-commit run --all-files to fix or find the spelling mistakes that are preventing this PR from passing.

@greptile-apps

greptile-apps Bot commented Sep 5, 2026 •

Copy link
Copy Markdown
Contributor

Confidence Score: 5/5

Safe to merge based on focused execution of the affected C-extension error paths.

No blocking failure remains.

T-Rex T-Rex Logs

What T-Rex did

  • Verified the focused free-threaded refcount skip validation source and its command-output log, establishing the exact authored validation used for the test.
  • Noted the guard update in commit 76c8e96 that adds the free-threaded check.
  • Reviewed the focused Python harness and C probe sources and the associated test outputs for PR 1381 error paths.
  • Validated the PR 1381 error-paths results by examining the targeted C-extension test output (result=-1, outputs_cleared=1, identity_refcount=1->1, exit 0) and the focused harness output for the forced md_next failure.

View all artifacts

T-Rex Ran code and verified through T-Rex

Reviews (5): Last reviewed commit: "Update CHANGES/1381.bugfix.rst" | Re-trigger Greptile

Comment thread tests/test_multidict.py Resolved
Comment thread tests/test_multidict.py Resolved
Comment thread tests/test_multidict.py Resolved
Comment thread tests/test_multidict.py Resolved
Comment thread tests/test_multidict.py Resolved
Comment thread CHANGES/1381.bugfix.rst Resolved
@codecov

codecov Bot commented Sep 6, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.87%. Comparing base (0a4d62d) to head (8be73a3).
⚠️ Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #1381   +/-   ##
=======================================
  Coverage   99.86%   99.87%           
=======================================
  Files          29       29           
  Lines        3808     3852   +44     
  Branches      282      284    +2     
=======================================
+ Hits         3803     3847   +44     
  Misses          3        3           
  Partials        2        2           
Flag Coverage Δ
CI-GHA 99.87% <100.00%> (+<0.01%) ⬆️
pytest 99.87% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@asvetlov
asvetlov merged commit e4cd5f6 into aio-libs:master Sep 6, 2026
57 checks passed
@asvetlov

asvetlov commented Sep 6, 2026

Copy link
Copy Markdown
Member

Thanks, @devdanzin !

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided There is a change note present in this PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants