Skip to content

Fix lru_cache(...) resetting when cell is rerun#9609

Open
leoadberg wants to merge 1 commit into
marimo-team:mainfrom
leoadberg:leoadberg/lru_cache_fix
Open

Fix lru_cache(...) resetting when cell is rerun#9609
leoadberg wants to merge 1 commit into
marimo-team:mainfrom
leoadberg:leoadberg/lru_cache_fix

Conversation

@leoadberg
Copy link
Copy Markdown

@leoadberg leoadberg commented May 19, 2026

📝 Summary

Previously @mo.lru_cache(maxsize=128) (or similar) would reset after the cell is rerun because the extra wrapping call caused the frame offset to point one frame too high, causing the _cache_call._set_context to think the function isn't global.

📋 Pre-Review Checklist

  • For large changes, or changes that affect the public API: this change was discussed or approved through an issue, on Discord, or the community discussions (Please provide a link if applicable). Not a large change
  • Any AI generated code has been reviewed line-by-line by the human PR author, who stands by it. The unit test is AI-written, I stand by it
  • Video or media evidence is provided for any visual changes (optional). no visual changes

✅ Merge Checklist

  • I have read the contributor guidelines.
  • Documentation has been updated where applicable, including docstrings for API changes.
  • Tests have been added for the changes made.

I have read the CLA Document and I hereby sign the CLA

@vercel
Copy link
Copy Markdown

vercel Bot commented May 19, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
marimo-docs Ready Ready Preview, Comment May 19, 2026 5:55pm

Request Review

@github-actions
Copy link
Copy Markdown

github-actions Bot commented May 19, 2026

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@leoadberg
Copy link
Copy Markdown
Author

recheck

Copy link
Copy Markdown
Contributor

@cubic-dev-ai cubic-dev-ai Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No issues found across 2 files

Architecture diagram
sequenceDiagram
    participant User
    participant Kernel as Marimo Kernel
    participant ExecReq as ExecReqProvider
    participant Cell as Cell Executor
    participant LRUCache as @mo.lru_cache Decorator
    participant CacheLogic as _cache_call._set_context
    participant Globals as Kernel Globals

    Note over User,Globals: LRU Cache Persistence Across Cell Reruns

    User->>Kernel: Run cell with @lru_cache(maxsize=128)
    Kernel->>ExecReq: Build execution request
    ExecReq-->>Kernel: Execution request with cell code
    Kernel->>Cell: Execute cell code

    Cell->>LRUCache: Apply @lru_cache(maxsize=128)
    alt Decorator called with parentheses (callable=True)
        LRUCache->>LRUCache: _frame_offset=1
    else Decorator called as @lru_cache (callable=False)
        LRUCache->>LRUCache: _frame_offset=2
    end
    LRUCache->>CacheLogic: _set_context() to determine scope

    alt Frame offset correct (global scope detected)
        CacheLogic-->>LRUCache: Context set as global
        LRUCache->>Globals: Register cached function
        Cell->>LRUCache: Execute foo()
        LRUCache->>Globals: Check cache miss
        Globals-->>LRUCache: No cache entry
        LRUCache->>Cell: Execute foo() body (print "ran")
        Cell-->>LRUCache: Return result
        LRUCache->>Globals: Store result in cache
        Cell->>LRUCache: Execute foo() again
        LRUCache->>Globals: Check cache hit
        Globals-->>LRUCache: Cached result
        LRUCache-->>Cell: Return cached result (no print)
    else Frame offset wrong (local scope detected)
        CacheLogic-->>LRUCache: Context set as local
        Note over LRUCache,Globals: This path caused cache reset on rerun
    end

    Cell-->>Kernel: Cell execution complete
    Kernel-->>User: Cell output (printed "ran" once)

    Note over User,Kernel: Cell rerun (same cell)

    User->>Kernel: Rerun the same cell
    Kernel->>Cell: Execute cell code again
    Cell->>LRUCache: Re-apply decorator
    LRUCache->>CacheLogic: _set_context() with corrected frame offset

    alt Correct frame offset (PR fix)
        CacheLogic-->>LRUCache: Context set as global
        LRUCache->>Globals: Reuse existing cached function
        Cell->>LRUCache: Execute foo()
        LRUCache->>Globals: Check cache hit
        Globals-->>LRUCache: Previous cache entry exists
        LRUCache-->>Cell: Return cached result (no re-execution)
        Note over Cell,Globals: foo() body not re-run, "ran" not printed again
        Cell->>LRUCache: Execute foo() again
        LRUCache->>Globals: Another cache hit
        Globals-->>LRUCache: Cached result
        LRUCache-->>Cell: Return cached result
    end

    Cell-->>Kernel: Cell execution complete
    Kernel-->>User: No additional output (cache persisted across rerun)
Loading

Re-trigger cubic

@mscolnick mscolnick requested a review from dmadisetti May 19, 2026 17:59
@dmadisetti dmadisetti added the bug Something isn't working label May 19, 2026
@leoadberg
Copy link
Copy Markdown
Author

I have read the CLA Document and I hereby sign the CLA

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants