vintlabs / fauxmoESP

Add voice control of your ESP32 and ESP8266 devices using Amazon Alexa
MIT License
391 stars 71 forks source link

ESP8266 crashing during discovery #208

Open riban-bw opened 2 years ago

riban-bw commented 2 years ago

It seems that the handle() function mostly (only?) manages discovery requests. When this is enabled and a discovery request is sent by Alexa the ESP8266 may overflow and restart. I read in another issue an observation by @pvint:

On ESP8266 there is a limit of 3 devices (this does not seem to be a hard limit, but more of a general rule... with more than 3 devices there's an overflow when creating the reply packets, which needs more investigation).

This bug report is a requets for that, "more investigation". I have managed to get my device discovered and then commented out the handle() call so that I reduce the risk of the device (apparently) randomly restarting every few hours.

I now have a beter handle on what is happening hence realise it is not random. (I had been checking for stack / heap capacity and other issues.) It looks like there are discovery messages that trigger fauxmo to respond but the response may be too large (for some buffer?) possibly due to the quantity of lamps and/or length of each lamp's name.

riban-bw commented 2 years ago

I think the crash I have experienced is a stack overflow. The _handleUDP() function parses each UDP packet and creates a buffer on the stack of whatever size it needs for the parsed data. It may be prudent to validate there is sufficient available stack memory before creating this buffer. I am not sure how ESP8266 on Arduino manages its memory. There is a function that gives the available heap memory system_get_free_heap_size but that may be irrelevant here. This response message buffer is created with a generous overhead of 128 chars to allow dynamic data to be stored but only really requires 37. Maybe there is some optimisation possible there (although if it is that close to the edge then the underlying issue needs resolving).

It is possible the overflow is occuring in the udp library. We could enable debuging to see where the issue triggers.

tambnguyen commented 2 years ago

I'm getting the same problem with my ESP8266 crashing as well.