Skip to content

Commit 88688d8

Browse files
committed
Exceptions that occur in callbacks are no longer suppressed by default.
They can optionally be suppressed by setting `client.suppress_exceptions = True`. Closes #365.
1 parent 5a9ca61 commit 88688d8

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

ChangeLog.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
v1.x.x - 20xx-xx-xx
22
===================
33

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

58
v1.5.0 - 2019-10-30
69
===================

src/paho/mqtt/client.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,9 @@ def on_connect(client, userdata, flags, rc, properties=None):
436436
"userdata" is user data of any type and can be set when creating a new client
437437
instance or with user_data_set(userdata).
438438
439+
If you wish to suppress exceptions within a callback, you should set
440+
`client.suppress_exceptions = True`
441+
439442
The callbacks:
440443
441444
on_connect(client, userdata, flags, rc, properties=None): called when the broker responds to our connection
@@ -651,6 +654,7 @@ def __init__(self, client_id="", clean_session=None, userdata=None,
651654
self._websocket_extra_headers = None
652655
# for clean_start == MQTT_CLEAN_START_FIRST_ONLY
653656
self._mqttv5_first_connect = True
657+
self.suppress_exceptions = False # For callbacks
654658

655659
def __del__(self):
656660
self._reset_sockets()
@@ -2068,6 +2072,8 @@ def _call_socket_open(self):
20682072
except Exception as err:
20692073
self._easy_log(
20702074
MQTT_LOG_ERR, 'Caught exception in on_socket_open: %s', err)
2075+
if not self.suppress_exceptions:
2076+
raise
20712077

20722078
@property
20732079
def on_socket_close(self):
@@ -2100,6 +2106,8 @@ def _call_socket_close(self, sock):
21002106
except Exception as err:
21012107
self._easy_log(
21022108
MQTT_LOG_ERR, 'Caught exception in on_socket_close: %s', err)
2109+
if not self.suppress_exceptions:
2110+
raise
21032111

21042112
@property
21052113
def on_socket_register_write(self):
@@ -2135,6 +2143,8 @@ def _call_socket_register_write(self):
21352143
except Exception as err:
21362144
self._easy_log(
21372145
MQTT_LOG_ERR, 'Caught exception in on_socket_register_write: %s', err)
2146+
if not self.suppress_exceptions:
2147+
raise
21382148

21392149
@property
21402150
def on_socket_unregister_write(self):
@@ -2171,6 +2181,8 @@ def _call_socket_unregister_write(self, sock=None):
21712181
except Exception as err:
21722182
self._easy_log(
21732183
MQTT_LOG_ERR, 'Caught exception in on_socket_unregister_write: %s', err)
2184+
if not self.suppress_exceptions:
2185+
raise
21742186

21752187
def message_callback_add(self, sub, callback):
21762188
"""Register a message callback for a specific topic.
@@ -2348,6 +2360,8 @@ def _packet_write(self):
23482360
except Exception as err:
23492361
self._easy_log(
23502362
MQTT_LOG_ERR, 'Caught exception in on_publish: %s', err)
2363+
if not self.suppress_exceptions:
2364+
raise
23512365

23522366
packet['info']._set_as_published()
23532367

@@ -3017,6 +3031,8 @@ def _handle_connack(self):
30173031
except Exception as err:
30183032
self._easy_log(
30193033
MQTT_LOG_ERR, 'Caught exception in on_connect: %s', err)
3034+
if not self.suppress_exceptions:
3035+
raise
30203036

30213037
if result == 0:
30223038
rc = 0
@@ -3136,6 +3152,8 @@ def _handle_suback(self):
31363152
except Exception as err:
31373153
self._easy_log(
31383154
MQTT_LOG_ERR, 'Caught exception in on_subscribe: %s', err)
3155+
if not self.suppress_exceptions:
3156+
raise
31393157

31403158
return MQTT_ERR_SUCCESS
31413159

@@ -3323,6 +3341,8 @@ def _handle_unsuback(self):
33233341
except Exception as err:
33243342
self._easy_log(
33253343
MQTT_LOG_ERR, 'Caught exception in on_unsubscribe: %s', err)
3344+
if not self.suppress_exceptions:
3345+
raise
33263346
return MQTT_ERR_SUCCESS
33273347

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

33423364
def _do_on_publish(self, mid):
33433365
with self._callback_mutex:
@@ -3348,6 +3370,8 @@ def _do_on_publish(self, mid):
33483370
except Exception as err:
33493371
self._easy_log(
33503372
MQTT_LOG_ERR, 'Caught exception in on_publish: %s', err)
3373+
if not self.suppress_exceptions:
3374+
raise
33513375

33523376
msg = self._out_messages.pop(mid)
33533377
msg.info._set_as_published()
@@ -3407,6 +3431,8 @@ def _handle_on_message(self, message):
34073431
callback.__name__,
34083432
err
34093433
)
3434+
if not self.suppress_exceptions:
3435+
raise
34103436
matched = True
34113437

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

34203448
def _thread_main(self):
34213449
self.loop_forever(retry_first_connection=True)

0 commit comments

Comments
 (0)