arendst / Tasmota

Alternative firmware for ESP8266 and ESP32 based devices with easy configuration using webUI, OTA updates, automation using timers or rules, expandability and entirely local control over MQTT, HTTP, Serial or KNX. Full documentation at
https://tasmota.github.io/docs
GNU General Public License v3.0
21.69k stars 4.72k forks source link

Crash with tcp server in `>B` or `>BS` section #21654

Closed Thalhammer closed 1 week ago

Thalhammer commented 1 week ago

PROBLEM DESCRIPTION

ESP crashes when wso(x) is executed in a script >B or >BS section. Delaying the initialization using a timer fixes the issue, but is obviously less then ideal. Enabling the script after boot also works, but is not ideal as well.

REQUESTED INFORMATION

Make sure your have performed every step and checked the applicable boxes before submitting your issue. Thank you!

00:00:00.001 CMD: Using USB CDC 00:00:00.001 HDW: ESP32-C3 v0.4 00:00:00.004 UFS: FlashFS mounted with 304 kB free 00:00:00.008 CFG: Loaded from File, Count 33 00:00:00.010 SER: Set to 8N1 115200 bit/s 00:00:00.010 SER: HWCDC supports 115200 bit/s only 00:00:00.014 QPC: Count 1 00:00:00.016 SCR: ufilesystem found 00:00:00.021 SCR: nv=1, tv=0, vns=4, vmem=33, smem=8192, gmem=424, tmem=8653 00:00:00.047 BRY: Berry initialized, RAM used 4400 bytes 00:00:00.055 Project tasmota - Tasmota Version 14.1.0.2(tasmota32)-3_0_1(2024-06-18T09:13:07) 00:00:00.056 tcp server started Guru Meditation Error: Core 0 panic'ed (Load access fault). Exception was unhandled.

Core 0 register dump: MEPC : 0x420e6cd0 RA : 0x420d6d6e SP : 0x3fca2020 GP : 0x3fc8c000
TP : 0x3fc01d50 T0 : 0x420e6cca T1 : 0x40387cc2 T2 : 0x00000009
S0/FP : 0x3fc9e7f4 S1 : 0x420e8116 A0 : 0x3fc9ac10 A1 : 0x3fca2040
A2 : 0x3fc9e7f4 A3 : 0x00000004 A4 : 0x3fc9b000 A5 : 0x00000000
A6 : 0x3fca1fc0 A7 : 0x00000073 S2 : 0x3fc9e7b8 S3 : 0x000001f6
S4 : 0x00000007 S5 : 0x3fca23fc S6 : 0x3fca4c3c S7 : 0x3fca23f8
S8 : 0x3fca4c3c S9 : 0x00000000 S10 : 0x3fca23f7 S11 : 0x3fca483c
T3 : 0x7fffffff T4 : 0x00000000 T5 : 0x0ccccccc T6 : 0x00000019
MSTATUS : 0x00001881 MTVEC : 0x40380001 MCAUSE : 0x00000005 MTVAL : 0x00000000
MHARTID : 0x00000000

Stack memory: 3fca2020: 0x3fca4c3c 0x3fca23fc 0x00000007 0x3fca2040 0x3fc9e7b8 0x420e8116 0x3fc9e7f4 0x420d6d6e

0 0x420e8116 in lwip_netconn_do_newconn at ??:?

1 0x420d6d6e in tcpip_send_msg_wait_sem at ??:?

3fca2040: 0x00000000 0x420e8116 0x3fca2078 0x420e6ec0 0x3fc9e7b8 0x420e8116 0x3fca2078 0x420e6ffe

0 0x420e8116 in lwip_netconn_do_newconn at ??:?

1 0x420e6ec0 in sys_thread_sem_get at ??:?

2 0x420e8116 in lwip_netconn_do_newconn at ??:?

3 0x420e6ffe in netconn_apimsg at api_lib.c.obj:?

3fca2060: 0x3fc9e7b8 0x00000000 0x3fca6c40 0x420e706a 0x3fc9e7b8 0x3fca49e0 0x3fca6c40 0x40389750

0 0x420e706a in netconn_new_with_proto_and_callback at ??:?

1 0x40389750 in multi_heap_free at ??:?

3fca2080: 0x3fc9e700 0x00000002 0x3fca49e0 0x403832b6 0x3fca4c3c 0x3fca23fc 0x00000007 0x3fc9e7f4

0 0x403832b6 in heap_caps_free at ??:?

3fca20a0: 0x3fc9e7b8 0x3fc91b08 0x3fc9e7b8 0x420d6612 0x3fc9e7b8 0x3fc91b08 0x3fc9e7b8 0x4207c334

0 0x420d6612 in lwip_socket at ??:?

1 0x4207c334 in NetworkServer::begin(unsigned short, int) at /home/dominik/.platformio/packages/framework-arduinoespressif32/libraries/Network/src/NetworkServer.cpp:81

  (inlined by) NetworkServer::begin(unsigned short) at /home/dominik/.platformio/packages/framework-arduinoespressif32/libraries/Network/src/NetworkServer.cpp:70

3fca20c0: 0x00000001 0x3fc91b08 0x3fca23f0 0x4202efbe 0x3fca2538 0x3fc91b08 0x00000004 0x4005893a

0 0x4202efbe in isvar(char, unsigned char, T_INDEX, float, char, GVARS) at tasmota/tasmota_xdrv_driver/xdrv_10_scripter.ino:6202

3fca20e0: 0x3fc9e7b8 0x3fc91b08 0x3fca23f0 0x4202efc8 0x00000000 0x000002fc 0x3fca2178 0x00000007

0 0x4202efc8 in NetworkServer::setNoDelay(bool) at /home/dominik/.platformio/packages/framework-arduinoespressif32/libraries/Network/src/NetworkServer.cpp:109

  (inlined by) isvar(char*, unsigned char*, T_INDEX*, float*, char*, GVARS*) at tasmota/tasmota_xdrv_driver/xdrv_10_scripter.ino:6203

3fca2100: 0x00000000 0x3fca4848 0x3fca4844 0x00000077 0x00000000 0x00000008 0x00000000 0x3fca4c43 3fca2120: 0x00000000 0x00000000 0x000000e2 0x00000001 0x00001000 0x3fca40e8 0x3fca40e8 0x00000003 3fca2140: 0x00000000 0x00000003 0x3fca4844 0x00000072 0x00000000 0xffffffff 0x00000000 0x3fca4c38 3fca2160: 0x00000010 0x00000000 0x3c1d7e6c 0x42091316 0x2ae5e1c3 0x3fca21d8 0x3c1dd370 0x4209136e

0 0x42091316 in _hashcode at lib/libesp32/berry/src/be_map.c:92

1 0x4209136e in eqnode at lib/libesp32/berry/src/be_map.c:113 (discriminator 1)

3fca2180: 0x500ffc18 0x00000004 0x3fca40e8 0x42103c86 0x3fca4870 0x3c1df3c8 0x3c1dd370 0x420913f4

0 0x42103c86 in lfs_dir_fetchmatch at lfs.c.obj:?

1 0x420913f4 in find at lib/libesp32/berry/src/be_map.c:185

3fca21a0: 0x00000011 0x00000016 0x3fca6ed0 0x3c1e1a58 0x3c1df3c8 0x3fca4870 0x3fca21d8 0x42091522

0 0x42091522 in be_map_find at lib/libesp32/berry/src/be_map.c:252

3fca21c0: 0x3fca4870 0x3fc9e1e8 0x3fca6ed0 0x42091542 0xc0380000 0x00000000 0x3c1e1a58 0x00000010

0 0x42091542 in be_map_findstr at lib/libesp32/berry/src/be_map.c:314

3fca21e0: 0x3fca4870 0x3fc9e1e8 0x3fca6ed0 0x42091570 0x00000011 0x00000016 0x3fca6ed0 0x3fca6f90

0 0x42091570 in instance_member at lib/libesp32/berry/src/be_class.c:139

3fca2200: 0x3fca6f98 0x3fca4870 0x3fc9e1e0 0x420911c2 0x00000011 0x00000016 0x0000000b 0x3fca6ed0

0 0x420911c2 in be_instance_member at lib/libesp32/berry/src/be_class.c:282

3fca2220: 0x3fca6f98 0x3fc9e1e0 0x3fca4870 0x420884bc 0x00000011 0x00000016 0x0000000b 0x00000001

0 0x420884bc in ins_member at lib/libesp32/berry/src/be_api.c:702

3fca2240: 0x3fc9e1c8 0x42093aee 0x3fca4870 0x4208b39e 0x3fc9e1c8 0x42093aee 0x3fca4870 0x42090e88

0 0x42093aee in m_findmember at lib/libesp32/berry/src/be_introspectlib.c:67

1 0x4208b39e in be_getmember at lib/libesp32/berry/src/be_api.c:706

2 0x42093aee in m_findmember at lib/libesp32/berry/src/be_introspectlib.c:67

3 0x42090e88 in be_execprotected at lib/libesp32/berry/src/be_exec.c:124

3fca2260: 0x00000013 0x3fca4870 0x4208ffce 0x3fca6f98 0x3fca2318 0x00ffffff 0x42090e7e 0x3fca4870

0 0x4208ffce in m_findmember_protected at lib/libesp32/berry/src/be_introspectlib.c:62

1 0x42090e7e in be_execprotected at lib/libesp32/berry/src/be_exec.c:121

3fca2280: 0x42093aee 0x3fc9e1c8 0x00000001 0x0000000b 0x00000016 0x00000011 0x00000012 0x286f7377

0 0x42093aee in m_findmember at lib/libesp32/berry/src/be_introspectlib.c:67

3fca22a0: 0x00323035 0x3fca2318 0x3c1738cc 0x42091316 0x45d41313 0x3fca2318 0x3c17841c 0x286f7377

0 0x42091316 in _hashcode at lib/libesp32/berry/src/be_map.c:92

3fca22c0: 0x00323035 0x3c1785b0 0x3c17841c 0x420913f4 0x3fca4870 0x3c1785b0 0x3c17841c 0x00736572

0 0x420913f4 in find at lib/libesp32/berry/src/be_map.c:185

