ciniml / WireGuard-ESP32-Arduino

WireGuard implementation for ESP32 Arduino
Other
782 stars 60 forks source link

The device with esphome.io firmware reboots approximately every 10 minutes #13

Closed vsurkov closed 2 years ago

vsurkov commented 2 years ago

Hi, i use library with esphome firmware on ESP32 nodemcu 38 pin board (https://github.com/esphome/feature-requests/issues/1444#issuecomment-966295758) everything is great, but controller reboots approximately every 10 minutes. I found the error on ESP log and here is it:

[21:28:17][D][sntp:075]: Synchronized time: 2021-12-19 21:28:17
[21:28:17][W][MyModule:020]: _________________________________________
[21:28:17][W][MyModule:021]: START wirenguard
[21:28:17][W][MyModule:023]: END wirenguard
[21:28:17][W][MyModule:024]: _________________________________________
[21:28:27]Guru Meditation Error: Core  0 panic'ed (LoadProhibited). Exception was unhandled.
[21:28:27]Core 0 register dump:
[21:28:27]PC      : 0x400e1177  PS      : 0x00060830  A0      : 0x800e1320  A1      : 0x3ffb5680  
WARNING Decoded 0x400e1177: wireguardif_peer_output at /data/my_awesome_controller_two/.piolibdeps/my_awesome_controller_two/WireGuard-ESP32/src/wireguardif.c:912
 (inlined by) wireguardif_output_to_peer at /data/my_awesome_controller_two/.piolibdeps/my_awesome_controller_two/WireGuard-ESP32/src/wireguardif.c:162
[21:28:27]A2      : 0x00000000  A3      : 0x3ffc1970  A4      : 0x3ffd4c34  A5      : 0x3ffd4d24  
[21:28:27]A6      : 0x3ffd5a34  A7      : 0x00000000  A8      : 0x800e116c  A9      : 0x3ffb5650  
[21:28:27]A10     : 0x3ffd5aa8  A11     : 0x3ffd5aa8  A12     : 0x3ffd4c50  A13     : 0x0000ca6c  
[21:28:28]A14     : 0xfffffffc  A15     : 0x00000002  SAR     : 0x00000019  EXCCAUSE: 0x0000001c  
[21:28:28]EXCVADDR: 0x00000008  LBEG    : 0x400e3e4a  LEND    : 0x400e3e5a  LCOUNT  : 0x00000000  
[21:28:28]
[21:28:28]ELF file SHA256: 0000000000000000
[21:28:28]
[21:28:28]Backtrace: 0x400e1177:0x3ffb5680 0x400e131d:0x3ffb56b0 0x40139e65:0x3ffb5780 0x4013a06c:0x3ffb57a0 0x4013462c:0x3ffb57c0 0x40089dca:0x3ffb57f0
WARNING Found stack trace! Trying to decode it
WARNING Decoded 0x400e1177: wireguardif_peer_output at /data/my_awesome_controller_two/.piolibdeps/my_awesome_controller_two/WireGuard-ESP32/src/wireguardif.c:912
 (inlined by) wireguardif_output_to_peer at /data/my_awesome_controller_two/.piolibdeps/my_awesome_controller_two/WireGuard-ESP32/src/wireguardif.c:162
WARNING Decoded 0x400e131d: wireguardif_send_keepalive at /data/my_awesome_controller_two/.piolibdeps/my_awesome_controller_two/WireGuard-ESP32/src/wireguardif.c:912
 (inlined by) wireguardif_tmr at /data/my_awesome_controller_two/.piolibdeps/my_awesome_controller_two/WireGuard-ESP32/src/wireguardif.c:879
WARNING Decoded 0x40139e65: sys_check_timeouts at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/lwip/lwip/src/core/timeouts.c:381
WARNING Decoded 0x4013a06c: sys_timeouts_mbox_fetch at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/lwip/lwip/src/core/timeouts.c:433
WARNING Decoded 0x4013462c: tcpip_thread at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/lwip/lwip/src/api/tcpip.c:483
WARNING Decoded 0x40089dca: vPortTaskWrapper at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/port.c:355 (discriminator 1)
[21:28:28]
[21:28:28]Rebooting...

All recommendations like disabling logging do not help, I have consistently tried disabling everything else, the problem is in WireGuard. One more thing. I think this is due to the period of sntp time synchronization, perhaps at this moment the initialization is called again and crash "[21:28:17][D][sntp:075]: Synchronized time: 2021-12-19 21:28:17"

vsurkov commented 2 years ago

Hi there, I found a solution to this problem. In the esphome config file, wg initialization inside lambda cannot be specified without checking, because when the sntp are sync next time, the wg.begin(local_ip,private_key,endpoint_address,public_key,endpoint_port); function is called again and this leads to a crash application and reboot.

the correct configuration should be like this:

time:
  - platform: sntp
    on_time_sync:
        then:
        - lambda: |
            static const char* TAG = "wireguard";
            ESP_LOGD(TAG, "Starting...");
            if( !wg.is_initialized() ) {
                        ESP_LOGD(TAG, "Initializing WG interface...");
                        if( !wg.begin(
                                local_ip,
                                private_key,
                                endpoint_address,
                                public_key,
                                endpoint_port) ) {
                            ESP_LOGD(TAG, "Failed to initialize WG interface.");
                        }
                    }