forked from platypush/platypush
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:
parent
ca7f042ccc
commit
b76f141b61
1 changed files with 12 additions and 5 deletions
|
@ -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
|
||||
|
||||
|
|
Loading…
Reference in a new issue