miguelgrinberg / flask-sock

Modern WebSocket support for Flask.
MIT License
272 stars 24 forks source link

ValueError: Invalid file descriptor: -1 #51

Closed rsking01 closed 1 year ago

rsking01 commented 1 year ago

After connecting the websocket, it closes without errors (client side), and in the server log I see this:

Exception in thread Thread-2:
Traceback (most recent call last):
  File "/usr/lib/python3.8/threading.py", line 932, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.8/threading.py", line 870, in run
    self._target(*self._args, **self._kwargs)
  File "/root/server/serverenv/lib/python3.8/site-packages/simple_websocket/ws.py", line 144, in _thread
    sel.register(self.sock, selectors.EVENT_READ, True)
  File "/usr/lib/python3.8/selectors.py", line 352, in register
    key = super().register(fileobj, events, data)
  File "/usr/lib/python3.8/selectors.py", line 238, in register
    key = SelectorKey(fileobj, self._fileobj_lookup(fileobj), events, data)
  File "/usr/lib/python3.8/selectors.py", line 225, in _fileobj_lookup
    return _fileobj_to_fd(fileobj)
  File "/usr/lib/python3.8/selectors.py", line 42, in _fileobj_to_fd
    raise ValueError("Invalid file descriptor: {}".format(fd))
ValueError: Invalid file descriptor: -1

And I don't understand why it happens. My friend is facing this problem, but my client connects to the socket and everything works fine.

Server code:

@sock.route('/index')
@login_required
def index_socket(ws):
    while True:
        data = ws.receive()

        if data == 'test':
            ws.send('test ok')

    ws.close(message = 'bye')

Client Code:

$(document).ready(function() {
    const socket = new WebSocket('wss://' + location.host + '/index');

    socket.onopen = function(event) {
        socket.send("test");
    };

});
miguelgrinberg commented 1 year ago

So let me see if I understand. You have a server that you can connect to just fine, but your friend gets this error? In that case you should look at the browser console and network tabs on your friend's browser to see if there are any clues there.

rsking01 commented 1 year ago

Hello. It turns out that the problem was that my friend was using the http protocol, and the websocket was using wss. This was due to the fact that I sent the link to the site in the messenger without specifying the protocol (example.com). I'm sorry to bother you, thanks for the reply.