Flask WebSockets Invalid session...
I modified my Python Flask server to use socket.io-client and am seeing errors logged after deploy. The service remains running but the server is not responding to requests properly... Any help is much appreciated!
Invalid session 8nIFLl8Lm_WsCd_2AAAA (further occurrences of this error will be logged with level INFO)
[2023-05-15 13:02:53 +0000] [10] [ERROR] Socket error processing request.
Traceback (most recent call last):
File "/opt/venv/lib/python3.8/site-packages/gunicorn/workers/sync.py", line 134, in handle
self.handle_request(listener, req, client, addr)
File "/opt/venv/lib/python3.8/site-packages/gunicorn/workers/sync.py", line 190, in handle_request
util.reraise(*sys.exc_info())
File "/opt/venv/lib/python3.8/site-packages/gunicorn/util.py", line 625, in reraise
raise value
File "/opt/venv/lib/python3.8/site-packages/gunicorn/workers/sync.py", line 181, in handle_request
resp.write(item)
File "/opt/venv/lib/python3.8/site-packages/gunicorn/http/wsgi.py", line 326, in write
self.send_headers()
File "/opt/venv/lib/python3.8/site-packages/gunicorn/http/wsgi.py", line 322, in send_headers
util.write(self.sock, util.to_bytestring(header_str, "latin-1"))
File "/opt/venv/lib/python3.8/site-packages/gunicorn/util.py", line 286, in write
sock.sendall(data)
OSError: [Errno 9] Bad file descriptor
43 Replies
Project ID:
a686cf21-bbfd-4ddb-9e0c-5c11cd434e88
a686cf21-bbfd-4ddb-9e0c-5c11cd434e88
the problem suggests that you are sending data to a socket that doesnt exist
and it cant know the file descriptor because the socket file doesnt exist
websockets module works for me thats the only thing i can tell
also use the PORT environment variable if you didnt set it in the service settings to something else cause if you didnt, it automatically makes ssl work for you without anything and it your subdomain will work
Did you have to install any package like eventlet?
Also do I need to set up the PORT environment variable? Sorry I did not get the last part..
want the subdomain?
no but it should still work, websockets works
if you want the subdomain, dont set it in service env variables
instead
import os
and set port to os.environ['PORT']
it will automatically forward it to
https://yoursubdomain.up.railway.app
and on 443 port
for websockets you need to use
wss:// tho
(wss:// for with ssl
ws:// without)
Sorry I am still learning about this and can't follow...
What do you mean by want the subdomain?
subdomain is the part of the url
you know
subdomain.domain.com
or something
thats what you type in a browser
subdomain.subdomain.domain.com
the main domain is the last one
you need to pay for domains
but for subdomains you can just create a directoryon the machine that is hosting the web app(depends on your software)
Since I am hosting the service on Railway, I am assuming my subdomain is
flask-production-bcb7.up.railway.app
Is that correct?
where you found that
but it seems correct
In the service setting
change it to what you want
yes
but for custom domain you need to use your own so dont click on that button
if you dont have a domain bought
Oh I don't want to use custom domain yet
and do I need to set this URL anywhere?
read the websockets documentation if you want to use it
no
only when you connect
I have this in my frontend React to create the socket
const newSocket = socketIOClient(process.env.REACT_APP_SERVER_URL, {
query: { token }
});
setSocket(newSocket);
and the env variable is just the https:// subdomain
I was able to talk to my server fine until I added websocket
@ImLunaHey can probably help you better if he has time
i am not so much into web stuff, i never used react or web frameworks, dont ask me
she*
i checked your twitch and you have a male voice, are you transgender or smth? am i wrong?
doesnt matter what my voice sounds like... there are loads of cis/trans men/women who have deep/high voices. if someone corrects their pronouns just leave it at that.
ok sorry, i didnt know
gonna have to provide more info than that. got a link to a repo or a code sample?
The errors states you're using a closed socket.
Thanks for helping, to both of you!
Let me post the repo here
GitHub
GitHub - arthurjis/web_chat_base at add_basic_chatbot
Contribute to arthurjis/web_chat_base development by creating an account on GitHub.
So in this repo, I have a React app and Flask server
okay, and what did you do before the error occurred? Started the app? went to a specific endpoint?
After start_chat in ln75
in main.py
not seeing anything that's sticking out.
does this happen when you first start the app or when you connect?
If I redeploy the service and do not execute this code in App.js in React, seems like things are fine
// Initialize socket connection here
const newSocket = socketIOClient(process.env.REACT_APP_SERVER_URL, {
query: { token }
});
setSocket(newSocket);
Once I create the Socket, server starts to log the error message
did you mean to use socketio and not regular websockets?
theyre not the same thing
Hmmm. I am totally new to this...
yeah theyre not the same thing, that's likely the issue here.
Stack Overflow
Differences between socket.io and websockets
What are the differences between socket.io and websockets in
node.js?
Are they both server push technologies?
The only differences I felt was,
socket.io allowed me to send/emit messages by
You cannot just use a socketio client to connect to a standard websocket server.
But I think both the React and Flask server are socket.io?
Could you help me check on the main.py script
hmm, okay you are using flask_socketio for the server so that might not be the issue.
personally id use actual websockets instead as socketio adds a whole layer of unknowns
sorry i couldnt be of more help
ok. Let me try switch
So websocket might be more reliable?
yes, it is a lot better
yeah, the issue itself is the socket is not connected and you're trying to send it data.
it handles pings by default
and stuff
its very good and nice
I see. I would try that!
and very easy to set up
Thank you both so much!
websockets
websockets
licence version pyversions tests docs openssf websockets is a library for building WebSocket servers and clients in Python with a focus on correctness, simplicity, robustness, and performance. It s...
this will help
it has asyncio, threading, sans io support