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
Percy
Percy2y ago
Project ID: a686cf21-bbfd-4ddb-9e0c-5c11cd434e88
aj3163
aj3163OP2y ago
a686cf21-bbfd-4ddb-9e0c-5c11cd434e88
csd4ni3l
csd4ni3l2y ago
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
aj3163
aj3163OP2y ago
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..
csd4ni3l
csd4ni3l2y ago
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)
aj3163
aj3163OP2y ago
Sorry I am still learning about this and can't follow... What do you mean by want the subdomain?
csd4ni3l
csd4ni3l2y ago
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)
aj3163
aj3163OP2y ago
Since I am hosting the service on Railway, I am assuming my subdomain is flask-production-bcb7.up.railway.app Is that correct?
csd4ni3l
csd4ni3l2y ago
where you found that but it seems correct
aj3163
aj3163OP2y ago
In the service setting
csd4ni3l
csd4ni3l2y ago
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
aj3163
aj3163OP2y ago
Oh I don't want to use custom domain yet and do I need to set this URL anywhere?
csd4ni3l
csd4ni3l2y ago
read the websockets documentation if you want to use it no only when you connect
aj3163
aj3163OP2y ago
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
csd4ni3l
csd4ni3l2y ago
@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
luna
luna2y ago
she*
csd4ni3l
csd4ni3l2y ago
i checked your twitch and you have a male voice, are you transgender or smth? am i wrong?
luna
luna2y ago
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.
csd4ni3l
csd4ni3l2y ago
ok sorry, i didnt know
luna
luna2y ago
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.
aj3163
aj3163OP2y ago
Thanks for helping, to both of you! Let me post the repo here
aj3163
aj3163OP2y ago
GitHub
GitHub - arthurjis/web_chat_base at add_basic_chatbot
Contribute to arthurjis/web_chat_base development by creating an account on GitHub.
aj3163
aj3163OP2y ago
So in this repo, I have a React app and Flask server
luna
luna2y ago
okay, and what did you do before the error occurred? Started the app? went to a specific endpoint?
aj3163
aj3163OP2y ago
After start_chat in ln75 in main.py
luna
luna2y ago
not seeing anything that's sticking out. does this happen when you first start the app or when you connect?
aj3163
aj3163OP2y ago
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
luna
luna2y ago
did you mean to use socketio and not regular websockets? theyre not the same thing
aj3163
aj3163OP2y ago
Hmmm. I am totally new to this...
luna
luna2y ago
yeah theyre not the same thing, that's likely the issue here.
luna
luna2y ago
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
luna
luna2y ago
You cannot just use a socketio client to connect to a standard websocket server.
aj3163
aj3163OP2y ago
But I think both the React and Flask server are socket.io? Could you help me check on the main.py script
luna
luna2y ago
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
aj3163
aj3163OP2y ago
ok. Let me try switch So websocket might be more reliable?
csd4ni3l
csd4ni3l2y ago
yes, it is a lot better
luna
luna2y ago
yeah, the issue itself is the socket is not connected and you're trying to send it data.
csd4ni3l
csd4ni3l2y ago
it handles pings by default and stuff its very good and nice
aj3163
aj3163OP2y ago
I see. I would try that!
csd4ni3l
csd4ni3l2y ago
and very easy to set up
aj3163
aj3163OP2y ago
Thank you both so much!
csd4ni3l
csd4ni3l2y ago
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...
csd4ni3l
csd4ni3l2y ago
this will help it has asyncio, threading, sans io support
Want results from more Discord servers?
Add your server