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.

3 Likes

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. ::

@thiago

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 = datetime.datetime.now()

for i in range(20):
  "---"
  "Loop number:", i
  "Ellapsed time:", datetime.datetime.now() - 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 script_name.py 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 foo.py
  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!)

3 Likes

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/server.py, 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:

5 Likes

@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!

1 Like

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

2 Likes

Amazing thanks! I will check it out.

I experience exactly the same problen as @avila196 with streamlit==1.8.0
Any solution ?

2 Likes

It could be a memory leak. We havenā€™t had the problem since the websocket ping patch in 0.84, but we also havenā€™t upgraded to 1.8 yet. Saw this recently, maybe it will help: 3 steps to fix app memory leaks