Skip to content

Python: Fix Purview cache size accounting when replacing entries - #10257

Merged
Eduard van Valkenburg (eavanvalkenburg) merged 1 commit into
microsoft:mainfrom
Dhruv-Sh-git:fix/issue-8988
Nov 12, 2026
Merged

Eduard van Valkenburg (eavanvalkenburg) merged 1 commit into
microsoft:mainfrom
Dhruv-Sh-git:fix/issue-8988

Conversation

@Dhruv-Sh-git

@Dhruv-Sh-git Dhruv Shaswat (Dhruv-Sh-git) commented Nov 11, 2026 •

Copy link
Copy Markdown
Contributor

This PR fixes an issue in InMemoryCacheProvider where replacing an existing Purview cache entry with a larger value could cause incorrect cache size accounting and allow the configured max_size_bytes limit to be exceeded.

The root cause was that set() subtracted the size of the previous entry without removing its mapping from _cache. Consequently, when _evict_if_needed() ran, the eviction logic could encounter the old mapping and subtract its size again, resulting in an incorrect byte count.

Changes made:

  • Remove the previous entry mapping from _cache before triggering eviction. This ensures that any corresponding expiry timestamp encountered in the heap is correctly treated as stale and skipped.
  • Add a regression test, test_growing_entry_at_capacity_maintains_size_and_evicts_other_entry, to verify that replacing an entry with a larger value maintains accurate size accounting, evicts other entries when necessary, and correctly resets the tracked size to zero upon removal.

This change ensures consistent cache size tracking and prevents the cache from exceeding its configured memory limit due to incorrect accounting.

Closes #8988

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@agent-framework-automation agent-framework-automation Bot added the python Usage: [Issues, PRs], Target: Python label Nov 11, 2026
@eavanvalkenburg

Copy link
Copy Markdown
Member

/review

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

MAF Automated Review — Iteration 1

Result: No findings
Scope: full PR (1 commit(s)): b2f6cdcc0989
Model: gpt-5.6-sol

Overview

The change removes the replaced mapping before eviction, so its old expiry tuple is treated as stale rather than decrementing the entry twice. Cache operations contain no suspension points, expiry tuples are validated against the live mapping's expiry, and the regression test verifies eviction, accounting consistency, and removal. These guards prevent a publishable Critical, High, or Medium regression in the reviewed change.

Reviewed the supplied pull-request change set across correctness, security/reliability, architecture, and failure behavior.
No publishable findings remained after source verification for this scope.

Merged via the queue into microsoft:main with commit 01de197 Nov 12, 2026
56 checks passed

This branch was successfully deployed

1 active deployment
github-app-auth — b2f6cdcc Deployed Nov 11, 2026 by Dhruv-Sh-git via add_label #27559
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

python Usage: [Issues, PRs], Target: Python

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Python: [Bug]: Purview cache undercounts bytes when growing an entry at capacity

3 participants