Skip to content

Commit a839a36

Browse files
do not dispatch events for disconnected namespaces (Fixes #333)
1 parent 62a77bf commit a839a36

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

socketio/asyncio_server.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,6 +428,10 @@ async def _handle_event(self, sid, namespace, id, data):
428428
namespace = namespace or '/'
429429
self.logger.info('received event "%s" from %s [%s]', data[0], sid,
430430
namespace)
431+
if not self.manager.is_connected(sid, namespace):
432+
self.logger.warning('%s is not connected to namespace %s',
433+
sid, namespace)
434+
return
431435
if self.async_handlers:
432436
self.start_background_task(self._handle_event_internal, self, sid,
433437
data, namespace, id)

socketio/server.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,10 @@ def _handle_event(self, sid, namespace, id, data):
638638
namespace = namespace or '/'
639639
self.logger.info('received event "%s" from %s [%s]', data[0], sid,
640640
namespace)
641+
if not self.manager.is_connected(sid, namespace):
642+
self.logger.warning('%s is not connected to namespace %s',
643+
sid, namespace)
644+
return
641645
if self.async_handlers:
642646
self.start_background_task(self._handle_event_internal, self, sid,
643647
data, namespace, id)

tests/asyncio/test_asyncio_server.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,7 @@ def test_handle_disconnect_unknown_client(self, eio):
436436
def test_handle_event(self, eio):
437437
eio.return_value.send = AsyncMock()
438438
s = asyncio_server.AsyncServer(async_handlers=False)
439+
s.manager.connect('123', '/')
439440
handler = AsyncMock()
440441
s.on('my message', handler)
441442
_run(s._handle_eio_message('123', '2["my message","a","b","c"]'))
@@ -444,14 +445,25 @@ def test_handle_event(self, eio):
444445
def test_handle_event_with_namespace(self, eio):
445446
eio.return_value.send = AsyncMock()
446447
s = asyncio_server.AsyncServer(async_handlers=False)
448+
s.manager.connect('123', '/foo')
447449
handler = mock.MagicMock()
448450
s.on('my message', handler, namespace='/foo')
449451
_run(s._handle_eio_message('123', '2/foo,["my message","a","b","c"]'))
450452
handler.assert_called_once_with('123', 'a', 'b', 'c')
451453

454+
def test_handle_event_with_disconnected_namespace(self, eio):
455+
eio.return_value.send = AsyncMock()
456+
s = asyncio_server.AsyncServer(async_handlers=False)
457+
s.manager.connect('123', '/foo')
458+
handler = mock.MagicMock()
459+
s.on('my message', handler, namespace='/bar')
460+
_run(s._handle_eio_message('123', '2/bar,["my message","a","b","c"]'))
461+
handler.assert_not_called()
462+
452463
def test_handle_event_binary(self, eio):
453464
eio.return_value.send = AsyncMock()
454465
s = asyncio_server.AsyncServer(async_handlers=False)
466+
s.manager.connect('123', '/')
455467
handler = mock.MagicMock()
456468
s.on('my message', handler)
457469
_run(s._handle_eio_message('123', '52-["my message","a",'
@@ -476,6 +488,7 @@ def test_handle_event_binary_ack(self, eio):
476488
def test_handle_event_with_ack(self, eio):
477489
eio.return_value.send = AsyncMock()
478490
s = asyncio_server.AsyncServer(async_handlers=False)
491+
s.manager.connect('123', '/')
479492
handler = mock.MagicMock(return_value='foo')
480493
s.on('my message', handler)
481494
_run(s._handle_eio_message('123', '21000["my message","foo"]'))
@@ -486,6 +499,7 @@ def test_handle_event_with_ack(self, eio):
486499
def test_handle_event_with_ack_none(self, eio):
487500
eio.return_value.send = AsyncMock()
488501
s = asyncio_server.AsyncServer(async_handlers=False)
502+
s.manager.connect('123', '/')
489503
handler = mock.MagicMock(return_value=None)
490504
s.on('my message', handler)
491505
_run(s._handle_eio_message('123', '21000["my message","foo"]'))
@@ -729,6 +743,7 @@ def loads(*args, **kwargs):
729743

730744
def test_async_handlers(self, eio):
731745
s = asyncio_server.AsyncServer(async_handlers=True)
746+
s.manager.connect('123', '/')
732747
_run(s._handle_eio_message('123', '2["my message","a","b","c"]'))
733748
s.eio.start_background_task.assert_called_once_with(
734749
s._handle_event_internal, s, '123', ['my message', 'a', 'b', 'c'],

tests/common/test_server.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -392,20 +392,31 @@ def test_handle_disconnect_unknown_client(self, eio):
392392

393393
def test_handle_event(self, eio):
394394
s = server.Server(async_handlers=False)
395+
s.manager.connect('123', '/')
395396
handler = mock.MagicMock()
396397
s.on('my message', handler)
397398
s._handle_eio_message('123', '2["my message","a","b","c"]')
398399
handler.assert_called_once_with('123', 'a', 'b', 'c')
399400

400401
def test_handle_event_with_namespace(self, eio):
401402
s = server.Server(async_handlers=False)
403+
s.manager.connect('123', '/foo')
402404
handler = mock.MagicMock()
403405
s.on('my message', handler, namespace='/foo')
404406
s._handle_eio_message('123', '2/foo,["my message","a","b","c"]')
405407
handler.assert_called_once_with('123', 'a', 'b', 'c')
406408

409+
def test_handle_event_with_disconnected_namespace(self, eio):
410+
s = server.Server(async_handlers=False)
411+
s.manager.connect('123', '/foo')
412+
handler = mock.MagicMock()
413+
s.on('my message', handler, namespace='/bar')
414+
s._handle_eio_message('123', '2/bar,["my message","a","b","c"]')
415+
handler.assert_not_called()
416+
407417
def test_handle_event_binary(self, eio):
408418
s = server.Server(async_handlers=False)
419+
s.manager.connect('123', '/')
409420
handler = mock.MagicMock()
410421
s.on('my message', handler)
411422
s._handle_eio_message('123', '52-["my message","a",'
@@ -418,7 +429,6 @@ def test_handle_event_binary(self, eio):
418429
def test_handle_event_binary_ack(self, eio):
419430
mgr = mock.MagicMock()
420431
s = server.Server(client_manager=mgr)
421-
s.manager.initialize(s)
422432
s._handle_eio_message('123', '61-321["my message","a",'
423433
'{"_placeholder":true,"num":0}]')
424434
s._handle_eio_message('123', b'foo')
@@ -427,6 +437,7 @@ def test_handle_event_binary_ack(self, eio):
427437

428438
def test_handle_event_with_ack(self, eio):
429439
s = server.Server(async_handlers=False)
440+
s.manager.connect('123', '/')
430441
handler = mock.MagicMock(return_value='foo')
431442
s.on('my message', handler)
432443
s._handle_eio_message('123', '21000["my message","foo"]')
@@ -436,6 +447,7 @@ def test_handle_event_with_ack(self, eio):
436447

437448
def test_handle_event_with_ack_none(self, eio):
438449
s = server.Server(async_handlers=False)
450+
s.manager.connect('123', '/')
439451
handler = mock.MagicMock(return_value=None)
440452
s.on('my message', handler)
441453
s._handle_eio_message('123', '21000["my message","foo"]')
@@ -655,6 +667,7 @@ def loads(*args, **kwargs):
655667

656668
def test_async_handlers(self, eio):
657669
s = server.Server(async_handlers=True)
670+
s.manager.connect('123', '/')
658671
s._handle_eio_message('123', '2["my message","a","b","c"]')
659672
s.eio.start_background_task.assert_called_once_with(
660673
s._handle_event_internal, s, '123', ['my message', 'a', 'b', 'c'],

0 commit comments

Comments
 (0)