Skip to content

Commit 268fe12

Browse files
Do not send ACK packet for unknown events (Fixes #824)
1 parent 4471501 commit 268fe12

File tree

4 files changed

+24
-2
lines changed

4 files changed

+24
-2
lines changed

src/socketio/asyncio_server.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ async def _handle_event(self, eio_sid, namespace, id, data):
514514
async def _handle_event_internal(self, server, sid, eio_sid, data,
515515
namespace, id):
516516
r = await server._trigger_event(data[0], namespace, sid, *data[1:])
517-
if id is not None:
517+
if r != self.not_handled and id is not None:
518518
# send ACK packet with the response returned by the handler
519519
# tuples are expanded as multiple arguments
520520
if r is None:
@@ -553,6 +553,8 @@ async def _trigger_event(self, event, namespace, *args):
553553
else:
554554
ret = handler(*args)
555555
return ret
556+
else:
557+
return self.not_handled
556558

557559
# or else, forward the event to a namepsace handler if one exists
558560
elif namespace in self.namespace_handlers: # pragma: no branch

src/socketio/server.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ def __init__(self, client_manager=None, logger=False, serializer='default',
134134
self.environ = {}
135135
self.handlers = {}
136136
self.namespace_handlers = {}
137+
self.not_handled = object()
137138

138139
self._binary_packet = {}
139140

@@ -720,7 +721,7 @@ def _handle_event(self, eio_sid, namespace, id, data):
720721
def _handle_event_internal(self, server, sid, eio_sid, data, namespace,
721722
id):
722723
r = server._trigger_event(data[0], namespace, sid, *data[1:])
723-
if id is not None:
724+
if r != self.not_handled and id is not None:
724725
# send ACK packet with the response returned by the handler
725726
# tuples are expanded as multiple arguments
726727
if r is None:
@@ -748,6 +749,8 @@ def _trigger_event(self, event, namespace, *args):
748749
elif event not in self.reserved_events and \
749750
'*' in self.handlers[namespace]:
750751
return self.handlers[namespace]['*'](event, *args)
752+
else:
753+
return self.not_handled
751754

752755
# or else, forward the event to a namespace handler if one exists
753756
elif namespace in self.namespace_handlers: # pragma: no branch

tests/asyncio/test_asyncio_server.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -703,6 +703,15 @@ def test_handle_event_with_ack(self, eio):
703703
'123', '31000["foo"]'
704704
)
705705

706+
def test_handle_unknown_event_with_ack(self, eio):
707+
eio.return_value.send = AsyncMock()
708+
s = asyncio_server.AsyncServer(async_handlers=False)
709+
s.manager.connect('123', '/')
710+
handler = mock.MagicMock(return_value='foo')
711+
s.on('my message', handler)
712+
_run(s._handle_eio_message('123', '21000["another message","foo"]'))
713+
s.eio.send.mock.assert_not_called()
714+
706715
def test_handle_event_with_ack_none(self, eio):
707716
eio.return_value.send = AsyncMock()
708717
s = asyncio_server.AsyncServer(async_handlers=False)

tests/common/test_server.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -618,6 +618,14 @@ def test_handle_event_with_ack(self, eio):
618618
handler.assert_called_once_with(sid, 'foo')
619619
s.eio.send.assert_called_once_with('123', '31000["foo"]')
620620

621+
def test_handle_unknown_event_with_ack(self, eio):
622+
s = server.Server(async_handlers=False)
623+
s.manager.connect('123', '/')
624+
handler = mock.MagicMock(return_value='foo')
625+
s.on('my message', handler)
626+
s._handle_eio_message('123', '21000["another message","foo"]')
627+
s.eio.send.assert_not_called()
628+
621629
def test_handle_event_with_ack_none(self, eio):
622630
s = server.Server(async_handlers=False)
623631
sid = s.manager.connect('123', '/')

0 commit comments

Comments
 (0)