socketIO-client icon indicating copy to clipboard operation
socketIO-client copied to clipboard

Block when define a Namespace

Open davidwtf opened this issue 11 years ago • 5 comments

I used the following code, but the program blocked on socketio.define(...):

class TestNamespace(SocketIONamespace):
        pass
socketio = SocketIO('127.0.0.1', 8081)
socketio.define(TestNamespace, '/test')

I traced the code and found that:

  1. There's a default namespace after create a instance of SocketIO object.
  2. The define() while blocked until all namespaces are connected.
  3. In the _should_stop_waiting() function, the connection is checked by namespace's _connected property.
  4. In the _process_packet() function, the namespace's _connected property is set after received the connect message.
  5. But, the default namespace's _connected has never been set, so the define() is blocked.

And, I also checked the server's code:

  1. The connection build on polling first, and then upgrade to websocket.
  2. The sever send two packet by polling when client connected: the connect packet(type=0) and message (type=4) packet.
  3. But, on the client side, in _get_engineIO_session() function, only connect packet is processed. and then upgrade to websocket.

Finally, I found the block is caused by forgot set _connected to the default namespace. The fix to suggest is appending self._on_connect(None, self.get_namespace()) on SocketIO.__init__ function.

By the way, don't forget change __version__ in your code. :-)

davidwtf avatar Nov 05 '15 04:11 davidwtf

I also found that the define() function blocks forever--the examples in the documentation that use define() for custom namespaces don't work as intended. There is a function named _should_stop_waiting() in __init__.py that references the variable _connected, but the variable does not exist:

    def _should_stop_waiting(self, for_connect=False, for_callbacks=False):
        if for_connect:
            for namespace in self._namespace_by_path.values():
                is_namespace_connected = getattr(
                    namespace, '_connected', False)

If you remove the third parameter (False) from the getattr() call, an exception will occur because the BaseNamespace (SocketIONamespace) does not have an attribute named _connected, and getattr() is masking that fact. Is there possibly an update to namespaces.py that didn't get committed when the lines above were added to _should_stop_waiting()?

kdizzl avatar Nov 19 '15 01:11 kdizzl

Possibly related to the problem: The code you get when you checkout version 0.6.5 has __version__ set to 0.6.3 in __init__.py.

kdizzl avatar Nov 19 '15 01:11 kdizzl

Same issue here. I hope it'll be fixed soon.

As a temporary solution I use custom Namespace with hardcoded field _connected = True. You need to include this namespace while initializing SocketIO and than also in socketIO.define(), because in _should_stop_waiting loop method returns False and stops iterating when default namespace '/' doesn't have _connected = True.

So here's my workaround:

class Namespace(LoggingNamespace):
    _connected = True

    def on_connect(self):
        print('connected')

    def on_error(self, data):
        print('error')

with SocketIO('http://0.0.0.0', 8080, Namespace) as socketIO:
    nsp = socketIO.define(Namespace, '/my_namespace')
    nsp.emit('my message', 'hello')
    socketIO.wait(seconds=1)

ladislav-mizenko avatar Dec 02 '15 16:12 ladislav-mizenko

@ladislav-mizenko Great workaround. I just need this library for testing my webservice and it was really frustrating that it didn't work. Thank you very much for this!

Please include the fix in the actual version!

cansik avatar Feb 13 '16 16:02 cansik

This is most likely happening because the namespace that you are trying to define is not defined on the server.

Duplicate of https://github.com/invisibleroads/socketIO-client/issues/84

Fixed in 2abcc91fb7f6c5a5d1da1f5edf2fda1d85890015

Please re-open this issue if it is still a problem.

invisibleroads avatar Jul 14 '16 17:07 invisibleroads