Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/workaround for an issue with reboots when WiFi disconnects #298

Merged
merged 1 commit into from
Oct 25, 2024

Conversation

ruza87
Copy link
Contributor

@ruza87 ruza87 commented Oct 24, 2024

These are changes I made in effort to eliminate controller crashes when WiFi goes down and back up in a while.

I managed to replicate the issue described in #276. I disabled WiFi on my router for ~5 secs and then enabled it again. My router is MikroTik cAP ac. diyBMS config: no TFT, MQTT enabled, no influx and no HA export.

Core  0 register dump:
PC      : 0x401b31a2  PS      : 0x00060730  A0      : 0x801b3287  A1      : 0x3ffd7c80  
A2      : 0x3ffb6304  A3      : 0xffffffff  A4      : 0x00000000  A5      : 0xffffffff
A6      : 0x00000000  A7      : 0x3ffe23ac  A8      : 0x3ffdffe8  A9      : 0x3ffd7bf0  
A10     : 0x00000000  A11     : 0x00000001  A12     : 0x3ffe3500  A13     : 0x3ffe3500
A14     : 0x3ffe237c  A15     : 0x3ffe23b6  SAR     : 0x0000001f  EXCCAUSE: 0x0000001c  
EXCVADDR: 0x00000000  LBEG    : 0x4008a4dc  LEND    : 0x4008a4f2  LCOUNT  : 0xffffffff

Backtrace: 0x401b319f:0x3ffd7c80 0x401b3284:0x3ffd7cd0

  #0  0x401b319f:0x3ffd7c80 in handler_execute at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/esp_event/esp_event.c:139
      (inlined by) esp_event_loop_run at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/esp_event/esp_event.c:590
  #1  0x401b3284:0x3ffd7cd0 in esp_event_loop_run_task at /Users/ficeto/Desktop/ESP32/ESP32S2/esp-idf-public/components/esp_event/esp_event.c:115 (discriminator 15)

The stack trace suggests that the ESP tried to dereference null pointer - already freed memory, somewhere outside the diyBMS application. I noticed the ShutdownAllNetworkServices() call when WiFi is disconnected. Perhaps there is some kind of race condition when the resources are being freed and in the same time the ESP manages to connect back to the WiFi quickly and tries to send an event to already non-existing instance.

Anyway, I removed the ShutdownAllNetworkServices() call and make sure that the network services like HTTPD, mDNS and MQTT are started just once. I think there is no need to stop the services upon disconnect (and even when IP addr has changed), they will continue operating once the WiFi is back (for instance, the mqtt client has reconnect_timeout_ms option and the auto reconnect is enabled by default. HTTP for web server is client-initiated, so that should be good too).

I run some tests on a firmware with the proposed changes and was unable to replicate the issue again. Short WiFi dropouts, 1h disconnect... it hadn't crashed and once the WiFi was enabled, it got connected.

@stuartpittaway stuartpittaway merged commit 2395906 into stuartpittaway:master Oct 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants