Skip to content
Advertisement

Paho MQTT Python Client: No exceptions thrown, just stops

I try to setup a mqtt client in python3. This is not the first time im doing this, however i came across a rather odd behaviour. When trying to call a function, which contains a bug, from one of the callback functions (on_connect or on_message), python does not throw an exception (at least it is not printed), it just stops there. I tied together a short example, that reproduces that behaviour.

Does someone has an idea?

JavaScript

This is the output when sending a message to the topic “test”:

JavaScript

When calling func() from main, i get the correct TypeError thrown. So something catches this exception in paho. I had a look at an olderproject (python2 though) and tried to recreate the behaviour. There the exception gets thrown correctly. What do i miss?

EDIT I can catch the exception by putting the func() call in a try block. How ever, it does not stop the execution of the program when not catched. I dont get why

Advertisement

Answer

This will be due to the fact that the on_message function is called by the network thread and it will be wrapping that call in a try block to stop errors in on_message from stopping that thread.

If you want to an error to stop the app then you should use your own try block in on_message and behave appropriately.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement