Skip to content

Make iterators hold a reference to the cache - #70

Merged
awolverp merged 3 commits into
awolverp:mainfrom
Malkiz223:iterator_cache_ref
Aug 9, 2026
Merged

Make iterators hold a reference to the cache#70
awolverp merged 3 commits into
awolverp:mainfrom
Malkiz223:iterator_cache_ref

Conversation

@Malkiz223

Copy link
Copy Markdown
Contributor

Closes #69

The iterator types kept a raw cursor and a generation counter, but no reference to the cache object, so a cache that nothing else held was freed while the walk was still running, and the walk read freed memory. Now every iterator type holds a Py<PyAny> to the cache: keys, values and items take slf instead of &self and store it. __traverse__ is there so the cycle "cache holds the iterator as a value, iterator holds the cache" stays collectable; __clear__ is not needed, the cache's own __clear__ breaks that cycle.

No answers change, and speed does not change either: creating an iterator stays at 37 ns.

Two tests in the shared mixin, so they run for all seven types: walking a cache that nothing else holds, and a cache that holds its own iterator still being collected. The walk runs in a child process on purpose: reading freed memory takes the whole run down instead of failing a test. Without the patch it fails for Cache and LRUCache and passes for the other five, which is what reading freed memory looks like.

1011 passed, 6 skipped, 5 runs out of 5. Those runs have #68 applied on top: on plain main the suite freezes in almost every run because of #67, which this branch does not touch.

@awolverp

awolverp commented Aug 9, 2026

Copy link
Copy Markdown
Owner

You pointed out a good point. Thanks for the solution. It just seems like there’s a problem
on PyPI

@awolverp
awolverp merged commit f897b80 into awolverp:main Aug 9, 2026
6 of 12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Walking a cache that nothing else holds reads freed memory

2 participants