Skip to content

Commit

Permalink
Exceptions that occur in callbacks are no longer suppressed by default.
Browse files Browse the repository at this point in the history
They can optionally be suppressed by setting `client.suppress_exceptions = True`.

Closes #365.
  • Loading branch information
ralight committed Aug 20, 2020
1 parent 5a9ca61 commit 88688d8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
3 changes: 3 additions & 0 deletions ChangeLog.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
v1.x.x - 20xx-xx-xx
===================

- Exceptions that occur in callbacks are no longer suppressed by default. They
can optionally be suppressed by setting `client.suppress_exceptions = True`.
Closes #365.

v1.5.0 - 2019-10-30
===================
Expand Down
28 changes: 28 additions & 0 deletions src/paho/mqtt/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,9 @@ def on_connect(client, userdata, flags, rc, properties=None):
"userdata" is user data of any type and can be set when creating a new client
instance or with user_data_set(userdata).
If you wish to suppress exceptions within a callback, you should set
`client.suppress_exceptions = True`
The callbacks:
on_connect(client, userdata, flags, rc, properties=None): called when the broker responds to our connection
Expand Down Expand Up @@ -651,6 +654,7 @@ def __init__(self, client_id="", clean_session=None, userdata=None,
self._websocket_extra_headers = None
# for clean_start == MQTT_CLEAN_START_FIRST_ONLY
self._mqttv5_first_connect = True
self.suppress_exceptions = False # For callbacks

def __del__(self):
self._reset_sockets()
Expand Down Expand Up @@ -2068,6 +2072,8 @@ def _call_socket_open(self):
except Exception as err:
self._easy_log(
MQTT_LOG_ERR, 'Caught exception in on_socket_open: %s', err)
if not self.suppress_exceptions:
raise

@property
def on_socket_close(self):
Expand Down Expand Up @@ -2100,6 +2106,8 @@ def _call_socket_close(self, sock):
except Exception as err:
self._easy_log(
MQTT_LOG_ERR, 'Caught exception in on_socket_close: %s', err)
if not self.suppress_exceptions:
raise

@property
def on_socket_register_write(self):
Expand Down Expand Up @@ -2135,6 +2143,8 @@ def _call_socket_register_write(self):
except Exception as err:
self._easy_log(
MQTT_LOG_ERR, 'Caught exception in on_socket_register_write: %s', err)
if not self.suppress_exceptions:
raise

@property
def on_socket_unregister_write(self):
Expand Down Expand Up @@ -2171,6 +2181,8 @@ def _call_socket_unregister_write(self, sock=None):
except Exception as err:
self._easy_log(
MQTT_LOG_ERR, 'Caught exception in on_socket_unregister_write: %s', err)
if not self.suppress_exceptions:
raise

def message_callback_add(self, sub, callback):
"""Register a message callback for a specific topic.
Expand Down Expand Up @@ -2348,6 +2360,8 @@ def _packet_write(self):
except Exception as err:
self._easy_log(
MQTT_LOG_ERR, 'Caught exception in on_publish: %s', err)
if not self.suppress_exceptions:
raise

packet['info']._set_as_published()

Expand Down Expand Up @@ -3017,6 +3031,8 @@ def _handle_connack(self):
except Exception as err:
self._easy_log(
MQTT_LOG_ERR, 'Caught exception in on_connect: %s', err)
if not self.suppress_exceptions:
raise

if result == 0:
rc = 0
Expand Down Expand Up @@ -3136,6 +3152,8 @@ def _handle_suback(self):
except Exception as err:
self._easy_log(
MQTT_LOG_ERR, 'Caught exception in on_subscribe: %s', err)
if not self.suppress_exceptions:
raise

return MQTT_ERR_SUCCESS

Expand Down Expand Up @@ -3323,6 +3341,8 @@ def _handle_unsuback(self):
except Exception as err:
self._easy_log(
MQTT_LOG_ERR, 'Caught exception in on_unsubscribe: %s', err)
if not self.suppress_exceptions:
raise
return MQTT_ERR_SUCCESS

def _do_on_disconnect(self, rc, properties=None):
Expand All @@ -3338,6 +3358,8 @@ def _do_on_disconnect(self, rc, properties=None):
except Exception as err:
self._easy_log(
MQTT_LOG_ERR, 'Caught exception in on_disconnect: %s', err)
if not self.suppress_exceptions:
raise

def _do_on_publish(self, mid):
with self._callback_mutex:
Expand All @@ -3348,6 +3370,8 @@ def _do_on_publish(self, mid):
except Exception as err:
self._easy_log(
MQTT_LOG_ERR, 'Caught exception in on_publish: %s', err)
if not self.suppress_exceptions:
raise

msg = self._out_messages.pop(mid)
msg.info._set_as_published()
Expand Down Expand Up @@ -3407,6 +3431,8 @@ def _handle_on_message(self, message):
callback.__name__,
err
)
if not self.suppress_exceptions:
raise
matched = True

if matched == False and self.on_message:
Expand All @@ -3416,6 +3442,8 @@ def _handle_on_message(self, message):
except Exception as err:
self._easy_log(
MQTT_LOG_ERR, 'Caught exception in on_message: %s', err)
if not self.suppress_exceptions:
raise

def _thread_main(self):
self.loop_forever(retry_first_connection=True)
Expand Down

0 comments on commit 88688d8

Please sign in to comment.