How to save and restore session so it persist after shutting down

python 3.11.3
Streamlit latest

config.toml

[runner]
enforceSerializableSessionState = true

I’ve been trying to use this session_persistance.py

import os
import pickle

import streamlit as st


def save_session_state():
    try:
        with open('session_state.pkl', 'wb') as f:
            print("saving session_state", st.session_state)
            pickle.dump(st.session_state, f)
    except Exception as e:
        print("Error during saving session state:", e)


def load_session_state():
    if os.path.exists('session_state.pkl'):
        try:
            with open('session_state.pkl', 'rb') as f:
                loaded_state = pickle.load(f)
                print("Loaded session state:", loaded_state)
                return loaded_state
        except Exception as e:
            print("Error during loading session state:", e)
    else:
        print("session_state.pkl not found")
    return None

And load on my main.py

loaded_state = load_session_state()
if loaded_state is not None:
    st.session_state.update(loaded_state)

First thing after page config

And adding save_session_state() at the bottom of every page.

But it doesn’t work, when it comes to load it’s empty…

What is the way to achieve state persistence across reboots?