Skip to content

Commit

Permalink
Option to disable the SIGINT handler in the client (Fixes #792)
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Oct 26, 2021
1 parent db0565a commit ea84b9b
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 5 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ packages = find:
python_requires = >=3.6
install_requires =
bidict >= 0.21.0
python-engineio >= 4.1.0
python-engineio >= 4.3.0

[options.packages.find]
where = src
Expand Down
5 changes: 5 additions & 0 deletions src/socketio/asyncio_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ class AsyncClient(client.Client):
packets. Custom json modules must have ``dumps`` and ``loads``
functions that are compatible with the standard library
versions.
:param handle_sigint: Set to ``True`` to automatically handle disconnection
when the process is interrupted, or to ``False`` to
leave interrupt handling to the calling application.
Interrupt handling can only be enabled when the
client instance is created in the main thread.
The Engine.IO configuration supports the following settings:
Expand Down
11 changes: 9 additions & 2 deletions src/socketio/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ class Client(object):
packets. Custom json modules must have ``dumps`` and ``loads``
functions that are compatible with the standard library
versions.
:param handle_sigint: Set to ``True`` to automatically handle disconnection
when the process is interrupted, or to ``False`` to
leave interrupt handling to the calling application.
Interrupt handling can only be enabled when the
client instance is created in the main thread.
The Engine.IO configuration supports the following settings:
Expand All @@ -90,9 +95,9 @@ class Client(object):
def __init__(self, reconnection=True, reconnection_attempts=0,
reconnection_delay=1, reconnection_delay_max=5,
randomization_factor=0.5, logger=False, serializer='default',
json=None, **kwargs):
json=None, handle_sigint=True, **kwargs):
global original_signal_handler
if original_signal_handler is None and \
if handle_sigint and original_signal_handler is None and \
threading.current_thread() == threading.main_thread():
original_signal_handler = signal.signal(signal.SIGINT,
signal_handler)
Expand All @@ -101,8 +106,10 @@ def __init__(self, reconnection=True, reconnection_attempts=0,
self.reconnection_delay = reconnection_delay
self.reconnection_delay_max = reconnection_delay_max
self.randomization_factor = randomization_factor
self.handle_sigint = handle_sigint

engineio_options = kwargs
engineio_options['handle_sigint'] = handle_sigint
engineio_logger = engineio_options.pop('engineio_logger', None)
if engineio_logger is not None:
engineio_options['logger'] = engineio_logger
Expand Down
8 changes: 6 additions & 2 deletions tests/common/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,17 @@ def test_create(self, engineio_client_class):
reconnection_delay=5,
reconnection_delay_max=10,
randomization_factor=0.2,
handle_sigint=False,
foo='bar',
)
assert not c.reconnection
assert c.reconnection_attempts == 123
assert c.reconnection_delay == 5
assert c.reconnection_delay_max == 10
assert c.randomization_factor == 0.2
engineio_client_class().assert_called_once_with(foo='bar')
assert not c.handle_sigint
engineio_client_class().assert_called_once_with(
foo='bar', handle_sigint=False)
assert c.connection_url is None
assert c.connection_headers is None
assert c.connection_transports is None
Expand Down Expand Up @@ -89,7 +92,8 @@ def test_logger(self):
@mock.patch('socketio.client.Client._engineio_client_class')
def test_engineio_logger(self, engineio_client_class):
client.Client(engineio_logger='foo')
engineio_client_class().assert_called_once_with(logger='foo')
engineio_client_class().assert_called_once_with(
handle_sigint=True, logger='foo')

def test_on_event(self):
c = client.Client()
Expand Down

0 comments on commit ea84b9b

Please sign in to comment.