Get help from the marimo community

Updated 7 months ago

Invalid Session for create_asgi_app deployed across 4 different machines

At a glance
The post describes an error related to an invalid session ID in the marimo library. The comments discuss the stateful nature of marimo and the challenges of running multiple instances, especially in a Kubernetes environment where the community member has no control over scaling. The community members suggest exploring options like Session Affinity, disabling autoscaling, or managing state through a service like Redis, but note that the latter may be difficult due to the runtime state not being fully serializable. The community members also discuss creating an issue on the marimo repository to request a solution for this problem.
Useful resources
Plain Text
  File "/bb/libexec/workflow-metrics-notebooks/python/lib/python3.10/site-packages/marimo/_server/api/deps.py", line 135, in require_current_session
    raise ValueError(f"Invalid session id: {session_id}")
ValueError: Invalid session id: s_p41hf3
s
M
16 comments
Plain Text
import argparse
import logging
from typing import Any

import marimo
from fastapi import FastAPI
import uvicorn



LOG_LEVELS = ["DEBUG", "INFO", "WARNING", "ERROR", "CRITICAL"]

def parse_args(args: list[str] | None = None) -> dict[str, Any]:
    parser = argparse.ArgumentParser()
    parser.add_argument("--log-file", "-l")
    parser.add_argument("--host", default="0.0.0.0")
    parser.add_argument("--log-level", default="INFO", choices=LOG_LEVELS)
    parser.add_argument("--dir", default="./notebooks")
    parsed_args, unknown = parser.parse_known_args(args=args)
    logging.info(f'unknown args - {unknown}')
    return vars(parsed_args)



def start_server(host: str, dir: str) -> None:

    server = (
        marimo.create_asgi_app()
        .with_app(path="", root=f'{dir}/workflow-metrics.py')
    )

    # Create a FastAPI app
    app = FastAPI(host=host)
    app.mount("/", server.build())

    uvicorn.run(app, host=host, port=8080)


def main() -> None:
    args = parse_args()
    start_server(args.pop("host"), args.pop("dir"))


if __name__ == "__main__":
    main()
marimo is stateful so it doesn’t support multiple instances at once. You will need to build a layer on top to manage this
(or drop to one machine if you can)
where would you start
i'm deploying in my companies which is using kubernetes
so multiple containers w/ no control over it
it’s a concept called Session Affinity on most cloud providers. If you have a load balancer between your Kubernetes - you could enable it.
Usually you can set the scale factor or turn off autoscaling for k8s. Do you not have access to that? Can you ask?
yeah our kaas doesn't support it and requires us to be stateless....
its an internally managed and built tool
is there a way to manage the state via redis or something?
not at the moment - it’s quick difficult since there is a lot of runtime state (not all serializable)
btw just created an issue if thats okay it seems like a fair ask whether prioritized soon or not
This will be extremely difficult since not everything is serializable so can’t be stateless. These are running programs that inherently have state. Even if we made marimo stateless, your code may not be (e.g threads, db connections, etc)

I think a better request would be a load balancer that can manage multiple instance
Add a reply
Sign up and join the conversation on Discord