Session state resetting mid session

I have a multi-page app that heavily uses session state to control its execution flow. I made a function to initialize any arbitrary fields the first time a page is run:

def init_state(page_name=None, /, **kwargs: Any) -> None:
    """
    initialize the state of variables in streamlit session state when app page is run for the first time
    """
    flag = "stutils_init_state"
    if flag not in st.session_state:
        st.session_state[flag] = []

    if page_name not in st.session_state[flag]:
        logger.debug(f"{kwargs}")
        set(**kwargs)
        st.session_state[flag].append(page_name)

I use this function in conjunction with the streamlit-authenticator package to set the authentication_status of the user when first running the main page, among other parameters related to buttons and widgets in the other pages.

One thing to note is that the stutils_init_state field is only ever used in this function, and the code does work fine in my machine.

When I deploy my app on the cloud, however, my session state seems to randomly reset even without reloading the page. I put loggers at the end of all my pages to monitor the app state, and I did not leave the browser page at any point during these:


[DEBUG _load_config 17] Loading config from /mount/src/my_app/.streamlit/config.yaml
[DEBUG load_pages_dir 18] Loading pages from /mount/src/my_app/pages
[DEBUG init_state 17] {'authentication_status': None, 'name': None}
[DEBUG <main_page> 62] {'FormSubmitter:Login-Login': False, 'name': None, 'init': None, 'logout': None, 'stutils_init_state': [None], 'authentication_status': None, 'username': ''}
[DEBUG <main_page> 62] {'FormSubmitter:Login-Login': False, 'name': None, 'init': {'_hp2_id.269776235': {'userId': '8829966061954231', 'pageviewId': '1621272184519823', 'sessionId': '6652328196002205', 'identity': None, 'trackerVersion': '4.0'}, '__hstc': 'no', 'hubspotutk': 'something', 'userty.core.p.63850d': 'yeah idc about this data, not part of the problem', '_dd_s': 'smth about logs', '_hp2_ses_props.269788835': {'ts': 1707467652432, 'd': 'app_url', 'h': '/'}, '__hssrc': 1, '__hssc': 'some data', 'userty.core.s.63850d': 'a lot of data', 'some_dynamic_argument': 'an even bigger lot of data', 'ajs_anonymous_id': 'not the real values', '_ga': 'GA1.3.950379.1700738694', '_gid': 'GA1.5.1465234958.1707621375', '_dc_gtm_UA-122023594-8': 0}, 'logout': None, 'stutils_init_state': [None], 'authentication_status': None, 'username': ''}
[DEBUG init_state 17] {'authentication_status': None, 'name': None}
[DEBUG <main_page> 62] {'name': None, 'username': '', 'stutils_init_state': [None], 'authentication_status': None, 'FormSubmitter:Login-Login': False}
[DEBUG init_state 17] {'authentication_status': None, 'name': None}
[DEBUG <main_page> 62] {'name': None, 'username': '', 'stutils_init_state': [None], 'authentication_status': None, 'FormSubmitter:Login-Login': False}
[DEBUG <main_page> 62] {'FormSubmitter:Login-Login': True, 'name': 'my_name', 'set': None, 'stutils_init_state': [None], 'authentication_status': True, 'username': 'my_username'}
[DEBUG <main_page> 62] {'FormSubmitter:Login-Login': False, 'name': 'my_name', 'set': True, 'stutils_init_state': [None], 'authentication_status': True, 'username': 'my_username'}
[DEBUG <config_page> 45] {'name': 'my_name', 'stutils_init_state': [None], 'authentication_status': True, 'username': 'my_username'}
[DEBUG init_state 17] {'big_page_start_date': datetime.date(2024, 2, 9), 'big_page_end_date': datetime.date(2024, 2, 9), 'big_page_prediction_tab': False, 'big_page_model_tab': False, 'big_page_parameters_tab': False, 'big_page_data_tab': False, 'big_page_model_date': datetime.date(2024, 2, 9), 'big_page_model_index_low': 0, 'big_page_model_index_high': 0}
[DEBUG <big_page> 330] {'FormSubmitter:parameter-Submit': False, 'big_page_data_tab': False, 'big_page_model_index_low': 0, 'big_page_end_date': datetime.date(2024, 2, 9), 'big_page_predictions_start_date': datetime.date(2024, 2, 9), 'big_page_model_end_date': datetime.date(2024, 2, 9), 'big_page_model_index_high': 0, 'FormSubmitter:data-Submit': False, 'big_page_model_date': datetime.date(2024, 2, 9), 'big_page_model_tab': False, 'big_page_prediction_tab': False, 'stutils_init_state': [None, 'big_page'], 'FormSubmitter:predictions-Submit': False, 'username': 'my_username', 'authentication_status': True, 'big_page_parameters_tab': False, 'FormSubmitter:model-Submit': False, 'big_page_parameter_start_date': datetime.date(2024, 2, 9), 'big_page_parameter_end_date': datetime.date(2024, 2, 9), 'name': 'my_name', 'big_page_model_start_date': datetime.date(2024, 2, 9), 'big_page_data_end_date': datetime.date(2024, 2, 9), 'big_page_start_date': datetime.date(2024, 2, 9), 'big_page_predictions_end_date': datetime.date(2024, 2, 9), 'big_page_data_start_date': datetime.date(2024, 2, 9)}
[DEBUG <main_page> 62] {'FormSubmitter:parameter-Submit': False, 'big_page_data_tab': False, 'big_page_model_index_low': 0, 'big_page_end_date': datetime.date(2024, 2, 9), 'big_page_predictions_start_date': datetime.date(2024, 2, 9), 'big_page_model_end_date': datetime.date(2024, 2, 9), 'big_page_model_index_high': 0, 'FormSubmitter:data-Submit': False, 'big_page_model_date': datetime.date(2024, 2, 9), 'big_page_model_tab': False, 'big_page_prediction_tab': False, 'FormSubmitter:predictions-Submit': False, 'stutils_init_state': [None, 'big_page'], 'username': 'my_username', 'big_page_parameters_tab': False, 'FormSubmitter:model-Submit': False, 'big_page_parameter_start_date': datetime.date(2024, 2, 9), 'big_page_parameter_end_date': datetime.date(2024, 2, 9), 'name': 'my_name', 'big_page_data_end_date': datetime.date(2024, 2, 9), 'big_page_model_start_date': datetime.date(2024, 2, 9), 'big_page_start_date': datetime.date(2024, 2, 9), 'big_page_predictions_end_date': datetime.date(2024, 2, 9), 'authentication_status': True, 'big_page_data_start_date': datetime.date(2024, 2, 9)}
[DEBUG init_state 17] {'wip_page_start_date': datetime.date(2024, 2, 9), 'wip_page_end_date': datetime.date(2024, 2, 9), 'wip_page_prediction_tab': False, 'wip_page_model_tab': False, 'wip_page_parameters_tab': False, 'wip_page_data_tab': False, 'wip_page_model_date': datetime.date(2024, 2, 9), 'wip_page_model_index_low': 0, 'wip_page_model_index_high': 0}
[DEBUG <wip_page> 89] {'big_page_data_tab': False, 'FormSubmitter:predictions-Submit': False, 'big_page_model_index_low': 0, 'big_page_end_date': datetime.date(2024, 2, 9), 'wip_page_predictions_end_date': datetime.date(2024, 2, 9), 'wip_page_start_date': datetime.date(2024, 2, 9), 'big_page_model_index_high': 0, 'wip_page_parameters_tab': False, 'big_page_model_date': datetime.date(2024, 2, 9), 'big_page_model_tab': False, 'wip_page_model_index_low': 0, 'big_page_prediction_tab': False, 'stutils_init_state': [None, 'big_page', 'wip_page'], 'wip_page_predictions_start_date': datetime.date(2024, 2, 9), 'wip_page_prediction_tab': False, 'username': 'my_username', 'wip_page_model_tab': False, 'big_page_parameters_tab': False, 'wip_page_end_date': datetime.date(2024, 2, 9), 'name': 'my_name', 'wip_page_data_tab': False, 'wip_page_model_date': datetime.date(2024, 2, 9), 'big_page_start_date': datetime.date(2024, 2, 9), 'authentication_status': True, 'wip_page_model_index_high': 0}
[DEBUG init_state 17] {'authentication_status': None, 'name': None}
[DEBUG <main_page> 62] {'name': None, 'username': '', 'stutils_init_state': [None], 'authentication_status': None, 'FormSubmitter:Login-Login': False}

You can clearly see in the last 3 logs that the session state got deleted. Furthermore:

  • I never changed from wip_page to main_page: somehow main_page ran without me visiting the page.
  • I have not written any code that could remove session_state or any variable it stores

Again, this does not happen when I work on my computer. Locally the execution flow works just as I intended, but this is not the case when deploying to community cloud. I have no clue how to fix it or where to start troubleshooting the issue.

Is this a bug? Is there any reason why session_state may reset itself seemingly at random without closing the browser window? What (if any) is the difference between community cloud and a local VSCode environment with a .venv virtual environment? What do I do to get more information on this issue? How can I fix this issue?

I use Streamlit==1.31.0 and streamlit_authenticator==0.3.1

1 Like

Do you experience this all the time?

Yes, I’ve also noticed that it happens many different times even if I don’t interact with the app at all

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.