Frustrated: Streamlit frontend disconnects from apps with long-running processes

I have a streamlit app (built in docker, deployed to heroku) that formulates an optimization (a linear program) and then sends the schedule to a gurobi server. However, the process takes a while, and thus for a 20-year optimization at the hourly level, I chunk it into years (8760 data points for a year, x20 years).

Basically, once the inputs are set by the user and saved as state variables a la Joel Grus’ Game State hacks , the process is:

  1. Formulate inputs and constraints to optimization for 2021 →
  2. send to gurobi →
  3. get back optimal solution from gurobi as a dataframe->
  4. display on Streamlit app “Optimal Solution for 2021 Complete! Moving on to 2022…”
  5. Repeat for next iteration, for 20 iterations.
  6. Concatenante all 20 dataframes from the process and display some summary results and some download and “Send to database” buttons for the results.

Each iteration takes about a minute. However, I can only make it to about the 10th iteration (sometimes less, sometimes more) of that before the streamlit frontend “disconnects” from the running loop process of the app and resets all of my state variables, etc, like I had just reloaded the page. The background app process still continues (it’s still looping through the iterations - it doesn’t know streamlit got disconnected), but there is no way to reconnect to the running process again. Without streamlit, this process is fine to run locally with scripts, but with streamlit the frontend is so unreliable as to be unusable for this app. Nothing more frustrating than to get nearly done with a process that took 20 minutes and then it resets itself, forcing the user to start the process over.

I’m really not sure what to try at this point, other than build something much more robust like a flask app that sends these jobs to a job container, which then sends back the result when done. Running the optimization on the same computer as the frontend seems like a fool’s errand.

Anyone have tips to keep a streamlit frontend connected to a long-running process without resetting?

Hi @Kladar

That sounds super frustrating. Sorry about that :slightly_frowning_face:

In my experience those symptoms sound a lot like there’s some unexpected HTTP timeout going on on the host side. The only thing that gives me pause here is that the default HTTP timeout is ~30s, so usually that’s how long your app would take between disconnect-reconnect cycles. But in your case it sounds like these reconnect-disconnect cycles are much longer…

Either way, to help debug it would be great to get a couple of things:

  1. If you look at your server logs, do you see any errors?
  2. On the browser-side, if you open your browser’s dev tools, do you see anything either on the JS console or in the Network tab?

If you don’t know what to look for, or just want a second pair of eyes, feel free to post the logs from (1) and a HAR file from (2) here so I can help debug.


Thanks for the response, sorry about the late reply. I’ve recreated the error and made a screen recording, and I did so running the app locally to avoid the confounding variable of the heroku-based deployment, which I played up too much in the initial post. It actually is more likely to fail running locally, though I’m not sure why. For a 20 year run (~10 minutes), it resets the app about 60% of the time somewhere along in the process (I had to record a couple times to show a failure, as the first couple examples made it through the analysis successfully). Link to recording

I have a HAR file for a later failure (not the one in the video) but the forum won’t let me attach it. Should I convert it to some other file type? We don’t need the Heroku logs because since I ran it locally. And now that I think about it, it could be a memory thing - since python is pretty bad at memory management and Chrome is a RAM hog, perhaps running it several times successfully caused it to fail? Though I CTRL+C stopped the streamlit app and started it again each time and my memory usage on my computer seemed pretty static. I’m at a loss. ::


Hey @Kladar , thanks for the video!

After watching the video I tried reproducing the issue locally using a toy example, but I’m just not able to :confused:

This is the toy example I tried:

import streamlit as st
import time
import datetime

# Long-running app example

start_time =

for i in range(20):
  "Loop number:", i
  "Ellapsed time:", - start_time

  # Tried with
  # time.sleep(10)
  # ...but can't repro

  # Tried with
  # time.sleep(60)
  # ...but can't repro

At least this rules out a few hypotheses I had about websocket keepalive failures…

To help debug:

  1. Can you put the HAR file in this private Google Drive folder I shared with you?
  2. Can you repro the bug with streamlit run --logger.level=debug 2> bug.log and upload the log from bug.log to Google Drive?
  3. Can you try using a Chrome profile that has no Chrome extensions installed?
