marimo icon indicating copy to clipboard operation
marimo copied to clipboard

Code Execution Ordering Dependence

Open TateStaples opened this issue 7 months ago • 3 comments

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

{
  "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()

TateStaples avatar Apr 27 '26 23:04 TateStaples

We do have @mo.cache that will work reactively if you need it: https://docs.marimo.io/api/caching/

But yes, this is a known limitation. Thanks for opening, hopefully someone searching will find this issue. Please see this discussion: https://github.com/marimo-team/marimo/discussions/5713 for more context

I could add a lint rule marimo check ... that could capture these cases and complain, but it's not something we can easily address otherwise

dmadisetti avatar Apr 27 '26 23:04 dmadisetti

I’ve been looking into this — I’m checking how cell dependency scheduling decides execution order so I can pin down the ordering bug and add a regression test. Happy to send a fix.

onthebed avatar Apr 28 '26 05:04 onthebed

I'm actually going to close this out, since we do document this: https://docs.marimo.io/guides/reactivity/#variable-mutations-are-not-tracked

I think a lint rule would require more discussion

dmadisetti avatar Apr 28 '26 17:04 dmadisetti