vladak / shield

code for ESP32 Adafruit Feather to get various sensor readings and publish them to MQTT broker
2 stars 0 forks source link

pystack exhausted when using MQTT handler #10

Closed vladak closed 1 year ago

vladak commented 1 year ago

The recent changes to use MQTT handler cause stack overflow it seems (on QtPy):

269.940: INFO - Running
269.944: INFO - No library for the tmp117 sensor
269.946: INFO - No library for the sht40 sensor
270.097: INFO - No library for battery gauge (max17048)
270.099: INFO - Connecting to wifi
274.564: INFO - Connected to wifi
274.570: INFO - Attempting to connect to MQTT broker 172.40.0.3:1883
274.595: INFO - Connected to MQTT Broker!
274.602: INFO - Published to logs/pracovna/qtpy with PID 0
274.607: INFO - Published to logs/pracovna/qtpy with PID 0
Code stopped by unhandled exception:
['Traceback (most recent call last):
  File "code.py", line 176, in <module>
  File "code.py", line 128, in main
  File "adafruit_minimqtt/adafruit_minimqtt.py", line 553, in connect
  File "mqtt.py", line 18, in connect
  File "adafruit_logging.py", line 389, in info
  File "adafruit_logging.py", line 326, in _log
  File "adafruit_logging.py", line 348, in handle
  File "mqtt_handler.py", line 37, in emit
  File "adafruit_minimqtt/adafruit_minimqtt.py", line 670, in publish
  File "mqtt.py", line 38, in publish
  File "adafruit_logging.py", line 389, in info
  File "adafruit_logging.py", line 326, in _log
  File "adafruit_logging.py", line 348, in handle
  File "mqtt_handler.py", line 37, in emit
  File "adafruit_minimqtt/adafruit_minimqtt.py", line 670, in publish
  File "mqtt.py", line 38, in publish
  File "adafruit_logging.py", line 389, in info
  File "adafruit_logging.py", line 326, in _log
  File "adafruit_logging.py", line 348, in handle
  File "mqtt_handler.py", line 37, in emit
RuntimeError: pystack exhausted\n']
vladak commented 1 year ago

The MQTT client connect() callback has a log statement. Given that the MQTT handler is setup before the call to connect(), the callback will be invoked. This will in turn produce a MQTT message to be published, however there is also publish callback that too has a logging statement.. and recursion happens as I sort of anticipated in https://github.com/vladak/shield/issues/3#issuecomment-1350643432 however did not do anything about it.

One straightforward solution would be to use non-default logger in the MQTT callbacks to avoid using the MQTT handler, at the cost of losing the MQTT based logging inside MQTT callbacks.