Repository navigation
fix: stop CacheExtension leaking a cache key when a later extension short-circuits the render - #1648
Merged
Merged
Conversation
…ircuits CacheExtension tracked the in-progress cache key in `render_id_to_cache_key`, a dict on the (singleton) extension keyed by render ID, and removed the entry in `on_component_rendered`. User extensions are always ordered after the built-in CacheExtension, so if a later extension's `on_component_input` short-circuited the render (returned non-None) after CacheExtension had already stored its key on a cache miss, `on_component_rendered` never fired for that component and the entry was orphaned, growing unboundedly. Store the key on the per-component-instance `ComponentCache` config instead. A fresh component (and thus a fresh config) is built per render, so the key is released together with the component and there is no shared mutable state to leak into. This also removes the only cross-render shared state in the extension, so it is correct under concurrent rendering by construction. Tests: replace the dict-introspecting `TestCacheRenderIdCleanup` with `TestCacheRenderKeyLifecycle`, whose `test_no_leak_when_later_extension_short_circuits` reproduces the bug via a short-circuiting extension and asserts (by weakref) that the components are garbage-collected. Also guard the existing `test_request_is_gc_after_render` with `skipif` for non-CPython, since its two-pass `gc.collect()` relies on CPython collection ordering. Refs #1607. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Contributor
Performance Benchmark ResultsComparing PR changes against master branch: Benchmarks that have stayed the same:
|
Per the CHANGELOG policy in CLAUDE.md, internal fixes can still get a brief, high-level mention. Document the cache leak without the per-instance-config mechanics. Refs #1607. Co-Authored-By: Claude Opus 4.8 <[email protected]>
Contributor
Performance Benchmark ResultsComparing PR changes against master branch: Benchmarks that have stayed the same:
|
Contributor
Performance Benchmark ResultsComparing PR changes against master branch: Benchmarks that have stayed the same:
|
This was referenced May 30, 2026
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
(Opus 4.8)
Summary
Fixes a memory leak in
CacheExtensionthat is the residual case left by #1613 / #1607.CacheExtensiontracked the in-progress cache key inrender_id_to_cache_key— a dict on the singleton extension, keyed by render ID — and removed the entry inon_component_rendered. User extensions are always ordered after the built-inCacheExtension(app_settings.py). So if a later extension'son_component_inputshort-circuits the render (returns non-None) afterCacheExtensionhas already stored its key on a cache miss,on_component_renderednever fires for that component and the entry is orphaned — growing unboundedly, one entry per render.The fix
Store the key on the per-component-instance
ComponentCacheconfig instead of a dict on the extension. A fresh component (and thus a fresh config) is built per render, so:Reproduction (before the fix)
An extension whose
on_component_inputreturns non-None, registered afterCacheExtension, rendering a cache-enabled component on cache miss 5×:Tests
TestCacheRenderIdCleanupwithTestCacheRenderKeyLifecycle:test_no_leak_when_later_extension_short_circuits— reproduces the bug via aShortCircuitExtensionand asserts (by weakref) that the component instances are garbage-collected.test_error_does_not_cache— error path doesn't write to the cache.test_request_is_gc_after_render(added in fix: memory leaks in CacheExtension.render_id_to_cache_key and context_processors_data WeakKeyDictionary #1613) withskipiffor non-CPython, since its two-passgc.collect()relies on CPython collection ordering.Checks
uv run ruff check .— cleanuv run ruff format --check(touched files) — cleanuv run mypy src/django_components/extensions/cache.py— cleanuv run pytest tests/test_component_cache.py tests/test_context.py tests/test_extension.py— 122 passedRefs #1607, #1613
🤖 Generated with Claude Code