Skip to content

fix: mo.cache returning stale values#8411

Merged
dmadisetti merged 4 commits intomarimo-team:mainfrom
ffmiruz:fix-8364-cache-invalidation
Feb 27, 2026
Merged

fix: mo.cache returning stale values#8411
dmadisetti merged 4 commits intomarimo-team:mainfrom
ffmiruz:fix-8364-cache-invalidation

Conversation

@ffmiruz
Copy link
Contributor

@ffmiruz ffmiruz commented Feb 21, 2026

The RemoveReturns AST transformer was converting 'return expr' to just 'expr' (expression statement). Python's compiler constant-folds numeric expressions ('11 + 19' becomes '30') and since the result is unused (expression statement), it optimizes them away entirely. This caused different numeric expressions to compile to identical bytecode, producing identical hashes.

Changed RemoveReturns to convert 'return expr' to '_ = expr' (assignment statement) instead of just 'expr'. This preserves the constant values in the compiled bytecode, ensuring different expressions produce different hashes.

Fixes #8364

The RemoveReturns AST transformer was converting 'return expr' to
just 'expr' (expression statement). Python's compiler constant-folds numeric
expressions ('11 + 19' becomes '30') and since the result is unused (expression statement), it optimizes
them away entirely. This caused different numeric expressions to compile to
identical bytecode, producing identical hashes.

Changed RemoveReturns to convert 'return expr' to '_ = expr' (assignment
statement) instead of just 'expr'. This preserves the constant values in the
compiled bytecode, ensuring different expressions produce different hashes.
@ffmiruz ffmiruz requested a review from dmadisetti as a code owner February 21, 2026 10:19
@vercel
Copy link

vercel bot commented Feb 21, 2026

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

Project Deployment Actions Updated (UTC)
marimo-docs Ready Ready Preview, Comment Feb 27, 2026 8:49pm

Request Review

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This pull request fixes a critical cache invalidation bug where @mo.cache was returning stale values when decorated function bodies changed, specifically for numeric return types. The root cause was that Python's compiler optimizes away unused expression statements after constant-folding (e.g., 11 + 19 becomes 30, but as an unused expression it gets removed), resulting in identical bytecode hashes for different numeric expressions.

Changes:

  • Modified the RemoveReturns AST transformer to convert return expr into _ = expr (assignment statement) instead of just expr (expression statement), ensuring constant values are preserved in the compiled bytecode
  • Added comprehensive test coverage for cache invalidation with numeric (int), string, and float return types

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.

File Description
marimo/_ast/transformers.py Changed RemoveReturns transformer to use assignment statements instead of expression statements to preserve constants in bytecode
tests/_save/test_cache_invalidation.py Added new test suite for cache invalidation with int, string, and float return types

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

expr.col_offset = node.col_offset
return expr
def visit_Return(self, node: ast.Return) -> ast.Assign:
# Convert "return expr" to "_ = expr" to preserve constant values in bytecode.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice approach!

I think this is fairly cache-busting for many, can we check for particular statement forms to lower the chance of existing invalidation?

@dmadisetti dmadisetti merged commit 2940d15 into marimo-team:main Feb 27, 2026
28 of 39 checks passed
@ffmiruz ffmiruz deleted the fix-8364-cache-invalidation branch March 3, 2026 02:57
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.

mo.cache invalidation issues on function body change

4 participants