3fca22e0: 0x00000011 0x00000016 0x3fc9dc80 0x3c1738cc 0x3c1785b0 0x3fca4870 0x3fca2318 0x43fb0000 3fca2300: 0x3fca4870 0x3fc9e1d8 0x3fc9dc80 0x42091542 0x00000010 0x3fca2388 0x3c1738cc 0x00000010

0 0x42091542 in be_map_findstr at lib/libesp32/berry/src/be_map.c:314

3fca2320: 0x3fca4870 0x3fc9e1d8 0x3fc9dc80 0x42091570 0x00000011 0x00000016 0x3fc9dc80 0x3c1738cc

0 0x42091570 in instance_member at lib/libesp32/berry/src/be_class.c:139

3fca2340: 0x3c16fffc 0x3fca4870 0x3fc9e1d8 0x42091152 0x3fca23f9 0x4214d536 0x3fca2488 0x3fca2850

0 0x42091152 in be_instance_member at lib/libesp32/berry/src/be_class.c:268

1 0x4214d536 in __ssputs_r at /builds/idf/crosstool-NG/.build/riscv32-esp-elf/src/newlib/newlib/libc/stdio/nano-vfprintf.c:179

3fca2360: 0x3fca23ac 0x00000000 0x3fca23e0 0x421473f6 0x00000011 0x00000016 0x0000000a 0x3c1ff000

0 0x421473f6 in _printf_common at /builds/idf/crosstool-NG/.build/riscv32-esp-elf/src/newlib/newlib/libc/stdio/nano-vfprintf_i.c:94

3fca2380: 0x3c1ff000 0x3fca25dc 0xffffffff 0x4214d536 0x3fca2488 0x3fca2850 0x3fca23e0 0x421476d4

0 0x4214d536 in __ssputs_r at /builds/idf/crosstool-NG/.build/riscv32-esp-elf/src/newlib/newlib/libc/stdio/nano-vfprintf.c:179

1 0x421476d4 in _printf_i at /builds/idf/crosstool-NG/.build/riscv32-esp-elf/src/newlib/newlib/libc/stdio/nano-vfprintf_i.c:241

3fca23a0: 0x40058c02 0x420334e4 0x3fca2420 0x3fc8c000 0x42033460 0x42033430 0x3fca2430 0x3fca483c

0 0x420334e4 in Run_script_sub(char const, signed char, GVARS) at tasmota/tasmota_xdrv_driver/xdrv_10_scripter.ino:8386

1 0x3fc8c000 in __global_pointer$ at ??:?

2 0x42033460 in Run_script_sub(char const, signed char, GVARS) at tasmota/tasmota_xdrv_driver/xdrv_10_scripter.ino:7521

3 0x42033430 in Run_script_sub(char const, signed char, GVARS) at tasmota/tasmota_xdrv_driver/xdrv_10_scripter.ino:7517

3fca23c0: 0x00000001 0xffffffff 0x000000fe 0x00000028 0x00000005 0x3fca2490 0x00000010 0x3fca2538 3fca23e0: 0x3fc91b68 0x00000001 0x3fca2530 0x420b27a8 0x3fca25f0 0x00000073 0x3fc91b68 0x00000000

0 0x420b27a8 in GetNumericArgument(char, unsigned char, float, GVARS*) [clone .constprop.0] at tasmota/tasmota_xdrv_driver/xdrv_10_scripter.ino:6676

3fca2400: 0x00000001 0xffffffff 0x00000000 0x3fc91b68 0x00000000 0x00000001 0xffffffff 0x00000000

ELF file SHA256: 717d4dc851561acc

Rebooting...


### TO REPRODUCE
1. Put the following script into your script window.
2. Enable script
3. Reboot
4. Observe crash in serial console
5. ESP reboots with script disabled

```lua
>D
res=0
>B
res=wso(8080)
print res=%res%

EXPECTED BEHAVIOUR

ESP boots normally and opens a tcp port.

SCREENSHOTS

N/A

ADDITIONAL CONTEXT

I tried various versions between 14.0 and develop as well as prebuilds I could find (sadly TCP_SERVER is not enabled by default), all of them crash the same way.

I also added extra log lines to the script interpreter during debugging and it seems to crash inside glob_script_mem.tcp_server->begin(); here.

While debugging I also noticed a secondary bug (however thats not my issue), that being if new WiFiServer(port) where to ever return null it would crash as well because it unconditionally references the pointer after setting the result code.

In addition theres a memory leak if a script calls wso more than once because it does not check/delete the WiFiServer instance before reassigning it.

gemu2015 commented 1 week ago

you may not open a tcp server or access any wifi related resources when wifi is not connected. on a reboot this is never the case during >B you can open the port when wifi is connected which you can check e.g during >S with 'wifis'

Thalhammer commented 1 week ago

you may not open a tcp server or access any wifi related resources when wifi is not connected.

Yeah that's what I assumed (though opening it with a timer of 1s still does before the wifi is connected and works fine), however I couldn't find anything related to that in the docs and all the example scripts on the internet I found (apparently) have it working fine in >BS.

However I think the other two issues I noted in additional context are still valid.