Catch response write errors in the MQTT callback.

If the client that forwarded the request is no longer available (either
because an exception or a timeout was raised) then its I/O buffer and
event loop may be closed.

In this case, the response callback should handle and report the
exception, and still set the event, so that any other threads waiting
for the response can move on.
This commit is contained in:
Fabio Manganiello 2023-09-27 11:23:55 +02:00
parent ca7f042ccc
commit b76f141b61
Signed by untrusted user: blacklight
GPG key ID: D90FBA7F76362774

View file

@ -479,8 +479,9 @@ class MqttPlugin(RunnablePlugin):
client.stop()
del client
@staticmethod
def _response_callback(reply_topic: str, event: threading.Event, buffer: IO[bytes]):
def _response_callback(
self, reply_topic: str, event: threading.Event, buffer: IO[bytes]
):
"""
A response callback that writes the response to an IOBuffer and stops
the client loop.
@ -490,9 +491,15 @@ class MqttPlugin(RunnablePlugin):
if msg.topic != reply_topic:
return
buffer.write(msg.payload)
client.loop_stop()
event.set()
try:
buffer.write(msg.payload)
client.loop_stop()
except Exception as e:
self.logger.warning(
'Could not write the response back to the MQTT client: %s', e
)
finally:
event.set()
return on_message