R
Railway17mo ago
0us5ama

SSE connections timeout each 5 minutes

I have deployed a flask sse based app into railway using a dockerfile and gevent. When I check the Networking tab on the inspection window in my browser I see the connection restarting each 5 minutes which is not the case when I was developping my app locally. This issue leads after some days of redeploying the app to have a stream timeout error and stop giving any sse connection until snapshot restarted.
32 Replies
Percy
Percy17mo ago
Project ID: 87705bfd-1f41-4d43-8e4d-ed1380fbc20e
0us5ama
0us5ama17mo ago
87705bfd-1f41-4d43-8e4d-ed1380fbc20e
Brody
Brody17mo ago
is this a post request?
0us5ama
0us5ama17mo ago
it's an event stream
Brody
Brody17mo ago
tell me the request method please
0us5ama
0us5ama17mo ago
it's like a websocket
Brody
Brody17mo ago
tell me the request method please
0us5ama
0us5ama17mo ago
okey sorry it's a get request to the sse endpoint which makes the connection with the server and keeps it alive until the client aborts
Brody
Brody17mo ago
it's a get request
thank you
0us5ama
0us5ama17mo ago
I was just confused if you were familiar with the topic. This is my fault sorry I didn't answer you first straight forwardly
Brody
Brody17mo ago
i am familiar
0us5ama
0us5ama17mo ago
in any case I should have answered directly
Brody
Brody17mo ago
dont worry about it so far, i have been able to hold a sse connection open done with a get request, time is sitting at 6 minutes you can try it for yourself https://utilities.up.railway.app/sse
0us5ama
0us5ama17mo ago
locally? okey
Brody
Brody17mo ago
it sends a timestamp every second it may not follow EventStream spec, but it is still sse
0us5ama
0us5ama17mo ago
this is the part of my code responsible for sse. Is there anything here that maybe causing the connection restarting when hosted in railway?
@app.get("/sse")
def sse():
if "text/event-stream" not in request.accept_mimetypes:
abort(400)
def send_events():
nonlocal variable
while True:
event_binary = b''
if condition:
data = ...
_event = ...
event = ServerSentEvent(data, _event)
event_binary = event.encode()
yield event_binary
time.sleep(0.1)

response = Response(
send_events(),
mimetype="text/event-stream",
)
response.timeout = None
return response
@app.get("/sse")
def sse():
if "text/event-stream" not in request.accept_mimetypes:
abort(400)
def send_events():
nonlocal variable
while True:
event_binary = b''
if condition:
data = ...
_event = ...
event = ServerSentEvent(data, _event)
event_binary = event.encode()
yield event_binary
time.sleep(0.1)

response = Response(
send_events(),
mimetype="text/event-stream",
)
response.timeout = None
return response
maybe this if "text/event-stream" not in request.accept_mimetypes: abort(400)?
Brody
Brody17mo ago
id have to guess it would more so be with your gunicorn and gevent setup
0us5ama
0us5ama17mo ago
yeah that's highly probably since I didn't use it in the dev process. "gunicorn -b 0.0.0.0:8000 main:app --worker-class gevent" this is my startup command
Brody
Brody17mo ago
can you show me the headers tab of this?
0us5ama
0us5ama17mo ago
Brody
Brody17mo ago
is that all of the response headers?
0us5ama
0us5ama17mo ago
yes
Brody
Brody17mo ago
show me the same screenshot but to the server running locally
0us5ama
0us5ama17mo ago
ok one minute
0us5ama
0us5ama17mo ago
Brody
Brody17mo ago
connection: close send Connection: Keep-Alive instead, the demo app sends keep alive dont know if thats your issue but worth a shot
0us5ama
0us5ama17mo ago
hahahaha
Brody
Brody17mo ago
interesting, fix that and then try it out on railway
0us5ama
0us5ama17mo ago
I added it to my github repo and redeployed the app but the connection keep-alive was not added
Brody
Brody17mo ago
was the connection still closed after 5 minutes
0us5ama
0us5ama17mo ago
Yes the same issue persists
Brody
Brody17mo ago
well keep playing around, im sure youll get it
Want results from more Discord servers?
Add your server