Skip to content

fix: stop CacheExtension leaking a cache key when a later extension short-circuits the render - #1648

Merged
JuroOravec merged 3 commits into
masterfrom
fix/cache-extension-shortcircuit-leak
May 30, 2026
Merged

JuroOravec merged 3 commits into
masterfrom
fix/cache-extension-shortcircuit-leak

Conversation

@JuroOravec

@JuroOravec JuroOravec commented May 30, 2026 •

Copy link
Copy Markdown
Collaborator

(Opus 4.8)

Summary

Fixes a memory leak in CacheExtension that is the residual case left by #1613 / #1607.

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 (app_settings.py). So if a later extension's on_component_input short-circuits the render (returns non-None) after CacheExtension has already stored its key on a cache miss, on_component_rendered never 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 ComponentCache config instead of a dict on the extension. A fresh component (and thus a fresh config) is built per render, so:

  • The key is released together with the component — no dict to leak into.
  • There is no longer any cross-render shared mutable state in the extension, so it is correct under concurrent rendering by construction (the old dict was safe only because it was keyed by render ID).

Reproduction (before the fix)

An extension whose on_component_input returns non-None, registered after CacheExtension, rendering a cache-enabled component on cache miss 5×:

DICT SIZE AFTER 5 SHORT-CIRCUITED RENDERS: 5
AssertionError: LEAK: 5 orphaned entries

Tests

  • Replaced the dict-introspecting TestCacheRenderIdCleanup with TestCacheRenderKeyLifecycle:
    • test_no_leak_when_later_extension_short_circuits — reproduces the bug via a ShortCircuitExtension and asserts (by weakref) that the component instances are garbage-collected.
    • test_error_does_not_cache — error path doesn't write to the cache.
  • Guarded the existing test_request_is_gc_after_render (added in fix: memory leaks in CacheExtension.render_id_to_cache_key and context_processors_data WeakKeyDictionary #1613) with skipif for non-CPython, since its two-pass gc.collect() relies on CPython collection ordering.

Per CLAUDE.md's contract-change rule: the removed TestCacheRenderIdCleanup asserted on the internal render_id_to_cache_key dict, which this PR deletes. The replacement asserts the same guarantee (no leak) against the new, observable mechanism (component GC) rather than the now-removed internal attribute.

Checks

  • uv run ruff check . — clean
  • uv run ruff format --check (touched files) — clean
  • uv run mypy src/django_components/extensions/cache.py — clean
  • uv run pytest tests/test_component_cache.py tests/test_context.py tests/test_extension.py — 122 passed

Refs #1607, #1613

🤖 Generated with Claude Code

…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]>
@github-actions

Copy link
Copy Markdown
Contributor

Performance Benchmark Results

Comparing PR changes against master branch:

Benchmarks that have stayed the same:

Change Before [83962e4] After [bc3daee] Ratio Benchmark (Parameter)
62.4±1ms 62.3±1ms 1 Components vs Django.timeraw_render_lg_first('django')
226±4ms 226±2ms 1 Components vs Django.timeraw_render_lg_first('django-components')

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]>
@github-actions

Copy link
Copy Markdown
Contributor

Performance Benchmark Results

Comparing PR changes against master branch:

Benchmarks that have stayed the same:

Change Before [83962e4] After [84c232b] Ratio Benchmark (Parameter)
63.0±2ms 64.7±1ms 1.03 Components vs Django.timeraw_render_lg_first('django')
227±5ms 228±6ms 1 Components vs Django.timeraw_render_lg_first('django-components')

@github-actions

Copy link
Copy Markdown
Contributor

Performance Benchmark Results

Comparing PR changes against master branch:

Benchmarks that have stayed the same:

Change Before [83962e4] After [29b3ca5] Ratio Benchmark (Parameter)
62.6±1ms 62.8±1ms 1 Components vs Django.timeraw_render_lg_first('django')
224±5ms 227±2ms 1.01 Components vs Django.timeraw_render_lg_first('django-components')

@JuroOravec
JuroOravec merged commit 59020a2 into master May 30, 2026
15 checks passed
@JuroOravec
JuroOravec deleted the fix/cache-extension-shortcircuit-leak branch May 30, 2026 07:58
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