In v1.28.0, infinite hang when using st.rerun() maybe due to strange st.button() statefulness

Deployment info: Local deployment on Windows Subsystem for Linux 2 (Ubuntu 22.04.3 LTS, which I fully updated and restarted). Windows is also updated and restarted (OS build 22621.2428).

Today I upgraded Streamlit from 1.27.2 to 1.28.0 and ran into the following problem. Downgrading to earlier versions of Streamlit does not help. Before the upgrade, I had been running Streamlit regularly on my system for almost a year (including upgrades) without issue. The problem started occurring on my original version of Streamlit but it persists even after creating an entire fresh new installation, the details of which are below.

My test.py script is simple:

import streamlit as st

st.write('hello')

if st.button('click me'):
    st.rerun()

As soon as I click the button, the app runs continuously (RUNNING... in the top right) until it dies, after which the following appears in my terminal:

Exception in thread ScriptRunner.scriptThread:
Traceback (most recent call last):
  File "/home/weismanal/programs/miniconda3/envs/conda-streamlit-2023-10-30_2/lib/python3.11/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 534, in _run_script
    exec(code, module.__dict__)
  File "/home/weismanal/notebook/2023-10-30/new_streamlit_installation2/test.py", line 3, in <module>
    st.write('hello')
  File "/home/weismanal/programs/miniconda3/envs/conda-streamlit-2023-10-30_2/lib/python3.11/site-packages/streamlit/runtime/metrics_util.py", line 396, in wrapped_func
    result = non_optional_func(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/weismanal/programs/miniconda3/envs/conda-streamlit-2023-10-30_2/lib/python3.11/site-packages/streamlit/elements/write.py", line 268, in write
    flush_buffer()
  File "/home/weismanal/programs/miniconda3/envs/conda-streamlit-2023-10-30_2/lib/python3.11/site-packages/streamlit/elements/write.py", line 185, in flush_buffer
    self.dg.markdown(
  File "/home/weismanal/programs/miniconda3/envs/conda-streamlit-2023-10-30_2/lib/python3.11/site-packages/streamlit/runtime/metrics_util.py", line 396, in wrapped_func
    result = non_optional_func(*args, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/weismanal/programs/miniconda3/envs/conda-streamlit-2023-10-30_2/lib/python3.11/site-packages/streamlit/elements/markdown.py", line 106, in markdown
    return self.dg._enqueue("markdown", markdown_proto)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/weismanal/programs/miniconda3/envs/conda-streamlit-2023-10-30_2/lib/python3.11/site-packages/streamlit/delta_generator.py", line 532, in _enqueue
    _enqueue_message(msg)
  File "/home/weismanal/programs/miniconda3/envs/conda-streamlit-2023-10-30_2/lib/python3.11/site-packages/streamlit/delta_generator.py", line 873, in _enqueue_message
    ctx.enqueue(msg)
  File "/home/weismanal/programs/miniconda3/envs/conda-streamlit-2023-10-30_2/lib/python3.11/site-packages/streamlit/runtime/scriptrunner/script_run_context.py", line 108, in enqueue
    self._enqueue(msg)
  File "/home/weismanal/programs/miniconda3/envs/conda-streamlit-2023-10-30_2/lib/python3.11/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 321, in _enqueue_forward_msg
    self.on_event.send(
  File "/home/weismanal/programs/miniconda3/envs/conda-streamlit-2023-10-30_2/lib/python3.11/site-packages/blinker/base.py", line 297, in send
    result = receiver(sender, **kwargs)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/weismanal/programs/miniconda3/envs/conda-streamlit-2023-10-30_2/lib/python3.11/site-packages/streamlit/runtime/app_session.py", line 462, in _on_scriptrunner_event
    self._event_loop.call_soon_threadsafe(
  File "/home/weismanal/programs/miniconda3/envs/conda-streamlit-2023-10-30_2/lib/python3.11/asyncio/base_events.py", line 809, in call_soon_threadsafe
    handle = self._call_soon(callback, args, context)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/weismanal/programs/miniconda3/envs/conda-streamlit-2023-10-30_2/lib/python3.11/asyncio/base_events.py", line 781, in _call_soon
    handle = events.Handle(callback, args, self, context)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/weismanal/programs/miniconda3/envs/conda-streamlit-2023-10-30_2/lib/python3.11/asyncio/events.py", line 33, in __init__
    context = contextvars.copy_context()
              ^^^^^^^^^^^^^^^^^^^^^^^^^^
RecursionError: maximum recursion depth exceeded while calling a Python object

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/weismanal/programs/miniconda3/envs/conda-streamlit-2023-10-30_2/lib/python3.11/threading.py", line 1045, in _bootstrap_inner
    self.run()
  File "/home/weismanal/programs/miniconda3/envs/conda-streamlit-2023-10-30_2/lib/python3.11/threading.py", line 982, in run
    self._target(*self._args, **self._kwargs)
  File "/home/weismanal/programs/miniconda3/envs/conda-streamlit-2023-10-30_2/lib/python3.11/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 286, in _run_script_thread
    self._run_script(request.rerun_data)
  File "/home/weismanal/programs/miniconda3/envs/conda-streamlit-2023-10-30_2/lib/python3.11/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 588, in _run_script
    self._run_script(rerun_exception_data)
  File "/home/weismanal/programs/miniconda3/envs/conda-streamlit-2023-10-30_2/lib/python3.11/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 588, in _run_script
    self._run_script(rerun_exception_data)
  File "/home/weismanal/programs/miniconda3/envs/conda-streamlit-2023-10-30_2/lib/python3.11/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 588, in _run_script
    self._run_script(rerun_exception_data)
  [Previous line repeated 976 more times]
  File "/home/weismanal/programs/miniconda3/envs/conda-streamlit-2023-10-30_2/lib/python3.11/site-packages/streamlit/runtime/scriptrunner/script_runner.py", line 549, in _run_script
    handle_uncaught_app_exception(uncaught_exception)
  File "/home/weismanal/programs/miniconda3/envs/conda-streamlit-2023-10-30_2/lib/python3.11/site-packages/streamlit/error_util.py", line 95, in handle_uncaught_app_exception
    _LOGGER.warning("Uncaught app exception", exc_info=ex)
  File "/home/weismanal/programs/miniconda3/envs/conda-streamlit-2023-10-30_2/lib/python3.11/logging/__init__.py", line 1501, in warning
    self._log(WARNING, msg, args, **kwargs)
  File "/home/weismanal/programs/miniconda3/envs/conda-streamlit-2023-10-30_2/lib/python3.11/logging/__init__.py", line 1634, in _log
    self.handle(record)
  File "/home/weismanal/programs/miniconda3/envs/conda-streamlit-2023-10-30_2/lib/python3.11/logging/__init__.py", line 1644, in handle
    self.callHandlers(record)
  File "/home/weismanal/programs/miniconda3/envs/conda-streamlit-2023-10-30_2/lib/python3.11/logging/__init__.py", line 1706, in callHandlers
    hdlr.handle(record)
  File "/home/weismanal/programs/miniconda3/envs/conda-streamlit-2023-10-30_2/lib/python3.11/logging/__init__.py", line 978, in handle
    self.emit(record)
  File "/home/weismanal/programs/miniconda3/envs/conda-streamlit-2023-10-30_2/lib/python3.11/logging/__init__.py", line 1110, in emit
    msg = self.format(record)
          ^^^^^^^^^^^^^^^^^^^
  File "/home/weismanal/programs/miniconda3/envs/conda-streamlit-2023-10-30_2/lib/python3.11/logging/__init__.py", line 953, in format
    return fmt.format(record)
           ^^^^^^^^^^^^^^^^^^
  File "/home/weismanal/programs/miniconda3/envs/conda-streamlit-2023-10-30_2/lib/python3.11/logging/__init__.py", line 695, in format
    record.exc_text = self.formatException(record.exc_info)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/weismanal/programs/miniconda3/envs/conda-streamlit-2023-10-30_2/lib/python3.11/logging/__init__.py", line 645, in formatException
    traceback.print_exception(ei[0], ei[1], tb, None, sio)
  File "/home/weismanal/programs/miniconda3/envs/conda-streamlit-2023-10-30_2/lib/python3.11/traceback.py", line 124, in print_exception
    te = TracebackException(type(value), value, tb, limit=limit, compact=True)
         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/weismanal/programs/miniconda3/envs/conda-streamlit-2023-10-30_2/lib/python3.11/traceback.py", line 702, in __init__
    self.stack = StackSummary._extract_from_extended_frame_gen(
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/weismanal/programs/miniconda3/envs/conda-streamlit-2023-10-30_2/lib/python3.11/traceback.py", line 416, in _extract_from_extended_frame_gen
    for f, (lineno, end_lineno, colno, end_colno) in frame_gen:
  File "/home/weismanal/programs/miniconda3/envs/conda-streamlit-2023-10-30_2/lib/python3.11/traceback.py", line 353, in _walk_tb_with_full_positions
    positions = _get_code_position(tb.tb_frame.f_code, tb.tb_lasti)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/weismanal/programs/miniconda3/envs/conda-streamlit-2023-10-30_2/lib/python3.11/traceback.py", line 364, in _get_code_position
    if instruction_index < 0:
       ^^^^^^^^^^^^^^^^^^^^^
RecursionError: maximum recursion depth exceeded in comparison

Everything works just fine if I comment out the st.rerun().

Here is my environment:

(conda-streamlit-2023-10-30_2) weismanal@XXXX:~/notebook/2023-10-30/new_streamlit_installation2$ conda env export
name: conda-streamlit-2023-10-30_2
channels:
  - conda-forge
  - defaults
dependencies:
  - _libgcc_mutex=0.1=conda_forge
  - _openmp_mutex=4.5=2_gnu
  - bzip2=1.0.8=h7f98852_4
  - ca-certificates=2023.7.22=hbcca054_0
  - ld_impl_linux-64=2.40=h41732ed_0
  - libexpat=2.5.0=hcb278e6_1
  - libffi=3.4.2=h7f98852_5
  - libgcc-ng=13.2.0=h807b86a_2
  - libgomp=13.2.0=h807b86a_2
  - libnsl=2.0.1=hd590300_0
  - libsqlite=3.43.2=h2797004_0
  - libuuid=2.38.1=h0b41bf4_0
  - libzlib=1.2.13=hd590300_5
  - ncurses=6.4=hcb278e6_0
  - openssl=3.1.4=hd590300_0
  - pip=23.3.1=pyhd8ed1ab_0
  - python=3.11.6=hab00c5b_0_cpython
  - readline=8.2=h8228510_1
  - setuptools=68.2.2=pyhd8ed1ab_0
  - tk=8.6.13=h2797004_0
  - wheel=0.41.3=pyhd8ed1ab_0
  - xz=5.2.6=h166bdaf_0
  - pip:
      - altair==5.1.2
      - attrs==23.1.0
      - blinker==1.6.3
      - cachetools==5.3.2
      - certifi==2023.7.22
      - charset-normalizer==3.3.1
      - click==8.1.7
      - gitdb==4.0.11
      - gitpython==3.1.40
      - idna==3.4
      - importlib-metadata==6.8.0
      - jinja2==3.1.2
      - jsonschema==4.19.2
      - jsonschema-specifications==2023.7.1
      - markdown-it-py==3.0.0
      - markupsafe==2.1.3
      - mdurl==0.1.2
      - numpy==1.26.1
      - packaging==23.2
      - pandas==2.1.2
      - pillow==10.1.0
      - protobuf==4.24.4
      - pyarrow==13.0.0
      - pydeck==0.8.1b0
      - pygments==2.16.1
      - python-dateutil==2.8.2
      - pytz==2023.3.post1
      - referencing==0.30.2
      - requests==2.31.0
      - rich==13.6.0
      - rpds-py==0.10.6
      - six==1.16.0
      - smmap==5.0.1
      - streamlit==1.28.0
      - tenacity==8.2.3
      - toml==0.10.2
      - toolz==0.12.0
      - tornado==6.3.3
      - typing-extensions==4.8.0
      - tzdata==2023.3
      - tzlocal==5.2
      - urllib3==2.0.7
      - validators==0.22.0
      - watchdog==3.0.0
      - zipp==3.17.0
prefix: /home/weismanal/programs/miniconda3/envs/conda-streamlit-2023-10-30_2

Any advice would be greatly appreciated. Thank you!

1 Like

Update: 1.27.0 works, 1.28.0 fails.

My feeling is something with the statefulness of st.button() changed perhaps? What’s activated by the button is supposed to only run once, but if it’s instead running more than once (i.e., st.button() continues to be activated after st.rerun() is called), then the script just keeps rerunning indefinitely.

By the way, manually rerunning by hitting “r” works perfectly fine, which leads me to believe that maybe st.button() is the culprit.

@mathcatsand or any other members of the Streamlit team?

Thanks,
Andrew

1 Like

Hi @andrew-weisman ,

thanks for making this post. We are aware of this problem and actively trying to fix it.
Here is the github issue post:

We will attempt to release a patch for this.

Ah, great, thanks very much for your reply @willhuang!

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