streamlit: st.rerun Fails When Called Inside a try-except Block in version 1.37

Checklist

  • I have searched the existing issues for similar issues.
  • I added a very descriptive title to this issue.
  • I have provided sufficient information below to help reproduce this issue.

Summary

In the new version of Streamlit (1.37), it appears that calling st.rerun inside a try-except block results in a failure. This issue can be demonstrated using a decorator as an example, where the decorator includes a try-except block and wraps a function that calls st.rerun. This behavior does not occur when st.rerun is called directly within a function without such error handling. This issue appeared after the 1.37 release

Reproducible Code Example

import streamlit as st
import logging

def error_logging_decorator(func):
    def wrapper(*args, **kwargs):
        try:
            return func(*args, **kwargs)
        except Exception as e:
            logging.error(f"Error in function {func.__name__}: {e}")
            st.error(f"An error occurred: {e}")
            raise e  # Re-raise the exception
    return wrapper
@error_logging_decorator
def decorated_function():
    if st.button("Refresh"):
        st.rerun()

def plain_function():
    if st.button("Refresh2"):
        st.rerun()

Steps To Reproduce

  1. run an st.rerun inside a try except block

Expected Behavior

Both the decorated_function and plain_function should trigger a rerun when the “Refresh” button is pressed.

Current Behavior

decorated_function raises an exception and fails to rerun. plain_function works as expected and triggers a rerun

Is this a regression?

  • Yes, this used to work in a previous version.

Debug info

  • Streamlit version: 1.37
  • Python version: 3.10
  • Operating System: macOS
  • Browser: chrome

Additional Information

#Logs:

2024-07-26 13:10:22,773 - admin - ERROR - Error in function decorated_function: RerunData(page_script_hash=‘d42b4824a51958adffc95194dcc95934’, is_fragment_scoped_rerun=True) streamlit.runtime.scriptrunner.exceptions.RerunException: RerunData(page_script_hash=‘d42b4824a51958adffc95194dcc95934’, is_fragment_scoped_rerun=True)

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 14
  • Comments: 17 (6 by maintainers)

Commits related to this issue

Most upvoted comments

If this issue affects you, please react with a 👍 (thumbs up emoji) to the initial post.

Your feedback helps us prioritize which bugs to investigate and address first.

Visits

We have discussed this and are going to revert the change of the Exception-type back. Together with some other bug fixes we might issue a patch release later this week. Thanks for the analysis in this thread and the discussion, really appreciate it! And sorry for the inconvenience 🙂

@raethlein good news ! thanks you 😃

@raethlein amazing news! Thank you for resolving it so quickly!

@raethlein Cool, I think if we do change it back we should add a unit test to prevent a future similar regression.

Ah.

@raethlein, https://github.com/streamlit/streamlit/commit/485ec8d64a1b2c6b6662ad140d99447a185d3bb2 changed ScriptControlException type from a BaseException to an Exception between 1.36 and 1.37, which is causing the reported behavior change by the users. I can’t see a good reason why it should be Exception, as it’s functionally similar to a KeyboardInterrupt of sorts, which inherits from BaseException.

@Panosero the workaround here is such:

import streamlit as st
# import streamlit_qs as stqs
import streamlit.runtime.scriptrunner.exceptions
import time

def counter():
    for x in range(3):
        st.write(x)
        time.sleep(1)
    st.rerun()


if st.button("notry"):
    counter()

if st.button("try"):
    try:
        counter()
    except streamlit.runtime.scriptrunner.exceptions.ScriptControlException:
        raise
    except Exception as exc:
        st.error(exc)
        # logging stuffs
        time.sleep(10)
        raise

The rerun exception still has to propagate up the call stack, but this shortcircuits the exception handling behavior when the exception is a ScriptControlException.