whitphx / streamlit-server-state

A "server-wide" state object shared across sessions on a Streamlit server.
https://discuss.streamlit.io/t/new-library-streamlit-server-state-a-new-way-to-share-states-over-the-sessions-on-the-server/14981
MIT License
138 stars 13 forks source link

It stopped working #168

Open Nemecsek opened 1 year ago

Nemecsek commented 1 year ago

With this demo code I was able to make some experiments, but then it stopped working completely. I restarted the browser, tried with different browsers, restarted the app, removed the lock but nothing works anymore and the browsers is in an infinite loop tills it crashes.

import streamlit as st
from streamlit_server_state import server_state, server_state_lock

with server_state_lock["u1"]:
    if "u1" not in server_state:
        server_state.u1 = 0

server_state.u1 = server_state.u1 + 1

st.write(server_state.u1)

Traceback is

2023-01-26 13:02:04.236 Uncaught app exception
Traceback (most recent call last):
  File "/home/alex/PycharmProjects/test_bluelikon_client_streamlit/venv/lib/python3.8/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 565, in _run_script
    exec(code, module.__dict__)
  File "/home/alex/PycharmProjects/test_bluelikon_client_streamlit/singleuser.py", line 12, in <module>
    st.write(server_state.u1)
  ...
  File "/home/alex/PycharmProjects/test_bluelikon_client_streamlit/venv/lib/python3.8/site-packages/streamlit/runtime/scriptrunner/script_requests.py", line 157, in on_scriptrunner_yield
    return ScriptRequest(ScriptRequestType.RERUN, self._rerun_data)
  File "<string>", line 3, in __init__
RecursionError: maximum recursion depth exceeded while calling a Python object

Any idea?

bandoos commented 1 year ago

I think that what happens here (because of streamlit's - in this case streamlit_server_state - refresh logic) is that any time the script runs, the increment line is executed, which in mutates the server state and makes all pages reload (since server state has in fact changed), thus the st.write code is in practice dead code!

Nemecsek commented 1 year ago

I assume it is guaranteed that before page reload the current page is executed completely, so the st.write should be executed too.