Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Sep 3, 2025

This PR adds comprehensive test coverage for the garbage collection behavior of the cache_across_reloads decorator's memoization system.

Background

The cache_across_reloads decorator maintains two global dictionaries:

  • memos: stores cached computation results keyed by (path, qualname)
  • functions: stores function references keyed by (path, qualname)

The gc_memos() function, decorated with @post_reload, runs after module reloads to clean up orphaned memos for functions that no longer exist:

@post_reload
def gc_memos():
    for key in {*memos} - {*functions}:
        del memos[key]

This prevents memory leaks during hot reloading sessions when cached functions are removed or renamed.

Tests Added

Four new test functions comprehensively validate the garbage collection behavior:

  1. test_cache_across_reloads_gc_function_removal: Verifies that memos are properly cleaned up when cached functions are completely removed from a module
  2. test_cache_across_reloads_gc_function_modification: Ensures memos persist when functions are modified but not removed
  3. test_cache_across_reloads_gc_selective_removal: Tests partial cleanup when some functions are removed while others remain
  4. test_cache_across_reloads_gc_across_modules: Validates gc behavior across multiple modules

Each test directly inspects the memos and functions dictionaries to verify correct garbage collection behavior under various hot reload scenarios.

Fixes #348.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@codesandbox
Copy link

codesandbox bot commented Sep 3, 2025

Review or Edit in CodeSandbox

Open the branch in Web EditorVS CodeInsiders

Open Preview

@bolt-new-by-stackblitz
Copy link

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

Co-authored-by: CNSeniorious000 <74518716+CNSeniorious000@users.noreply.github.com>
Copilot AI changed the title [WIP] [hmr] Need tests for cache_across_reloads gc behaviors Add comprehensive tests for cache_across_reloads gc behaviors Sep 3, 2025
Copilot finished work on behalf of CNSeniorious000 September 3, 2025 19:26
@CNSeniorious000 CNSeniorious000 force-pushed the main branch 2 times, most recently from 263cfd3 to 99ecd7b Compare September 12, 2025 01:38
@CNSeniorious000 CNSeniorious000 force-pushed the main branch 5 times, most recently from 0dafc1f to 4cbbea5 Compare November 4, 2025 07:11
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.

[hmr] Need tests for cache_across_reloads gc behaviors

2 participants