Skip to content

Keep shallow cache copies independent of their sources - #715

Open
Sskift wants to merge 5 commits into
mahmoud:masterfrom
Sskift:fix/cache-shallow-copy
Open

Sskift wants to merge 5 commits into
mahmoud:masterfrom
Sskift:fix/cache-shallow-copy

Conversation

@Sskift

@Sskift Sskift commented Nov 2, 2026 •

Copy link
Copy Markdown

The change

Keep copy.copy() of LRI and LRU from sharing mutable cache bookkeeping with the source. In a two-entry cache, inserting into the shallow copy can currently make a source lookup return a value whose key is absent from that source's dictionary, or fail for a key that is still present.

Reconstruct the shallow copy under the source lock, following the reduction factory, instance state and list/item iterators. When copied state shares cache internals with the source, rebuild an independent lock and linked-list bookkeeping while retaining dictionary and eviction order. Registered reducers, subclass hooks, factory-provided contents and non-cache factory results retain their reconstruction behavior. Independently constructed cache results keep their own internal state. Payload objects remain shared, as expected for a shallow copy. The explicit cache.copy() method keeps its existing fresh-counter behavior; this does not add deepcopy or pickle support.

On Windows / CPython 3.12.10, the full unit and doctest suite passes: 816 passed, 1 skipped, 22 subtests passed. The 75 cache tests cover source/copy independence, statistics, dictionary versus eviction order, shared mutable values, constructor arguments, slots, state hooks, reducer dispatch, filtered or transformed item iterators, list results, custom assignment hooks and factories returning existing cache objects. Twelve added reducer-output cases fail against the previous revision; all pass with this change. git diff --check also passes. A separate Codex agent approved this exact revision after rerunning the 75 cache tests, checking 240 reducer scenarios on each of actual CPython 3.7.9, 3.8.10 and 3.12.10, and verifying the existing-target lock behavior with a concurrent on_miss scenario. The full pytest suite was run only on 3.12.10.

Related #506 fixed calls to cache.copy() and was folded into #609. The generic copy.copy() reconstruction path still shares the internal linked-list lookup and is the separate issue addressed here. Current open PR inventory and all-state cache-copy searches were checked before preparing this change.

The backstory

This was found through a source audit and executable comparisons of the cache's explicit copy() method with Python's copy.copy(). The account owner asked Codex to contribute tested fixes to established open-source projects. Further inspection and independent agent review checked reducer behavior and found compatibility cases involving factories, item iterators and an existing target cache's lock; the implementation and regression coverage were expanded accordingly.

AI assistance

  • Harness/tooling: OpenAI Codex, GitHub connector, Git, Python and pytest. Codex investigated the issue, prepared the patch and ran the tests.
  • Model(s): GPT-6-based Codex; the exact serving identifier is not exposed to this agent.
  • Original prompt from the account owner: “接替另一个会话的工作 把goal抄过来 开不少于20个subagent来推进”. The prior contribution goal is to submit useful, validated changes to established open-source projects and follow up on review feedback.
  • Repository-specific task, verbatim: “Own mahmoud/boltons contribution in D:/code-test/playground/contributions/python/boltons. First read D:/code-test/playground/contributions/handoff-20261103/WORKING-RULES.md. Inspect prior branch/changes and contribution policy, search duplicate fixes/PRs, reproduce a concrete issue, implement robust minimal fix plus regression and run appropriate checks. Send root review-ready evidence/proposed PR text before publication. Save boltons_fix.json. No spawning or shared state edits.”

This branch has not been deployed

No deployments
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.

1 participant