Snowflake connector error while deploying app on streamlit

Getting this error every morning after I deployed my code on streamlit
not able to figure out what the issue is here


Please help me with this I have to reboot the application every morning

import snowflake.connector
import streamlit as st

@st.experimental_singleton
def init_connection():
    return snowflake.connector.connect(**st.secrets["snowflake"])


conn = init_connection()

# Perform query.
# Uses st.experimental_memo to only rerun when the query changes or after 10 min.


@st.experimental_memo(ttl=600)
def run_query(query):
    with conn.cursor() as cur:
        cur.execute(query)
        return cur.fetchall()

This is the code that I use to run the query and initializes the connector
I also have put my secrets properly in the settings it is running after i reboot the app but next morning it will look like this
Please help!!!

Hi @Prarthit_Shah, welcome to the Streamlit Community! :wave:

Could you please paste the entire traceback? It’s unclear from your screenshot what the actual error is.

If the error you received is ProgrammingError: 390114 (08001): Authentication token has expired. :

When using the Snowflake Connector for Python on Streamlit Cloud, you can add

client_session_keep_alive = "True"

to your Cloud secrets to prevent authentication token expiration (typically occurs when the app is unused for > 4 hours).

When using the app locally, add client_session_keep_alive = "True" to .streamlit/secrets.toml.

This is the entire traceback

File "/home/appuser/venv/lib/python3.9/site-packages/streamlit/script_runner.py", line 354, in _run_script
    exec(code, module.__dict__)
File "/app/recommendation-metrics/app/streamlit_app.py", line 61, in <module>
    recommendation_published_query_result = run_query(
File "/home/appuser/venv/lib/python3.9/site-packages/streamlit/caching/cache_utils.py", line 125, in wrapped_func
    return get_or_create_cached_value()
File "/home/appuser/venv/lib/python3.9/site-packages/streamlit/caching/cache_utils.py", line 117, in get_or_create_cached_value
    return_value = func(*args, **kwargs)
File "/app/recommendation-metrics/app/streamlit_app.py", line 36, in run_query
    cur.execute(query)
File "/home/appuser/venv/lib/python3.9/site-packages/snowflake/connector/cursor.py", line 716, in execute
    ret = self._execute_helper(query, **kwargs)
File "/home/appuser/venv/lib/python3.9/site-packages/snowflake/connector/cursor.py", line 516, in _execute_helper
    ret = self._connection.cmd_query(
File "/home/appuser/venv/lib/python3.9/site-packages/snowflake/connector/connection.py", line 1004, in cmd_query
    ret = self.rest.request(
File "/home/appuser/venv/lib/python3.9/site-packages/snowflake/connector/network.py", line 465, in request
    return self._post_request(
File "/home/appuser/venv/lib/python3.9/site-packages/snowflake/connector/network.py", line 726, in _post_request
    raise ex.cause

I have tried this changes but Did not solve the problem

Also now I am getting another error also

This error is related to DatabaseError
Traceback for the same is below:-

File "/home/appuser/venv/lib/python3.9/site-packages/streamlit/script_runner.py", line 354, in _run_script
    exec(code, module.__dict__)
File "/app/streamlit-test/app/streamlit_app.py", line 48, in <module>
    rows = run_query(RECOMMENDATION_COUNT_QUERY)
File "/home/appuser/venv/lib/python3.9/site-packages/streamlit/caching/cache_utils.py", line 125, in wrapped_func
    return get_or_create_cached_value()
File "/home/appuser/venv/lib/python3.9/site-packages/streamlit/caching/cache_utils.py", line 117, in get_or_create_cached_value
    return_value = func(*args, **kwargs)
File "/app/streamlit-test/app/streamlit_app.py", line 28, in run_query
    with conn.cursor() as cur:
File "/home/appuser/venv/lib/python3.9/site-packages/snowflake/connector/connection.py", line 626, in cursor
    Error.errorhandler_wrapper(
File "/home/appuser/venv/lib/python3.9/site-packages/snowflake/connector/errors.py", line 276, in errorhandler_wrapper
    handed_over = Error.hand_to_other_handler(
File "/home/appuser/venv/lib/python3.9/site-packages/snowflake/connector/errors.py", line 334, in hand_to_other_handler
    connection.errorhandler(connection, cursor, error_class, error_value)
File "/home/appuser/venv/lib/python3.9/site-packages/snowflake/connector/errors.py", line 210, in default_errorhandler
    raise error_class(

I can’t seem to pin point what the error is. Looks like it stems from the following and is unrelated to the authentication token issue. I will defer to the rest of the community for help with this issue.

File "/app/streamlit-test/app/streamlit_app.py", line 48, in <module>
    rows = run_query(RECOMMENDATION_COUNT_QUERY)

Else, I’d suggest submitting an issue to the snowflake-connector-python repo since you’ve encountered multiple errors (ProgrammingError, DatabaseError`).

Can you confirm that the query is indeed running per @st.experimental_memo(ttl=600)? I have seen similar errors pop up if the connection has timed out and a subsequent query gets executed. I am running some test code locally to see if I can recreate in my environment.

Is this implemented in Streamlit Cloud or on a local resource? I started my Streamlit app then went into Snowflake and executed the following select system$abort_session(<session number>); where session number is for the Streamlit app connection to Snowflake. I then executed a query from the app and my error message was almost line for line what has been posted above.

This appears to be some type of time out issue. One thing I just thought of that COULD be the issue, is if overnight, there is no change in the data and basically continuing to receive result from the results cache causes the connection to drop. Don’t know if that is true, just a thought.