Skip to content

Commit 64de050

Browse files
authored
Merge pull request #507 from ralight/exception-suppression
Exception suppression
2 parents d7592a6 + 88688d8 commit 64de050

File tree

2 files changed

+51
-37
lines changed

2 files changed

+51
-37
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: 48 additions & 37 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()
@@ -1637,14 +1641,7 @@ def loop_misc(self):
16371641
else:
16381642
rc = 1
16391643

1640-
with self._callback_mutex:
1641-
if self.on_disconnect:
1642-
with self._in_callback_mutex:
1643-
try:
1644-
self.on_disconnect(self, self._userdata, rc)
1645-
except Exception as err:
1646-
self._easy_log(
1647-
MQTT_LOG_ERR, 'Caught exception in on_disconnect: %s', err)
1644+
self._do_on_disconnect(rc)
16481645

16491646
return MQTT_ERR_CONN_LOST
16501647

@@ -2075,6 +2072,8 @@ def _call_socket_open(self):
20752072
except Exception as err:
20762073
self._easy_log(
20772074
MQTT_LOG_ERR, 'Caught exception in on_socket_open: %s', err)
2075+
if not self.suppress_exceptions:
2076+
raise
20782077

20792078
@property
20802079
def on_socket_close(self):
@@ -2107,6 +2106,8 @@ def _call_socket_close(self, sock):
21072106
except Exception as err:
21082107
self._easy_log(
21092108
MQTT_LOG_ERR, 'Caught exception in on_socket_close: %s', err)
2109+
if not self.suppress_exceptions:
2110+
raise
21102111

21112112
@property
21122113
def on_socket_register_write(self):
@@ -2142,6 +2143,8 @@ def _call_socket_register_write(self):
21422143
except Exception as err:
21432144
self._easy_log(
21442145
MQTT_LOG_ERR, 'Caught exception in on_socket_register_write: %s', err)
2146+
if not self.suppress_exceptions:
2147+
raise
21452148

21462149
@property
21472150
def on_socket_unregister_write(self):
@@ -2178,6 +2181,8 @@ def _call_socket_unregister_write(self, sock=None):
21782181
except Exception as err:
21792182
self._easy_log(
21802183
MQTT_LOG_ERR, 'Caught exception in on_socket_unregister_write: %s', err)
2184+
if not self.suppress_exceptions:
2185+
raise
21812186

21822187
def message_callback_add(self, sub, callback):
21832188
"""Register a message callback for a specific topic.
@@ -2219,18 +2224,8 @@ def _loop_rc_handle(self, rc, properties=None):
22192224
if self._state == mqtt_cs_disconnecting:
22202225
rc = MQTT_ERR_SUCCESS
22212226

2222-
with self._callback_mutex:
2223-
if self.on_disconnect:
2224-
with self._in_callback_mutex:
2225-
try:
2226-
if properties:
2227-
self.on_disconnect(
2228-
self, self._userdata, rc, properties)
2229-
else:
2230-
self.on_disconnect(self, self._userdata, rc)
2231-
except Exception as err:
2232-
self._easy_log(
2233-
MQTT_LOG_ERR, 'Caught exception in on_disconnect: %s', err)
2227+
self._do_on_disconnect(rc, properties)
2228+
22342229
return rc
22352230

22362231
def _packet_read(self):
@@ -2365,6 +2360,8 @@ def _packet_write(self):
23652360
except Exception as err:
23662361
self._easy_log(
23672362
MQTT_LOG_ERR, 'Caught exception in on_publish: %s', err)
2363+
if not self.suppress_exceptions:
2364+
raise
23682365

23692366
packet['info']._set_as_published()
23702367

@@ -2374,15 +2371,7 @@ def _packet_write(self):
23742371
with self._msgtime_mutex:
23752372
self._last_msg_out = time_func()
23762373

2377-
with self._callback_mutex:
2378-
if self.on_disconnect:
2379-
with self._in_callback_mutex:
2380-
try:
2381-
self.on_disconnect(
2382-
self, self._userdata, 0)
2383-
except Exception as err:
2384-
self._easy_log(
2385-
MQTT_LOG_ERR, 'Caught exception in on_disconnect: %s', err)
2374+
self._do_on_disconnect(0)
23862375

23872376
self._sock_close()
23882377
return MQTT_ERR_SUCCESS
@@ -2437,14 +2426,8 @@ def _check_keepalive(self):
24372426
rc = MQTT_ERR_SUCCESS
24382427
else:
24392428
rc = 1
2440-
with self._callback_mutex:
2441-
if self.on_disconnect:
2442-
with self._in_callback_mutex:
2443-
try:
2444-
self.on_disconnect(self, self._userdata, rc)
2445-
except Exception as err:
2446-
self._easy_log(
2447-
MQTT_LOG_ERR, 'Caught exception in on_disconnect: %s', err)
2429+
2430+
self._do_on_disconnect(rc)
24482431

24492432
def _mid_generate(self):
24502433
with self._mid_generate_mutex:
@@ -3048,6 +3031,8 @@ def _handle_connack(self):
30483031
except Exception as err:
30493032
self._easy_log(
30503033
MQTT_LOG_ERR, 'Caught exception in on_connect: %s', err)
3034+
if not self.suppress_exceptions:
3035+
raise
30513036

30523037
if result == 0:
30533038
rc = 0
@@ -3167,6 +3152,8 @@ def _handle_suback(self):
31673152
except Exception as err:
31683153
self._easy_log(
31693154
MQTT_LOG_ERR, 'Caught exception in on_subscribe: %s', err)
3155+
if not self.suppress_exceptions:
3156+
raise
31703157

31713158
return MQTT_ERR_SUCCESS
31723159

@@ -3354,8 +3341,26 @@ def _handle_unsuback(self):
33543341
except Exception as err:
33553342
self._easy_log(
33563343
MQTT_LOG_ERR, 'Caught exception in on_unsubscribe: %s', err)
3344+
if not self.suppress_exceptions:
3345+
raise
33573346
return MQTT_ERR_SUCCESS
33583347

3348+
def _do_on_disconnect(self, rc, properties=None):
3349+
with self._callback_mutex:
3350+
if self.on_disconnect:
3351+
with self._in_callback_mutex:
3352+
try:
3353+
if properties:
3354+
self.on_disconnect(
3355+
self, self._userdata, rc, properties)
3356+
else:
3357+
self.on_disconnect(self, self._userdata, rc)
3358+
except Exception as err:
3359+
self._easy_log(
3360+
MQTT_LOG_ERR, 'Caught exception in on_disconnect: %s', err)
3361+
if not self.suppress_exceptions:
3362+
raise
3363+
33593364
def _do_on_publish(self, mid):
33603365
with self._callback_mutex:
33613366
if self.on_publish:
@@ -3365,6 +3370,8 @@ def _do_on_publish(self, mid):
33653370
except Exception as err:
33663371
self._easy_log(
33673372
MQTT_LOG_ERR, 'Caught exception in on_publish: %s', err)
3373+
if not self.suppress_exceptions:
3374+
raise
33683375

33693376
msg = self._out_messages.pop(mid)
33703377
msg.info._set_as_published()
@@ -3424,6 +3431,8 @@ def _handle_on_message(self, message):
34243431
callback.__name__,
34253432
err
34263433
)
3434+
if not self.suppress_exceptions:
3435+
raise
34273436
matched = True
34283437

34293438
if matched == False and self.on_message:
@@ -3433,6 +3442,8 @@ def _handle_on_message(self, message):
34333442
except Exception as err:
34343443
self._easy_log(
34353444
MQTT_LOG_ERR, 'Caught exception in on_message: %s', err)
3445+
if not self.suppress_exceptions:
3446+
raise
34363447

34373448
def _thread_main(self):
34383449
self.loop_forever(retry_first_connection=True)

0 commit comments

Comments
 (0)