Skip to content

fix: memory leaks in CacheExtension.render_id_to_cache_key and context_processors_data WeakKeyDictionary - #1606

Closed
oliverhaas wants to merge 2 commits into
django-components:masterfrom
oliverhaas:fix/cache-render-id-memory-leak
Closed

oliverhaas wants to merge 2 commits into
django-components:masterfrom
oliverhaas:fix/cache-render-id-memory-leak

Conversation

@oliverhaas

@oliverhaas oliverhaas commented Mar 20, 2026 •

Copy link
Copy Markdown
Contributor

See#1607

  1. Change dict read to .pop() in on_component_rendered so entries are removed after use. Also clean up on the error path.
  2. Remove context_processors_data WeakKeyDictionary in favor of setting the context on the request itself.

Tests for reproduction mostly, can be dropped, but I would keep them.

@github-actions

Copy link
Copy Markdown
Contributor

Performance Benchmark Results

Comparing PR changes against master branch:

Benchmarks that have stayed the same:

Change Before [51b51a8] After [da7b921] Ratio Benchmark (Parameter)
68.0±0.4ms 70.3±1ms 1.03 Components vs Django.timeraw_render_lg_first('django')
246±3ms 251±5ms 1.02 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 [51b51a8] After [8b849e6] Ratio Benchmark (Parameter)
63.2±0.9ms 62.5±1ms 0.99 Components vs Django.timeraw_render_lg_first('django')
232±9ms 227±6ms 0.98 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 [51b51a8] After [bb464c0] Ratio Benchmark (Parameter)
58.5±0.8ms 58.1±0.7ms 0.99 Components vs Django.timeraw_render_lg_first('django')
211±1ms 212±2ms 1 Components vs Django.timeraw_render_lg_first('django-components')

@oliverhaas oliverhaas changed the title fix: memory leak in CacheExtension.render_id_to_cache_key fix: memory leak in CacheExtension.render_id_to_cache_key and context_processors_data WeakKeyDictionary Mar 21, 2026
@oliverhaas
oliverhaas force-pushed the fix/cache-render-id-memory-leak branch 2 times, most recently from 88b26f2 to 52ead66 Compare March 21, 2026 20:31
Entries were never removed. Two leak paths:
- on_component_rendered read the key but never popped it.
- on_component_input stored the key before checking the cache, so on
  cache hits the entry leaked because on_component_rendered never runs.
Cache context processor data on the request object itself instead of a
module-level WeakKeyDictionary, so the cache is automatically cleaned up
when the request is garbage collected.
@oliverhaas
oliverhaas force-pushed the fix/cache-render-id-memory-leak branch from 52ead66 to f656098 Compare March 21, 2026 20:32
@github-actions

Copy link
Copy Markdown
Contributor

Performance Benchmark Results

Comparing PR changes against master branch:

Benchmarks that have stayed the same:

Change Before [51b51a8] After [1ecdc22] Ratio Benchmark (Parameter)
67.8±0.5ms 68.3±1ms 1.01 Components vs Django.timeraw_render_lg_first('django')
244±3ms 243±1ms 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 [51b51a8] After [311de34] Ratio Benchmark (Parameter)
69.1±0.8ms 69.2±1ms 1 Components vs Django.timeraw_render_lg_first('django')
248±2ms 249±3ms 1.01 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 [51b51a8] After [e90834a] Ratio Benchmark (Parameter)
67.6±0.9ms 67.1±0.3ms 0.99 Components vs Django.timeraw_render_lg_first('django')
242±2ms 242±3ms 1 Components vs Django.timeraw_render_lg_first('django-components')

@oliverhaas oliverhaas changed the title fix: memory leak in CacheExtension.render_id_to_cache_key and context_processors_data WeakKeyDictionary fix: memory leaks in CacheExtension.render_id_to_cache_key and context_processors_data WeakKeyDictionary Mar 22, 2026
@oliverhaas oliverhaas closed this Apr 4, 2026
@oliverhaas
oliverhaas deleted the fix/cache-render-id-memory-leak branch April 4, 2026 07:30
@EmilStenstrom

Copy link
Copy Markdown
Collaborator

@oliverhaas How come you closed this?

@oliverhaas
oliverhaas restored the fix/cache-render-id-memory-leak branch April 4, 2026 20:09
@oliverhaas
oliverhaas deleted the fix/cache-render-id-memory-leak branch April 4, 2026 20:21
@oliverhaas

Copy link
Copy Markdown
Contributor Author

@EmilStenstrom Sorry I kinda messed up my fork while trying out some stuff and then cleaning up. Had to create a new one... See #1613.

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.

2 participants