Use a bounded LRU cache by default in ParserCache and ValidationCache - #4606
Conversation
|
Thanks for adding the Below is the changelog that will be used for the release. This release fixes a potential unbounded memory growth in the Both extensions previously defaulted to The default is now a bounded LRU cache of 128 entries, matching the import strawberry
from strawberry.extensions import ParserCache, ValidationCache
schema = strawberry.Schema(
Query,
extensions=[
ParserCache(maxsize=None), # explicitly unbounded
ValidationCache(maxsize=100),
],
)Only use This release was contributed by @patrick91 in #4606 |
Greptile SummaryThe PR changes ParserCache and ValidationCache to use a bounded 128-entry LRU cache by default, preventing untrusted query text from causing unbounded memory growth. Explicit
Confidence Score: 5/5The PR appears safe to merge and improves resilience against memory growth from unique client queries. Both extensions now use the intended bounded default, preserve explicit unbounded configuration, and retain safe cache keys and existing extension lifecycle behavior.
|
| Filename | Overview |
|---|---|
| strawberry/extensions/parser_cache.py | Changes the parser cache default from unbounded to a 128-entry shared LRU while preserving explicit unbounded configuration. |
| strawberry/extensions/validation_cache.py | Changes the validation cache default to 128 entries; cache keys continue to include schema identity, document, and validation rules. |
| tests/schema/extensions/test_parser_cache.py | Verifies the parser cache default and eviction bound using 200 distinct query texts. |
| tests/schema/extensions/test_validation_cache.py | Verifies that schema validation through the default extension populates no more than 128 shared cache entries. |
| RELEASE.md | Documents the resource-exhaustion motivation, compatibility impact, and explicit unbounded opt-in. |
| docs/extensions/parser-cache.md | Updates the ParserCache API reference and explains when an unbounded cache is unsafe. |
| docs/extensions/validation-cache.md | Updates the ValidationCache API reference and explains the bounded default. |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart LR
A[GraphQL request] --> B[Query text]
B --> C{Parser cache hit?}
C -->|Yes| D[Parsed document]
C -->|No| E[Parse query]
E --> F[Store in 128-entry LRU]
F --> D
D --> G{Validation cache hit?}
G -->|Yes| H[Validation result]
G -->|No| I[Validate against schema]
I --> J[Store in 128-entry LRU]
J --> H
F -. least recently used eviction .-> K[Bounded memory]
J -. least recently used eviction .-> K
Reviews (1): Last reviewed commit: "Use a bounded LRU cache by default in Pa..." | Re-trigger Greptile
|
This PR was published as 0.327.0. Thank you for contributing! |
Summary by Sourcery
Bound parser and validation caches by default to protect publicly exposed servers from unbounded memory growth.
Bug Fixes:
Enhancements:
Documentation:
Tests: