@@ -436,6 +436,9 @@ def on_connect(client, userdata, flags, rc, properties=None):
436
436
"userdata" is user data of any type and can be set when creating a new client
437
437
instance or with user_data_set(userdata).
438
438
439
+ If you wish to suppress exceptions within a callback, you should set
440
+ `client.suppress_exceptions = True`
441
+
439
442
The callbacks:
440
443
441
444
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,
651
654
self ._websocket_extra_headers = None
652
655
# for clean_start == MQTT_CLEAN_START_FIRST_ONLY
653
656
self ._mqttv5_first_connect = True
657
+ self .suppress_exceptions = False # For callbacks
654
658
655
659
def __del__ (self ):
656
660
self ._reset_sockets ()
@@ -1637,14 +1641,7 @@ def loop_misc(self):
1637
1641
else :
1638
1642
rc = 1
1639
1643
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 )
1648
1645
1649
1646
return MQTT_ERR_CONN_LOST
1650
1647
@@ -2075,6 +2072,8 @@ def _call_socket_open(self):
2075
2072
except Exception as err :
2076
2073
self ._easy_log (
2077
2074
MQTT_LOG_ERR , 'Caught exception in on_socket_open: %s' , err )
2075
+ if not self .suppress_exceptions :
2076
+ raise
2078
2077
2079
2078
@property
2080
2079
def on_socket_close (self ):
@@ -2107,6 +2106,8 @@ def _call_socket_close(self, sock):
2107
2106
except Exception as err :
2108
2107
self ._easy_log (
2109
2108
MQTT_LOG_ERR , 'Caught exception in on_socket_close: %s' , err )
2109
+ if not self .suppress_exceptions :
2110
+ raise
2110
2111
2111
2112
@property
2112
2113
def on_socket_register_write (self ):
@@ -2142,6 +2143,8 @@ def _call_socket_register_write(self):
2142
2143
except Exception as err :
2143
2144
self ._easy_log (
2144
2145
MQTT_LOG_ERR , 'Caught exception in on_socket_register_write: %s' , err )
2146
+ if not self .suppress_exceptions :
2147
+ raise
2145
2148
2146
2149
@property
2147
2150
def on_socket_unregister_write (self ):
@@ -2178,6 +2181,8 @@ def _call_socket_unregister_write(self, sock=None):
2178
2181
except Exception as err :
2179
2182
self ._easy_log (
2180
2183
MQTT_LOG_ERR , 'Caught exception in on_socket_unregister_write: %s' , err )
2184
+ if not self .suppress_exceptions :
2185
+ raise
2181
2186
2182
2187
def message_callback_add (self , sub , callback ):
2183
2188
"""Register a message callback for a specific topic.
@@ -2219,18 +2224,8 @@ def _loop_rc_handle(self, rc, properties=None):
2219
2224
if self ._state == mqtt_cs_disconnecting :
2220
2225
rc = MQTT_ERR_SUCCESS
2221
2226
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
+
2234
2229
return rc
2235
2230
2236
2231
def _packet_read (self ):
@@ -2365,6 +2360,8 @@ def _packet_write(self):
2365
2360
except Exception as err :
2366
2361
self ._easy_log (
2367
2362
MQTT_LOG_ERR , 'Caught exception in on_publish: %s' , err )
2363
+ if not self .suppress_exceptions :
2364
+ raise
2368
2365
2369
2366
packet ['info' ]._set_as_published ()
2370
2367
@@ -2374,15 +2371,7 @@ def _packet_write(self):
2374
2371
with self ._msgtime_mutex :
2375
2372
self ._last_msg_out = time_func ()
2376
2373
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 )
2386
2375
2387
2376
self ._sock_close ()
2388
2377
return MQTT_ERR_SUCCESS
@@ -2437,14 +2426,8 @@ def _check_keepalive(self):
2437
2426
rc = MQTT_ERR_SUCCESS
2438
2427
else :
2439
2428
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 )
2448
2431
2449
2432
def _mid_generate (self ):
2450
2433
with self ._mid_generate_mutex :
@@ -3048,6 +3031,8 @@ def _handle_connack(self):
3048
3031
except Exception as err :
3049
3032
self ._easy_log (
3050
3033
MQTT_LOG_ERR , 'Caught exception in on_connect: %s' , err )
3034
+ if not self .suppress_exceptions :
3035
+ raise
3051
3036
3052
3037
if result == 0 :
3053
3038
rc = 0
@@ -3167,6 +3152,8 @@ def _handle_suback(self):
3167
3152
except Exception as err :
3168
3153
self ._easy_log (
3169
3154
MQTT_LOG_ERR , 'Caught exception in on_subscribe: %s' , err )
3155
+ if not self .suppress_exceptions :
3156
+ raise
3170
3157
3171
3158
return MQTT_ERR_SUCCESS
3172
3159
@@ -3354,8 +3341,26 @@ def _handle_unsuback(self):
3354
3341
except Exception as err :
3355
3342
self ._easy_log (
3356
3343
MQTT_LOG_ERR , 'Caught exception in on_unsubscribe: %s' , err )
3344
+ if not self .suppress_exceptions :
3345
+ raise
3357
3346
return MQTT_ERR_SUCCESS
3358
3347
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
+
3359
3364
def _do_on_publish (self , mid ):
3360
3365
with self ._callback_mutex :
3361
3366
if self .on_publish :
@@ -3365,6 +3370,8 @@ def _do_on_publish(self, mid):
3365
3370
except Exception as err :
3366
3371
self ._easy_log (
3367
3372
MQTT_LOG_ERR , 'Caught exception in on_publish: %s' , err )
3373
+ if not self .suppress_exceptions :
3374
+ raise
3368
3375
3369
3376
msg = self ._out_messages .pop (mid )
3370
3377
msg .info ._set_as_published ()
@@ -3424,6 +3431,8 @@ def _handle_on_message(self, message):
3424
3431
callback .__name__ ,
3425
3432
err
3426
3433
)
3434
+ if not self .suppress_exceptions :
3435
+ raise
3427
3436
matched = True
3428
3437
3429
3438
if matched == False and self .on_message :
@@ -3433,6 +3442,8 @@ def _handle_on_message(self, message):
3433
3442
except Exception as err :
3434
3443
self ._easy_log (
3435
3444
MQTT_LOG_ERR , 'Caught exception in on_message: %s' , err )
3445
+ if not self .suppress_exceptions :
3446
+ raise
3436
3447
3437
3448
def _thread_main (self ):
3438
3449
self .loop_forever (retry_first_connection = True )
0 commit comments