hivemq-mqtt-client icon indicating copy to clipboard operation
hivemq-mqtt-client copied to clipboard

What happens if exceptions are thrown in callbacks?

Open inad9300 opened this issue 1 year ago • 3 comments

I have a Mqtt5AsyncClient on which I call subscribeWith(), .callback(), etc. My question is, what would happen if an exception is thrown by the consumer passed as callback? In my experiments, the consumer is never called again after such an event, so I would like to understand more in detail what happens internally in HiveMQ in case of exception, and what is the expectation from library users. I was quite surprised by this behavior to be honest – I was expecting HiveMQ to handle the exception in such a way that future calls to the callback kept working.

inad9300 avatar Jun 01 '23 11:06 inad9300

Hi @inad9300 - @SgtSilvio might know off hand as he's the original author. Do you have a test case? We could definitely document this better with our findings.

pglombardo avatar Jun 01 '23 11:06 pglombardo

I guess it depends what you mean by test case. This is basically the code I used for my experiment:

.callback(__ -> {
  System.out.println("Handling message..."); // Printed once and never again.
  throw new RuntimeException("Error handling message.");
})

inad9300 avatar Jun 01 '23 12:06 inad9300

Encountered the same problem and would very much like this to be fixed.

rod2j avatar Jun 25 '23 13:06 rod2j

Hi @inad9300 @rod2j, the current requirement is to protect against exceptions in callbacks but this could be improved and definitely at least documented.

I filed #624 to improve this behaviour based on your suggestions. If you have any other suggestions or ideas, feel free to add them into #624.

I'll close this question issue for now. If anything else remains, let me know.

pglombardo avatar Mar 05 '24 14:03 pglombardo