question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Server shutdown fails to exit while processing infinite loop

See original GitHub issue

Summary

Python process does not exit when shutting down the server while processing an infinite loop in a script

Steps to reproduce

Code snippet: (using a recent SessionState prototype, before I fixed the implementation)

import streamlit as st

state = st.get_state()
st.write("printed something")
st.write("foobar" in state) # this line infinite looped before I fixed the implementation

If applicable, please provide the steps we should take to reproduce the bug:

  1. Run the above snippet against the previous state prototype (I ran the react server as a separate process from the dev install of that commit, rather than running them together via a wheel, don’t know if that affects this)
  2. C-c in the shell running the python server

Expected behavior:

Python server stops, process exits and control returns to my shell

Actual behavior:

Logs indicate server goes into the STOPPING state and then almost immediately the STOPPED state, but after 20 seconds it still has not exited the process. Sending SIGTERM repeatedly afterwards fails to cause the process to exit, and SIGKILL was required to regain control.

Is this a regression?

Unknown

Debug info

  • Streamlit version: Session state prototype, as linked above, development install
  • Python version: 3.8.6
  • Using Conda? PipEnv? PyEnv? Pex?: Pipenv
  • OS version: Arch Linux
  • Shell version: Zsh 5.8

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:1
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

2reactions
kmcgradycommented, Jul 1, 2021

@robertmartin8 I’ve been thinking about this, and I came up with an idea on my personal time that might help. Essentially, I implemented a custom component that auto re-runs the app. This avoids the need of an infinite loop in the script. This improves the developer experience (shutting down while an infinite loop is running) while easing resources on the server with a tiny tradeoff from the user.

https://github.com/kmcgrady/streamlit-autorefresh

Here’s a trivial example.

import streamlit as st
from streamlit_autorefresh import st_autorefresh

# Run the autorefresh about every 2000 milliseconds (2 seconds) and stop
# after it's been refreshed 100 times.
count = st_autorefresh(interval=2000, limit=100, key="fizzbuzzcounter")

# The function returns a counter for number of refreshes. This allows the
# ability to make special requests at different intervals based on the count
if count == 0:
    st.write("Count is zero")
elif count % 3 == 0 and count % 5 == 0:
    st.write("FizzBuzz")
elif count % 3 == 0:
    st.write("Fizz")
elif count % 5 == 0:
    st.write("Buzz")
else:
    st.write(f"Count: {count}")

Your use case suggests this could work and may even simplify your work. Given the code structure, the new script would be:

import streamlit as st
from streamlit_autorefresh import st_autorefresh
from streamlit import caching
from binance import Client
client = Client()

# update every 5 mins
st_autorefresh(interval=5 * 60 * 1000, key="dataframerefresh")

@st.cache(ttl=310, allow_output_mutation=True)
def get_data(timestamp=None)
    client.get(...)
    return df

caching.clear_cache()  # not sure if needed
timestamp = dt.utcnow()    
st.dataframe(get_data(timestamp))

The only use case that I think this would hurt is one with complex UI implemented before the infinite loop. That being said, proper caching could solve that challenge.

Let me know if it helps or not. It’s a simple component so feedback is appreciated.

0reactions
AnOctopuscommented, Nov 8, 2021

@schaumb That approach seems promising. Could you open a PR for it?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Stopping an infinite loop on a remote server PHP
You might want to contact your hosting service and ask them to kill your script. Most likely you don't have execute-access or ssh-access....
Read more >
What happens if I run an infinite loop on a server? - Quora
In C, and infinite loop is easy : · That is an infinite loop - and it will run until you (or your...
Read more >
Bug #551130 “[SRU] infinite loop in /etc/init/mysql.conf if mysq...”
The file /etc/init/mysql.conf will go into an infinite loop waiting for mysqladmin to succeed if the mysqld daemon is failing.
Read more >
Stopping and Correcting Endless Loops in Business Processes
Diagnosing The Problem. From Websphere Process Server Version 7.0.0.2 onwards the following warning can be used to help identify where the loop ......
Read more >
Bash Infinite Loop Examples - nixCraft
How do I write an infinite loop in Bash script under Linux or UNIX like operating ... Using case statement to exit infinite...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found