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
Percy2y ago
Project ID: 87705bfd-1f41-4d43-8e4d-ed1380fbc20e
0us5ama
0us5amaOP2y ago
87705bfd-1f41-4d43-8e4d-ed1380fbc20e
Brody
Brody2y ago
is this a post request?
0us5ama
0us5amaOP2y ago
it's an event stream
Brody
Brody2y ago
tell me the request method please
0us5ama
0us5amaOP2y ago
it's like a websocket
Brody
Brody2y ago
tell me the request method please
0us5ama
0us5amaOP2y 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
Brody2y ago
it's a get request
thank you
0us5ama
0us5amaOP2y 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
Brody2y ago
i am familiar
0us5ama
0us5amaOP2y ago
in any case I should have answered directly
Brody
Brody2y 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
0us5amaOP2y ago
locally? okey
Brody
Brody2y ago
it sends a timestamp every second it may not follow EventStream spec, but it is still sse
0us5ama
0us5amaOP2y 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
Brody2y ago
id have to guess it would more so be with your gunicorn and gevent setup
0us5ama
0us5amaOP2y 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
Brody2y ago
can you show me the headers tab of this?
0us5ama
0us5amaOP2y ago
Brody
Brody2y ago
is that all of the response headers?
0us5ama
0us5amaOP2y ago
yes
Brody
Brody2y ago
show me the same screenshot but to the server running locally
0us5ama
0us5amaOP2y ago
ok one minute
0us5ama
0us5amaOP2y ago
Brody
Brody2y 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
0us5amaOP2y ago
hahahaha
Brody
Brody2y ago
interesting, fix that and then try it out on railway
0us5ama
0us5amaOP2y ago
I added it to my github repo and redeployed the app but the connection keep-alive was not added
Brody
Brody2y ago
was the connection still closed after 5 minutes
0us5ama
0us5amaOP2y ago
Yes the same issue persists
Brody
Brody2y ago
well keep playing around, im sure youll get it
Want results from more Discord servers?
Add your server