Skip to content

Code Execution Ordering Dependence #9407

@TateStaples

Description

@TateStaples

Describe the bug

I was writing code with a _cache variable and I noticed that this leads to the code behaving non-deterministically depending on your chosen execution order.

I understand if this is problematic to try to solve with dependency analysis but was hoping it could at least end up as a warning somewhere so this gotcha doesn't happen to anyone else

Will you submit a PR?

  • Yes

Environment

Details
{
  "marimo": "0.23.2",
  "editable": false,
  "location": "/Users/tatestaples/Code/Explorations/.venv/lib/python3.13/site-packages/marimo",
  "OS": "Darwin",
  "OS Version": "24.1.0",
  "Processor": "arm",
  "Python Version": "3.13.11",
  "Locale": "C/en_US",
  "Binaries": {
    "Browser": "147.0.7727.103",
    "Node": "v25.9.0",
    "uv": "0.10.10 (Homebrew 2026-03-13)"
  },
  "Dependencies": {
    "click": "8.3.1",
    "docutils": "0.22.4",
    "itsdangerous": "2.2.0",
    "jedi": "0.19.2",
    "markdown": "3.10.2",
    "narwhals": "2.18.0",
    "packaging": "26.0",
    "psutil": "7.2.2",
    "pygments": "2.19.2",
    "pymdown-extensions": "10.21",
    "pyyaml": "6.0.3",
    "starlette": "1.0.0",
    "tomlkit": "0.14.0",
    "typing-extensions": "4.15.0",
    "uvicorn": "0.42.0",
    "websockets": "16.0"
  },
  "Optional Dependencies": {
    "altair": "6.0.0",
    "anywidget": "0.9.21",
    "duckdb": "1.5.1",
    "loro": "1.10.3",
    "nbformat": "5.10.4",
    "openai": "2.29.0",
    "pandas": "3.0.1",
    "polars": "1.39.3",
    "pyarrow": "23.0.1",
    "ruff": "0.15.7",
    "sqlglot": "30.0.3"
  },
  "Experimental Flags": {
    "rtc_v2": false
  }
}

Code to reproduce

# /// script
# requires-python = ">=3.12"
# dependencies = [
#     "marimo>=0.20.2",
# ]
# ///

import marimo

__generated_with = "0.23.2"
app = marimo.App(width="medium")


@app.cell
def _():
    _cache = dict()
    def square(x):
        if x in _cache:
            print("Cache Hit")
            return _cache[x] + 1
        res = x*x
        print("Cache Miss")
        _cache[x] = res
        return res

    return (square,)


@app.cell(hide_code=True)
def _(square):
    square(5)
    return


@app.cell
def _(square):
    square(5)
    return


if __name__ == "__main__":
    app.run()

Metadata

Metadata

Assignees

No one assigned

    Labels

    documentationImprovements or additions to documentation

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions