Skip to content

Fix memory leak in SpecValidator.iter_errors by caching on instance - #524

Open
GruffElixir wants to merge 1 commit into
python-openapi:masterfrom
GruffElixir:fix-iter-errors-lru-cache-leak
Open

Fix memory leak in SpecValidator.iter_errors by caching on instance#524
GruffElixir wants to merge 1 commit into
python-openapi:masterfrom
GruffElixir:fix-iter-errors-lru-cache-leak

Conversation

@GruffElixir

Copy link
Copy Markdown

Summary

Fixes #523.

SpecValidator.iter_errors was decorated with @lru_cache(maxsize=None). Because @lru_cache was applied to an instance method, the class-level cache dictionary held a strong reference to self (SpecValidator instance) for every validation performed. This prevented garbage collection of the validator and its loaded schemas, causing unbounded memory growth in long-running applications that validate specifications repeatedly.

Solution

  1. Removed @lru_cache from SpecValidator.iter_errors.
  2. Cached the CachedIterable lazily on the instance (self._cached_errors). This preserves error caching across multiple iterations on the same SpecValidator instance while ensuring instances and schemas are freed once callers release them.
  3. Added regression tests verifying both instance-level error caching and validator garbage collection.

Copilot AI lite review requested due to automatic review settings September 10, 2026 22:03

Copilot AI left a comment

Copy link
Copy Markdown

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 reached their quota limit.

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.

Unbounded cache in SpecValidator.iter_errors retains validator instances and schemas

2 participants