1 Like

Thanks for the help. I’ve uploaded the .HAR file and the bug.log from the most recent run to that folder (at least I believe it’s that folder - let me know if you can’t see them). I ran it again locally in a chrome profile with no extensions and it failed at about the same point as in the video.

I believe the failure happens around line 2977 of the log. You can see ~ line 2958 “optimization is solved with status 2” which is good, that means that year completed (Year 1 of 20), and is saving the results as a dataframe (see the pandas warning about setting a value on a slice from a df blah blah etc). Then on line 2977 we get “Server state: State.ONE_OR_MORE_BROWSERS_CONNECTED → State.NO_BROWSERS_CONNECTED” which I believe is our issue.

I will attempt to run it to completion and post what a successful log file looks like as well, as there is a lot going on in that log.

(Note, neither here nor there but I also upgraded to streamlit 0.8.0 before that run and the log files attest to me being excited about using secrets! :upside_down_face:)

Thanks, this is super helpful!

I believe the failure happens around line 2977 of the log.

Yup, that’s right.

The logs around that line make me think that the problem is on the browser side. We basically see the websocket disconnecting the same way it would if the browser had terminated the connection normally. Very odd!

Unfortunately, the data in the HAR file starts a few seconds after the websocket first connects, which means the websocket connection (and, more importantly, its disconnection!) isn’t present in the data :frowning_face:

Can you try capturing a HAR file using the method below?

  1. Start your Streamlit server streamlit run
  2. Open a browser at localhost:8501
  3. Open the dev tools in the browser > Network > “preserve log”
    Screenshot from 2021-04-22 16-54-20
  4. Reload the browser page with Ctrl-R (or Cmd-R)
  5. Now save the HAR file and upload to our shared Drive link

Also: can you copy/paste into a file whatever you see in the Console tab in the dev tools?

If none of this bears fruit, our next best step is to set up a video call and see what’s up!

1 Like

Okay, @thiago I’ve hopefully captured everything in a couple newly uploaded .HAR files from similarly failed runs.
There is one from just recording after the app has started, and one from a full refresh, and both gave similar errors when the app reset itself.

In failed_run_attempt2, there is a 101 code on ~line 71, that followed 304s at line 61 and 70, that all seemed to be generated just as the app reset itself.

The file failed_run_from_full_refresh is the similar, but started a bit “further back” from a Cmd-R Refresh point. We see the 304 and 101 code combo around line ~131. Hopefully that helps disentangle what is normal process vs what is failure.

And here is the full dev tools console capture for failed_run_attempt2 (also uploaded to the shared drive). Thanks for working through this with me!

Ok, I’m still stumped. I’ll reach out by email and try to set up a live debug with you.

(Will post any findings here for anyone who’s watching!)


Awesome, thanks for the help.

Update: we still don’t know what’s going on, but we were able to find a possible solution.

In Streamlit’s sever/, change this:

    "websocket_ping_interval": 20,  # Ping every 20s to keep WS alive.

to this

    "websocket_ping_interval": 1,  # Ping every 1s to keep WS alive.

In @Kladar’s case, this appears to solve the problem, and I don’t expect it to have any noticeable negative impact.

I’ll talk to our eng team to see if it makes sense to make this the default, as it’s very possible that I’m missing something obvious :laughing:


@thiago curious for status on this?

I see the 0.82 garbage collection which is much appreciated, though assuming that is not the fix here.

@thiago fortnightly bump

Hello @thiago !
I’m having exactly the same issue as @Kladar. I have a model that takes almost an hour to run and sometimes it just disconnects from the backend process while I see the console is still running but the front is just reset to default. Is there a more friendly solution already for this?

Thanks in advance!

Hey @Kladar and @avila196 - I’m taking a look at this now. It’s obviously a trivial change; the question is, will it have other consequences that we’re not considering?

I’ll do a bit more research - but the plan right now is to merge this change in the next 2 weeks.

1 Like

Update here?

Hey @Kladar - I made the change in Merge pull request #3464 from tconkling/tim/DecreaseWebSocketPing · streamlit/streamlit@20a636f · GitHub

And it landed in Streamlit release 0.84.0


Amazing thanks! I will check it out.