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

[BUG] Memory leak (lwip layer ?) when hit with SSL request (https://ip:80) #172

Closed
BeaverUI opened this issue Dec 15, 2024 · 41 comments · Fixed by #175 or #178
Closed

[BUG] Memory leak (lwip layer ?) when hit with SSL request (https://ip:80) #172

BeaverUI opened this issue Dec 15, 2024 · 41 comments · Fixed by #175 or #178
Assignees
Labels
bug help wanted Extra attention is needed

Comments

@BeaverUI
Copy link

BeaverUI commented Dec 15, 2024

Description

Running PlatformIO with espressif32@6.9.0, mathieucarbou AsyncTCP and ESPAsyncWebServer. Custom settings:
-D CONFIG_ASYNC_TCP_PRIORITY=10
-D CONFIG_ASYNC_TCP_QUEUE_SIZE=256
-D CONFIG_ASYNC_TCP_STACK_SIZE=8192
-D CONFIG_ASYNC_TCP_RUNNING_CORE=1

When a request is made through HTTPS to the HTTP port, the web server (or AsyncTCP, perhaps) crashes fairly reliably, instead of just returning a request error. Sometimes it requires a few CTRL-F5 attempts for the core panic to occur.

Stack trace

Guru Meditation Error: Core  0 panic'ed (Load access fault). Exception was unhandled.

Core  0 register dump:
MEPC    : 0x420d36b6  RA      : 0x420d3718  SP      : 0x3fca4450  GP      : 0x3fc8e200  
TP      : 0x3fc74f78  T0      : 0x00000006  T1      : 0xe6baac4d  T2      : 0x7201a8c0  
S0/FP   : 0x3fcb2d7c  S1      : 0x3fcb2d7c  A0      : 0x00000000  A1      : 0x3fcb2d7c  
A2      : 0x00000000  A3      : 0x3fcb2888  A4      : 0x00000000  A5      : 0x420d3710  
A6      : 0x00000000  A7      : 0x00000000  S2      : 0x3fc9a000  S3      : 0x3fc99386  
S4      : 0x3fc99380  S5      : 0x3fc99390  S6      : 0x3fc9a000  S7      : 0x3fc99384  
S8      : 0x3fc99388  S9      : 0x3fc94a38  S10     : 0x3fc993a6  S11     : 0x00000010  
T3      : 0x00000000  T4      : 0x00000000  T5      : 0x3fcb2d7c  T6      : 0x3fcb3cec  
MSTATUS : 0x00001881  MTVEC   : 0x40380001  MCAUSE  : 0x00000005  MTVAL   : 0x00000030  
MHARTID : 0x00000000  

Stack memory:
3fca4450: 0x3fc9a000 0x3fc9938c 0x3fcb2d7c 0x420d3718 0x3fc9a000 0x3fc9938c 0x3fcb2d7c 0x4205dac0
  #0  0x3fc9938c in ?? at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/lwip/lwip/src/core/tcp_in.c:85
  #1  0x420d3718 in AsyncServer::_s_accept(void*, tcp_pcb*, signed char) at lib/AsyncTCP/src/AsyncTCP.cpp:1591
  #2  0x3fc9938c in ?? at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/lwip/lwip/src/core/tcp_in.c:85
  #3  0x4205dac0 in tcp_process at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/lwip/lwip/src/core/tcp_in.c:947
      (inlined by) tcp_input at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/lwip/lwip/src/core/tcp_in.c:438

3fca4470: 0x3fca4d6c 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
3fca4490: 0x00000000 0x00000000 0x3fcb2d7c 0x00000000 0x00000000 0x00000000 0x00000001 0x00000000
3fca44b0: 0x3fc9a000 0x00000014 0x3fca622c 0x3fca622c 0x3fcb27c8 0x3fc99ad8 0x3fcb4caa 0x42063776
  #0  0x3fc99ad8 in ?? at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/lwip/lwip/src/core/ip.c:64
  #1  0x42063776 in ip4_input at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/lwip/lwip/src/core/ipv4/ip4.c:804

3fca44d0: 0x00000004 0x00000003 0x00000000 0x00000000 0x00000004 0x00000003 0x3fcb2598 0x00000002
3fca44f0: 0x3fca622c 0x00000008 0x3fcb27c8 0x42067ee2 0x3fc99000 0xffffffff 0x3fcb4054 0x42056896
  #0  0x42067ee2 in ethernet_input at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/lwip/lwip/src/netif/ethernet.c:253
  #1  0x42056896 in tcpip_thread_handle_msg at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/lwip/lwip/src/api/tcpip.c:180
      (inlined by) tcpip_thread at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/lwip/lwip/src/api/tcpip.c:154

3fca4510: 0x00000000 0x00000000 0x00000000 0x3fcb4054 0x00000000 0x00000000 0x00000000 0x00000000
3fca4530: 0x00000000 0x00000000 0x00000000 0x403897b4 0x00000000 0x00000000 0x00000000 0x00000000
  #0  0x403897b4 in _prvTaskExitError at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/port/riscv/port.c:126
      (inlined by) prvTaskExitError at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/port/riscv/port.c:140

3fca4550: 0x00000000 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xbaad5678
3fca4570: 0x00000160 0xabba1234 0x00000154 0x3fca43f0 0x00004c09 0x3fc95750 0x3fc95750 0x3fca457c
3fca4590: 0x3fc95748 0x00000007 0x3fca4d74 0x3fca4d74 0x3fca457c 0x00000000 0x00000012 0x3fca396c
3fca45b0: 0x00546974 0x00000000 0x40100000 0x001ca50c 0x00000000 0x3fca4560 0x00000012 0x00000000
3fca45d0: 0x00000000 0x00000000 0x00000000 0x3fc9a914 0x3fc9a97c 0x3fc9a9e4 0x00000000 0x00000000
3fca45f0: 0x00000001 0x00000000 0x00000000 0x00000000 0x420d49ae 0x00000000 0x00000000 0x00000000
  #0  0x420d49ae in _cleanup_r at /builds/idf/crosstool-NG/.build/riscv32-esp-elf/src/newlib/newlib/libc/stdio/findfp.c:229

3fca4610: 0x3fcb3998 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
3fca4630: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
3fca4650: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
3fca4670: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
3fca4690: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
3fca46b0: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
3fca46d0: 0xbaad5678 0x000000e0 0xabba1234 0x000000d4 0x3fca4734 0x3fca4754 0x3fca47b4 0x3fca4750
3fca46f0: 0x00000000 0x3fca46f8 0xffffffff 0x3fca46f8 0x3fca46f8 0x00000001 0x3fca470c 0xffffffff
3fca4710: 0x3fca341c 0x3fca341c 0x00000000 0x00000020 0x00000004 0x0000ffff 0x00000000 0xb33fffff
3fca4730: 0x00000000 0x3fcacf88 0x3fcad688 0x3fcad010 0x3fcac674 0x3fcacd4c 0x3fcadd00 0x3fcae0f8
3fca4750: 0x3fcaea30 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5
3fca4770: 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5
3fca4790: 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5
3fca47b0: 0xa5a5a5a5 0xbaad5678 0x00000060 0xabba1234 0x00000054 0x00000000 0x3fca47c4 0x00000000
3fca47d0: 0x00000000 0x00000000 0x3fca47dc 0xffffffff 0x3fca47dc 0x3fca47dc 0x00000000 0x3fca47f0
3fca47f0: 0xffffffff 0x3fca47f0 0x3fca47f0 0x00000001 0x00000001 0x00000000 0x0000ffff 0x00000000
3fca4810: 0xb33fffff 0x00000000 0xbaad5678 0x00000018 0xabba1234 0x0000000c 0x00000000 0x3fca4844
3fca4830: 0x00000000 0xbaad5678 0x0000001c 0xabba1234 0x00000010 0x3c1002e4 0x3fca4eec 0x3fca38d0

How can I avoid this? Some of my users are using port forwarding with random port numbers, which results in problems if you just enter the web address without adding http:// explicitly since the browser attempts HTTPS first for non-standard ports. Apart from that, crashing because of such requests should never occur imho.

@BeaverUI
Copy link
Author

BeaverUI commented Dec 15, 2024

Another coredump error I sometimes get is this one:

Guru Meditation Error: Core  0 panic'ed (Load access fault). Exception was unhandled.

Core  0 register dump:
MEPC    : 0x420d2dc6  RA      : 0x420d3128  SP      : 0x3fca3410  GP      : 0x3fc8e200  
TP      : 0x3fc73f58  T0      : 0x00000006  T1      : 0xb801a8c0  T2      : 0x7201a8c0  
S0/FP   : 0x00000000  S1      : 0x3fcb5a60  A0      : 0x00000000  A1      : 0x3fcb5a60  
A2      : 0x00000000  A3      : 0x00000000  A4      : 0x3fc99494  A5      : 0x3fcb5b40  
A6      : 0x000013e7  A7      : 0x0000059c  S2      : 0x00000000  S3      : 0x00000000  
S4      : 0x3fc99380  S5      : 0x3fc99390  S6      : 0x3fc9a000  S7      : 0x3fc99384  
S8      : 0x3fc99388  S9      : 0x3fc94a38  S10     : 0x3fc993a6  S11     : 0x00000011  
T3      : 0x00000000  T4      : 0x00000000  T5      : 0x3fcb4608  T6      : 0x00000000  
MSTATUS : 0x00001881  MTVEC   : 0x40380001  MCAUSE  : 0x00000005  MTVAL   : 0x00000000  
MHARTID : 0x00000000  

Stack memory:
3fca3410: 0x00000000 0x3fcb5a60 0x00000000 0x420d319c 0x3fca2868 0x00000000 0x3fc99388 0x3fcb5b40
  #0  0x420d319c in _tcp_recv(void*, tcp_pcb*, pbuf*, signed char) at lib/AsyncTCP/src/AsyncTCP.cpp:345
  #1  0x3fc99388 in ?? at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/lwip/lwip/src/core/tcp_in.c:86

3fca3430: 0x3fc9a000 0x3fc99390 0x3fc99380 0x3fc99386 0x3fc9a000 0x3fc9938c 0x3fcb5a60 0x4205dd84
  #0  0x3fc99390 in ?? at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/lwip/lwip/src/core/tcp_in.c:80
  #1  0x3fc99380 in ?? at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/lwip/lwip/src/core/tcp_in.c:80
  #2  0x3fc99386 in ?? at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/lwip/lwip/src/core/tcp_in.c:81
  #3  0x3fc9938c in ?? at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/lwip/lwip/src/core/tcp_in.c:85
  #4  0x4205dd84 in tcp_input at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/lwip/lwip/src/core/tcp_in.c:542 (discriminator 1)

3fca3450: 0x3fca288c 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
3fca3470: 0x00000000 0x00000000 0x3fcb5dc0 0x00000000 0x00000000 0x00000000 0x00000001 0x00000000
3fca3490: 0x3fc9a000 0x00000014 0x3fca5ed8 0x3fca5ed8 0x3fcb4470 0x3fc99ad8 0x3fcb746e 0x42063776
  #0  0x3fc99ad8 in ?? at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/lwip/lwip/src/core/ip.c:64
  #1  0x42063776 in ip4_input at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/lwip/lwip/src/core/ipv4/ip4.c:804

3fca34b0: 0x00000004 0x00000003 0x00000000 0x00000000 0x00000004 0x00000003 0x3fcb4238 0x00000002
3fca34d0: 0x3fca5ed8 0x00000008 0x3fcb4470 0x42067ee2 0x3fc99000 0xffffffff 0x3fcb47b0 0x42056896
  #0  0x42067ee2 in ethernet_input at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/lwip/lwip/src/netif/ethernet.c:253
  #1  0x42056896 in tcpip_thread_handle_msg at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/lwip/lwip/src/api/tcpip.c:180
      (inlined by) tcpip_thread at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/lwip/lwip/src/api/tcpip.c:154

3fca34f0: 0x00000000 0x00000000 0x00000000 0x3fcb47b0 0x00000000 0x00000000 0x00000000 0x00000000
3fca3510: 0x00000000 0x00000000 0x00000000 0x403897b4 0x00000000 0x00000000 0x00000000 0x00000000
  #0  0x403897b4 in _prvTaskExitError at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/port/riscv/port.c:126
      (inlined by) prvTaskExitError at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/port/riscv/port.c:140

3fca3530: 0x00000000 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xbaad5678
3fca3550: 0x00000160 0xabba1234 0x00000154 0x3fca3400 0x0000e34f 0x3fc95750 0x3fc95750 0x3fca355c
3fca3570: 0x3fc95748 0x00000007 0x3fca2894 0x3fca2894 0x3fca355c 0x00000000 0x00000012 0x3fca294c
3fca3590: 0x00546974 0x8ad41622 0x08c6e377 0x00b5bfe0 0x00000000 0x3fca3540 0x00000012 0x00000000
3fca35b0: 0x00000000 0x00000000 0x00000000 0x3fc9a914 0x3fc9a97c 0x3fc9a9e4 0x00000000 0x00000000
3fca35d0: 0x00000001 0x00000000 0x00000000 0x00000000 0x420d49ae 0x00000000 0x00000000 0x00000000
  #0  0x420d49ae in _cleanup_r at /builds/idf/crosstool-NG/.build/riscv32-esp-elf/src/newlib/newlib/libc/stdio/findfp.c:229

3fca35f0: 0x3fcb5f10 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
3fca3610: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
3fca3630: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
3fca3650: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
3fca3670: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
3fca3690: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
3fca36b0: 0xbaad5678 0x00000060 0xabba1234 0x00000054 0x3fca36c0 0x3fca36c0 0x3fca36c0 0x3fca36c0
3fca36d0: 0x00000000 0x3fca36d8 0xffffffff 0x3fca36d8 0x3fca36d8 0x00000000 0x3fca36ec 0xffffffff
3fca36f0: 0x3fca36ec 0x3fca36ec 0x00000001 0x00000001 0x00000000 0x0000ffff 0x00000000 0xb33fffff
3fca3710: 0x00000000 0xbaad5678 0x00000030 0xabba1234 0x00000024 0x00003038 0x00000000 0x3fca3730
3fca3730: 0xffffffff 0x3fca3730 0x3fca3730 0x00000000 0xb33fffff 0x00000000 0xbaad5678 0x000000e0
3fca3750: 0xabba1234 0x000000d4 0x3fca37ac 0x3fca37cc 0x3fca382c 0x3fca37c8 0x00000000 0x3fca3770
3fca3770: 0xffffffff 0x3fca3770 0x3fca3770 0x00000001 0x3fca3784 0xffffffff 0x3fca576c 0x3fca576c
3fca3790: 0x00000000 0x00000020 0x00000004 0x0000ffff 0x00000000 0xb33fffff 0x00000000 0x3fcacc34
3fca37b0: 0x3fcad334 0x3fcaccbc 0x3fcac320 0x3fcac9f8 0x3fcad9ac 0x3fcadda4 0x3fcae6dc 0x00000001
3fca37d0: 0x00000000 0x001a7d88 0x3fca3fac 0x00000013 0x00000000 0x001baf88 0x3fca3fd0 0x00000004
3fca37f0: 0x00000000 0x001ce788 0x3fca4008 0x00000000 0x403b8000 0x001da08c 0x3fca4040 0x0000002d

@mathieucarbou
Copy link
Owner

mathieucarbou commented Dec 15, 2024

Hello,

Your stack size and queue size are huge: this is possible you run out of memory.

Also, please do not post encoded stack traces like that: this is impossible to read and decrypt them.
Please follow the process described when opening an issue, or use PIO exception decoder.

There is nothing we can do so I will close.

Please reopen if:

  1. you are using decoded stack trace
  2. after having read it and fully made sure that the stack trace is not crashing because of your app or memory issues related to you
  3. with some more context detail or some more info like pointers to source code, reproductible use case, etc

Note: we are all very busy and maintaining this OSS project on our free time so please make sure when you open an issue, there is inside enough info for us to do something ;-)

Thanks.

@mathieucarbou
Copy link
Owner

#1 0x420d3718 in AsyncServer::_s_accept(void*, tcp_pcb*, signed char) at lib/AsyncTCP/src/AsyncTCP.cpp:1591

you do not seem to use latest versions.

please make sure to upgrade also before opening an issue ;-)

@BeaverUI
Copy link
Author

BeaverUI commented Dec 15, 2024

Many thanks for your prompt reply, highly appreciated!

Your stack size and queue size are huge: this is possible you run out of memory.

I did try reducing this before posting this issue, but it didn't influence the behaviour for HTTPS responses. Moreover, I need to transfer files of ~500 kB, and if I reduce the queue size and stack size, the ESP doesn't survive a stresstest (wdt triggers).

Also, please do not post encoded stack traces like that: this is impossible to read and decrypt them. Please follow the process described when opening an issue, or use PIO exception decoder.

I'm using a debug build with the decoder enabled, hence the for the '#0 0x403897b4 in _prvTaskExitError at /home/runner/work/' messages. For the life of me I've never seen any more advanced debugging messages. I just did some Googling and as far as I can see I've configured things correctly. Could you please point me in the right direction? That would help me out immensely.

There is nothing we can do so I will close.

While I understand you are very busy, and would like more advanced debugging data, I'm not sure this statement is true. I do see the behaviour in various implementations and am quite sure my code is OK since it runs fine otherwise.

Was it ever confirmed that repetitively sending HTTPS-requests to the HTTP server does not cause exceptions?

Note: we are all very busy and maintaining this OSS project on our free time so please make sure when you open an issue, there is inside enough info for us to do something ;-)

Your effort is highly appreciated! I hope you can point me in the direction of how to get more advanced exception decoding, so I can support as much as possible.

#1 0x420d3718 in AsyncServer::_s_accept(void*, tcp_pcb*, signed char) at lib/AsyncTCP/src/AsyncTCP.cpp:1591

you do not seem to use latest versions. please make sure to upgrade also before opening an issue ;-)

Thanks for the pointer, I recently pulled changes from the libs and assumed they were the latest versions. Will look into this as well.

@mathieucarbou
Copy link
Owner

  • AsyncTCP does not support HTTPS ;-)

  • AsyncTCP supports a max of 16 opened sockets (defined by esp-idf compiler flag of Arduino Core)

  • 500kB is not huge, but definitely slow if this is a file transfer. For this kind of usage, a workaround fix a has been implemented (see Watchdog in AsyncTCP when sending very long chunked response #165 (comment)) and a more comprehensive fix is in progress and will be released early next week. This should solve the watchdog triggering on slow file reading

best thing you could do is:

  1. upgrade
  2. test the next release that will be done soon (this evening or tomorrow that will contain an AsyncTCP improvement + in-flight buffer credits #171)

to decode exceptions you can place that in your pio file:

monitor_filters = esp32_exception_decoder

Please re-open the issue once you have:

  1. a proper stack trace
  2. some code to show what you do ;)

thanks!

@mathieucarbou
Copy link
Owner

@vortigont : FYI.

this might be related to #165 but not sure considering the little info we have....

@BeaverUI
Copy link
Author

I already had monitor_filters = esp32_exception_decoder in the ini, and build using debug. I honestly don't know what you're looking for; I think it can't decode some of the memory since it's from pre-built drivers/libs.

After updating AsyncTCP to v3.3.0 and ESPAsyncWebServer to v3.4.1, the error did change a bit:

Guru Meditation Error: Core  0 panic'ed (Load access fault). Exception was unhandled.

Core  0 register dump:
MEPC    : 0x420d16c8  RA      : 0x420d172a  SP      : 0x3fca3470  GP      : 0x3fc8e200
TP      : 0x3fc73e9c  T0      : 0x00000006  T1      : 0xb801a8c0  T2      : 0x7201a8c0
S0/FP   : 0x3fcada60  S1      : 0x3fcada60  A0      : 0x00000000  A1      : 0x3fcada60
A2      : 0x00000000  A3      : 0x3fcb2320  A4      : 0x00000000  A5      : 0x420d1722
A6      : 0x00000000  A7      : 0x00000000  S2      : 0x3fc9a000  S3      : 0x3fc99386
S4      : 0x3fc99380  S5      : 0x3fc99390  S6      : 0x3fc9a000  S7      : 0x3fc99384
S8      : 0x3fc99388  S9      : 0x3fc94a38  S10     : 0x3fc993a6  S11     : 0x00000010
T3      : 0x00000000  T4      : 0x00000000  T5      : 0x3fcada60  T6      : 0x3fcb23f8
MSTATUS : 0x00001881  MTVEC   : 0x40380001  MCAUSE  : 0x00000005  MTVAL   : 0x00000030
MHARTID : 0x00000000

Stack memory:
3fca3470: 0x3fc9a000 0x3fc9938c 0x3fcada60 0x420d172a 0x3fc9a000 0x3fc9938c 0x3fcada60 0x4205dab8   
  #0  0x3fc9938c in ?? at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/lwip/lwip/src/core/tcp_in.c:85
  #1  0x420d172a in ieee80211_output_process at ??:?
  #2  0x3fc9938c in ?? at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/lwip/lwip/src/core/tcp_in.c:85
  #3  0x4205dab8 in lfs_bd_flush at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/components/esp_littlefs/src/littlefs/lfs.c:179

3fca3490: 0x3fca28cc 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000   
3fca34b0: 0x7201a8c0 0x00000006 0x00000000 0x00000000 0x00000000 0x00000000 0x00000001 0x00000000   
3fca34d0: 0x3fc9a000 0x00000014 0x3fca5f18 0x3fca5f18 0x3fcb4560 0x3fc99ad8 0x3fcb397e 0x4206376e   
  #0  0x3fc99ad8 in ?? at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/lwip/lwip/src/core/ip.c:64
  #1  0x4206376e in elf_process_tasks_regs at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/espcoredump/src/core_dump_elf.c:411
      (inlined by) elf_write_tasks_data at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/espcoredump/src/core_dump_elf.c:442
      (inlined by) esp_core_dump_do_write_elf_pass at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/espcoredump/src/core_dump_elf.c:551

3fca34f0: 0x00000004 0x00000003 0x00000000 0x00000000 0x00000004 0x00000003 0x3fcb2008 0x00000002   
3fca3510: 0x3fca5f18 0x00000008 0x3fcb4560 0x42067eda 0x3fc99000 0xffffffff 0x3fcadb74 0x4205688e   
  #0  0x42067eda in sswu at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/wpa_supplicant/src/common/sae.c:692
  #1  0x4205688e in heap_caps_init at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/heap/heap_caps_init.c:98

3fca3530: 0x00000000 0x00000000 0x00000000 0x3fcadb74 0x00000000 0x00000000 0x00000000 0x00000000   
3fca3550: 0x00000000 0x00000000 0x00000000 0x403897b4 0x00000000 0x00000000 0x00000000 0x00000000   
  #0  0x403897b4 in _prvTaskExitError at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/port/riscv/port.c:126
      (inlined by) prvTaskExitError at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/port/riscv/port.c:140

3fca3570: 0x00000000 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5 0xbaad5678   
3fca3590: 0x00000160 0xabba1234 0x00000154 0x3fca3440 0x002654bc 0x3fc95750 0x3fc95750 0x3fca359c   
3fca35b0: 0x3fc95748 0x00000007 0x3fca28d4 0x3fca28d4 0x3fca359c 0x00000000 0x00000012 0x3fca298c   
3fca35d0: 0x00546974 0x00000014 0xabba1234 0x00000008 0x00000000 0x3fca3580 0x00000012 0x00000000   
3fca35f0: 0x00000000 0x00000000 0x00000000 0x3fc9a914 0x3fc9a97c 0x3fc9a9e4 0x00000000 0x00000000   
3fca3610: 0x00000001 0x00000000 0x00000000 0x00000000 0x420d4c6c 0x00000000 0x00000000 0x00000000   
  #0  0x420d4c6c in loop_node_remove_handler at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/esp_event/esp_event.c:387
      (inlined by) esp_event_handler_unregister_with_internal at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/esp_event/esp_event.c:793

3fca3630: 0x3fcb2260 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000   
3fca3650: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000   
3fca3670: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000   
3fca3690: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000   
3fca36b0: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000   
3fca36d0: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000   
3fca36f0: 0xbaad5678 0x00000060 0xabba1234 0x00000054 0x3fca3700 0x3fca3700 0x3fca3700 0x3fca3700   
3fca3710: 0x00000000 0x3fca3718 0xffffffff 0x3fca3718 0x3fca3718 0x00000000 0x3fca372c 0xffffffff   
3fca3730: 0x3fca372c 0x3fca372c 0x00000001 0x00000001 0x00000000 0x0000ffff 0x00000000 0xb33fffff   
3fca3750: 0x00000000 0xbaad5678 0x00000030 0xabba1234 0x00000024 0x00003038 0x00000000 0x3fca3770   
3fca3770: 0xffffffff 0x3fca3770 0x3fca3770 0x00000000 0xb33fffff 0x00000000 0xbaad5678 0x000000e0   
3fca3790: 0xabba1234 0x000000d4 0x3fca37ec 0x3fca3814 0x3fca386c 0x3fca3810 0x00000000 0x3fca37b0   
3fca37b0: 0xffffffff 0x3fca37b0 0x3fca37b0 0x00000001 0x3fca37c4 0xffffffff 0x3fca57ac 0x3fca57ac   
3fca37d0: 0x00000000 0x00000020 0x00000004 0x0000ffff 0x00000000 0xb33fffff 0x00000000 0x3fcacc74   
3fca37f0: 0x3fcad374 0x3fcaccfc 0x3fcac360 0x3fcaca38 0x3fcad90c 0x3fcadd5c 0x3fcae6d4 0x3fcb45cc   
3fca3810: 0x3fcb4540 0x001a0088 0x3fca3ff0 0x00000000 0x40318000 0x001b008c 0x3fca4014 0x00000004   
3fca3830: 0x00000000 0x001c0088 0x3fca404c 0x00000000 0x403b8000 0x001d008c 0x3fca4084 0x0000002d   
3fca3850: 0x00000000 0x001e0088 0x3fca40bc 0x0000002d 0x00000000 0x001f0088 0x3fca40f4 0xbaad5678

@BeaverUI
Copy link
Author

BeaverUI commented Dec 15, 2024

I'm happy to provide more information, but at the moment that doesn't seem possible (at least for me). However, triggering the problem is as easy as opening any webpage served by an ESP running your library through a browser (Firefox in my case) using https://ESP:80 a few times in a row.

@mathieucarbou
Copy link
Owner

mathieucarbou commented Dec 15, 2024

I am wondering if it could be because you are on an old arduino version (espressif32@6.9.0).

could you try switching to latest arduino version ?

platform = https://github.com/pioarduino/platform-espressif32/releases/download/53.03.10-rc3/platform-espressif32.zip

Since Arduino 3, you need to use pioarduino since PlatformIO dropped support for Arduino.

Also, which ESP board are you using ?

@BeaverUI
Copy link
Author

I didn't know that support was dropped, will take a look to migrate things. I'm using an ESP32-C3-MINI chipset btw.

@mathieucarbou
Copy link
Owner

Yes, sadly. The community is quite unhappy about that.

FYI ESP32-C3-MINI is single core, so you cannot have CONFIG_ASYNC_TCP_RUNNING_CORE=1.

ESPAsyncWS and ASyncTCP of course have more sense on dual-core.... If you are on single core, there is no point in using an asynchronous web server (although it should work).

You could directly use Arduino WebServer ;-)

Also, do you have this issue if you use other forks of AsyncTCP and ESPAsyncWS, or the original repos from me-no-dev ?

  • me-no-dev/ESPAsyncWebServer
  • me-no-dev/AsyncTCP

@vortigont
Copy link
Collaborator

a browser (Firefox in my case) using https://esp:80/ a few times in a row.

ugh that SSL-everywhere madness...
try to set browser.fixup.fallback-to-https to false in FF's about:config, it might help to remove such really stupid attempts to run SSL where it does not belongs to.

SSL is a binary protocol and can't be properly identified by current websever's code. We might try to improve it probably, but it would be an overhead code for a thing that should no be there anyway.

@BeaverUI
Copy link
Author

BeaverUI commented Dec 16, 2024

This morning I spent some time migrating everything to the latest platform (which required some code changes, but ala). Now, the good news is that the code runs like it used to. However, the bad news is that the code runs like it used to. It's perfectly stable until you do https://esp:80. This will crash it quite reliably.

I know I can set this parameter, however the people that use my hardware do not... They just open port forwarding on the router and have it crash the ESP every time they attempt to open the web page as the browsers attempt SSL first.

@mathieucarbou
Copy link
Owner

I already explained that AsyncTCP does not support SSL. Why are you still connecting with https to your esp ?

If you need SSL, use PsychicHttp, not ESPAsyncWS!

@BeaverUI
Copy link
Author

I believe there is a misunderstanding. My intention is NOT to use SSL. The intention is to avoid crashing when my users attempt to (accidentally) do so :)

@vortigont
Copy link
Collaborator

vortigont commented Dec 16, 2024

BTW, I can't reproduce it with curl, it just closes the connection with 404, no crash.
like this:
curl https://192.168.1.26:80/

* Connection from 192.168.1.100:46664
> ANY  HTTP/1.1
> �:
>
* Processed in 0 ms
< HTTP/1.1 404 Not Found
< connection: close

So it's not a 100% case and might depend on how webserver is set for your project.
I think you might want to start checking with a very basic example how it works and then compare on how handlers are set in your project. It might make a difference.

@BeaverUI
Copy link
Author

curl also doesn't cause a crash here, please test with Firefox instead.

@mathieucarbou
Copy link
Owner

Tried to run the SimpleServer sample in the project: https://192.168.4.1:80/ gives an ssl error in the browser and does not crash the esp, in safari, chrome, firefox on my Mac.

@BeaverUI
Copy link
Author

BeaverUI commented Dec 17, 2024

I did some more testing:

  • It seems only Firefox causes the issue. Chromium and curl (both on Linux) don't trigger the issue.
  • Most of the time I need to CTRL-F5 a few times to trigger it. Holding F5 definitely triggers it after a few seconds. Firefox reports "Secure Connection Failed. An error occurred during a connection to 192.168.1.114:80. SSL received a record that exceeded the maximum permissible length. Error code: SSL_ERROR_RX_RECORD_TOO_LONG". ESP reboots.
  • Updating the ESP platform from 6.9.0 to 52.03.10 didn't change the behavior
  • Updating ESPAsync and AsyncWS didn't solve it
  • I completely removed almost all handles from the code, leaving only the initialization. It still happens with this piece of code:
AsyncWebServer web_server(80);

void InitWebserver(void){
  web_server
    .on("/fwupdate", HTTP_GET, [](AsyncWebServerRequest *request){
PROGMEM const char * text = R"(
<html>
<head></head>
<body>
test
</body>
</html>
)";
      request->send(200, "text/html", text);
    });

  web_server.begin();
}

With the latest libraries, my coredump is as follows:

assert failed: tcp_update_rcv_ann_wnd /IDF/components/lwip/lwip/src/core/tcp.c:957 (new_rcv_ann_wnd <= 0xffff)
Core  0 register dump:
MEPC    : 0x40381994  RA      : 0x40386bf2  SP      : 0x3fcb7b00  GP      : 0x3fc8f600  
TP      : 0x3fcb7d40  T0      : 0x37363534  T1      : 0x7271706f  T2      : 0x33323130  
S0/FP   : 0x00000001  S1      : 0x3fcb7c9e  A0      : 0x3fcb7b64  A1      : 0x3fc91b51  
A2      : 0x00000001  A3      : 0x00000029  A4      : 0x00000001  A5      : 0x3fc9a000  
A6      : 0x7a797877  A7      : 0x76757473  S2      : 0x00000059  S3      : 0x3fcb7b58  
S4      : 0x3fcb7b58  S5      : 0x3c1124a8  S6      : 0x00000000  S7      : 0x00000000  
S8      : 0x00000000  S9      : 0x00000000  S10     : 0x00000000  S11     : 0x00000000  
T3      : 0x6e6d6c6b  T4      : 0x6a696867  T5      : 0x66656463  T6      : 0x62613938  
MSTATUS : 0x00001801  MTVEC   : 0x40380001  MCAUSE  : 0x00000007  MTVAL   : 0x00000000  
MHARTID : 0x00000000  

Stack memory:
3fcb7b00: 0x00000000 0x00000000 0x3c12078c 0x4038d89a 0x00000005 0x00000000 0x420820cc 0x3fc91b50
  #0  0x4038d89a in newlib_include_assert_impl at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/newlib/assert.c:92
  #1  0x420820cc in queue_send_wrapper at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/esp_wifi/esp32c3/esp_adapter.c:214
      (inlined by) queue_send_wrapper at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/esp_wifi/esp32c3/esp_adapter.c:211
  #2  0x3fc91b50 in __global_pointer$ at ??:?

3fcb7b20: 0x00000001 0x00000024 0x00000005 0x00373539 0x3fcb89da 0x3fc91eb8 0x3c12078c 0x3fc9217c
  #0  0x3fc91eb8 in __global_pointer$ at ??:?
  #1  0x3fc9217c in __global_pointer$ at ??:?

3fcb7b40: 0x3c1124a8 0x3fc91ec8 0x3fcb7b2c 0x3fc91ecc 0x3c11254c 0x3fc91b50 0x00000000 0x00000000
  #0  0x3fc91ec8 in __global_pointer$ at ??:?
  #1  0x3fc91ecc in __global_pointer$ at ??:?
  #2  0x3fc91b50 in __global_pointer$ at ??:?

3fcb7b60: 0x3f000000 0x65737361 0x66207472 0x656c6961 0x74203a64 0x755f7063 0x74616470 0x63725f65
3fcb7b80: 0x6e615f76 0x6e775f6e 0x492f2064 0x632f4644 0x6f706d6f 0x746e656e 0x776c2f73 0x6c2f7069
3fcb7ba0: 0x2f706977 0x2f637273 0x65726f63 0x7063742f 0x393a632e 0x28203735 0x5f77656e 0x5f766372
3fcb7bc0: 0x5f6e6e61 0x20646e77 0x30203d3c 0x66666678 0x3f002966 0x00000000 0x00000001 0x00000001
3fcb7be0: 0x3fc9a000 0x3fc9a000 0x00000000 0x600c2194 0x3fc96968 0x00000000 0x00000000 0x40387710
  #0  0x3fc9a000 in ?? at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/spi_flash/flash_ops.c:92
  #1  0x3fc9a000 in ?? at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/spi_flash/flash_ops.c:92
  #2  0x3fc96968 in ?? at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/lwip/lwip/src/core/ipv4/etharp.c:105
  #3  0x40387710 in xQueueSemaphoreTake at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/FreeRTOS-Kernel/queue.c:1731

3fcb7c00: 0xffffffff 0x00000002 0x3fca5084 0xffffffff 0x00000000 0x00000000 0x3fca5e2c 0x40386c30
  #0  0x40386c30 in prvCopyDataToQueue at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/FreeRTOS-Kernel/queue.c:2470

3fcb7c20: 0x00000000 0x00000000 0x3fca5e2c 0x786a5ad2 0x3fce0000 0x00000000 0x00000000 0x00000000
3fcb7c40: 0x3fc9a000 0x3fcb99f4 0x00000007 0x4205301a 0x00000000 0x00000000 0x00000007 0x42053102
  #0  0x3fc9a000 in ?? at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/spi_flash/flash_ops.c:92
  #1  0x4205301a in tcp_update_rcv_ann_wnd at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/lwip/lwip/src/core/tcp.c:957 (discriminator 1)
  #2  0x42053102 in tcp_recved at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/lwip/lwip/src/core/tcp.c:1003

3fcb7c60: 0x3fc9a000 0x3fcb7c98 0x3fcb7c98 0x420d877a 0x00000002 0x00000061 0x420d8744 0x4204e7ae
  #0  0x3fc9a000 in ?? at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/spi_flash/flash_ops.c:92
  #1  0x420d877a in _tcp_recved_api(tcpip_api_call_data*) at .pio/libdeps/debug/AsyncTCP/src/AsyncTCP.cpp:566
  #2  0x420d8744 in _tcp_recved_api(tcpip_api_call_data*) at .pio/libdeps/debug/AsyncTCP/src/AsyncTCP.cpp:556
  #3  0x4204e7ae in tcpip_api_call at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/lwip/lwip/src/api/tcpip.c:478

3fcb7c80: 0x00000000 0x3fcb9eac 0x3fcb9b44 0x420d860c 0x00000000 0x00000000 0x3fcb9b44 0x3fcb99f4
  #0  0x420d860c in _tcp_recved(tcp_pcb*, signed char, unsigned int) at .pio/libdeps/debug/AsyncTCP/src/AsyncTCP.cpp:577

3fcb7ca0: 0x00000004 0x00000007 0x3fcb9b44 0x3fcb9c58 0x00000000 0x00000000 0x00000000 0x420d98ec
  #0  0x420d98ec in AsyncClient::_recv(tcp_pcb*, pbuf*, signed char) at .pio/libdeps/debug/AsyncTCP/src/AsyncTCP.cpp:1098

3fcb7cc0: 0x00000000 0x00000000 0x3fcbb2c8 0x420d9904 0x00000000 0x00000000 0x3fcbb398 0x420d9a46
  #0  0x420d9904 in AsyncClient::_s_recv(void*, tcp_pcb*, pbuf*, signed char) at .pio/libdeps/debug/AsyncTCP/src/AsyncTCP.cpp:1488
  #1  0x420d9a46 in _handle_async_event(lwip_event_packet_t*) at .pio/libdeps/debug/AsyncTCP/src/AsyncTCP.cpp:272

3fcb7ce0: 0x3fc9a000 0x00000001 0x00000000 0x420d9a9a 0x00000000 0x00000000 0x00000000 0x3fcbb2c8
  #0  0x3fc9a000 in ?? at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/spi_flash/flash_ops.c:92
  #1  0x420d9a9a in _async_service_task(void*) at .pio/libdeps/debug/AsyncTCP/src/AsyncTCP.cpp:310

3fcb7d00: 0x00000000 0x00000000 0x00000000 0x40387ed8 0x00000000 0x00000000 0x00000000 0x00000000
  #0  0x40387ed8 in vPortTaskWrapper at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/FreeRTOS-Kernel/portable/riscv/port.c:258

3fcb7d20: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0xa5a5a5a5 0xa5a5a5a5 0xa5a5a5a5
3fcb7d40: 0xa5a5a5a5 0xbaad5678 0x00000168 0xabba1234 0x0000015c 0x3fcb7bb0 0x000b2496 0x3fc95240
3fcb7d60: 0x3fc95240 0x3fcb7d54 0x3fc95238 0x0000000f 0x3fcb5b04 0x3fcb5b04 0x3fcb7d54 0x00000000
3fcb7d80: 0x0000000a 0x3fcb5d44 0x6e797361 0x63745f63 0x00000070 0x00000000 0x3fcb7d40 0x00000012
3fcb7da0: 0x00000000 0x0000000a 0x00000001 0x3fcb9f7c 0x420404fc 0x001ace43 0x00000000 0x3fc9ac94
  #0  0x420404fc in pthread_cleanup_thread_specific_data_callback at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/pthread/pthread_local_storage.c:126

3fcb7dc0: 0x3fc9acfc 0x3fc9ad64 0x00000000 0x00000000 0x00000001 0x00000000 0x00000000 0x00000000
3fcb7de0: 0x4203fa9e 0x00000000 0x00000000 0x00000000 0x3fcb8380 0x00000000 0x00000000 0x00000000
  #0  0x4203fa9e in esp_cleanup_r at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/newlib/newlib_init.c:43

3fcb7e00: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
3fcb7e20: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
3fcb7e40: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
3fcb7e60: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
3fcb7e80: 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000 0x00000000
3fcb7ea0: 0x00000000 0x00000000 0x00000000 0x00000000 0xbaad5678 0x0000001c 0xabba1234 0x00000010
3fcb7ec0: 0x00000000 0x3fcb7d54 0x00000000 0x00000000 0xbaad5678 0x0000001c 0xabba1234 0x0000000d
3fcb7ee0: 0x2e323931 0x2e383631 0x31312e31 0xad567800 0x3fcb7eba 0x0000002c 0xabba1234 0x00000020

Note the message assert failed: tcp_update_rcv_ann_wnd /IDF/components/lwip/lwip/src/core/tcp.c:957 (new_rcv_ann_wnd <= 0xffff), which could be related to mathieucarbou/AsyncTCP#26

@mathieucarbou
Copy link
Owner

I was finally able to reproduce with FF, hitting refresh a couple f time with https://192.168.4.1:80/.

I think we could add a feature inside AsyncWebServerRequest::_onData(void* buf, size_t len) to detect ssl traffic and avoid crashing the ESP... I'll dig into it...

@mathieucarbou mathieucarbou self-assigned this Dec 17, 2024
@mathieucarbou
Copy link
Owner

v3.4.3 released!

@BeaverUI
Copy link
Author

No worries, it's great you picked it up. There is a memory leak now though :-P

@mathieucarbou
Copy link
Owner

No worries, it's great you picked it up. There is a memory leak now though :-P

oh crap where ?

@BeaverUI
Copy link
Author

no idea; every time I do an F5 with HTTPS about 2kb is lost.

@BeaverUI
Copy link
Author

perhaps _client->close() is leaky?

@mathieucarbou
Copy link
Owner

mathieucarbou commented Dec 17, 2024

I am wondering if this is the created request object which is never sent... I will have a look.

edit

Oh! thanks a lot for the support btw 😃

@mathieucarbou
Copy link
Owner

I can reproduce and I fear this memory leak was caused by something else because as we can see, the disconnect handler is closed, which is deleting the request object.

@vortigont did some change recently in AsyncTCP queues so I will check with a previous version of AsyncTCP

[ 25004][D][WebRequest.cpp:76] _onData(): Free Heap: 200816
[ 25011][D][WebRequest.cpp:77] _onData(): SSL/TLS handshake detected: closing connection
[ 25503][D][WebServer.cpp:138] _handleDisconnect(): Client disconnected
[ 25520][D][WebServer.cpp:138] _handleDisconnect(): Client disconnected
[ 25528][D][WebRequest.cpp:76] _onData(): Free Heap: 198920
[ 25534][D][WebRequest.cpp:77] _onData(): SSL/TLS handshake detected: closing connection
[ 25546][D][WebServer.cpp:138] _handleDisconnect(): Client disconnected
[ 25554][D][WebRequest.cpp:76] _onData(): Free Heap: 200192
[ 25560][D][WebRequest.cpp:77] _onData(): SSL/TLS handshake detected: closing connection
[ 25570][D][WebServer.cpp:138] _handleDisconnect(): Client disconnected
[ 25577][D][WebRequest.cpp:76] _onData(): Free Heap: 199280
[ 25584][D][WebRequest.cpp:77] _onData(): SSL/TLS handshake detected: closing connection
[ 25594][D][WebServer.cpp:138] _handleDisconnect(): Client disconnected
[ 25601][D][WebRequest.cpp:76] _onData(): Free Heap: 199960
[ 25608][D][WebRequest.cpp:77] _onData(): SSL/TLS handshake detected: closing connection
[ 26333][D][WebServer.cpp:138] _handleDisconnect(): Client disconnected
[ 26341][D][WebRequest.cpp:76] _onData(): Free Heap: 197836
[ 26347][D][WebRequest.cpp:77] _onData(): SSL/TLS handshake detected: closing connection
[ 26357][D][WebServer.cpp:138] _handleDisconnect(): Client disconnected
[ 26365][D][WebRequest.cpp:76] _onData(): Free Heap: 198692
[ 26371][D][WebRequest.cpp:77] _onData(): SSL/TLS handshake detected: closing connection
[ 26381][D][WebServer.cpp:138] _handleDisconnect(): Client disconnected
[ 26388][D][WebRequest.cpp:76] _onData(): Free Heap: 198572
[ 26395][D][WebRequest.cpp:77] _onData(): SSL/TLS handshake detected: closing connection
[ 26404][D][WebServer.cpp:138] _handleDisconnect(): Client disconnected
[ 26412][D][WebRequest.cpp:76] _onData(): Free Heap: 198900
[ 26418][D][WebRequest.cpp:77] _onData(): SSL/TLS handshake detected: closing connection
[ 29673][D][AsyncTCP.cpp:1131] _poll(): rx timeout 4
[ 29680][D][WebServer.cpp:138] _handleDisconnect(): Client disconnected

@mathieucarbou
Copy link
Owner

mathieucarbou commented Dec 17, 2024

Ok so this is not caused by AsyncTCP changes. The request object is correctly deleted:

https://192.168.4.1:80/ (1 hit creates 2 or more requests)

[2214841][D][WebRequest.cpp:70] _onData(): Request pointer: 0x3ffd4368
[2214848][D][WebRequest.cpp:71] _onData(): Free Heap: 214964
[2214855][D][WebRequest.cpp:77] _onData(): SSL/TLS handshake detected: closing connection
[2214866][D][WebRequest.cpp:221] _onDisconnect(): Client disconnected
[2214873][D][WebServer.cpp:138] _handleDisconnect(): Deleting request pointer: 0x3ffd4368
[2214882][D][WebRequest.cpp:70] _onData(): Request pointer: 0x3ffd4234
[2214890][D][WebRequest.cpp:71] _onData(): Free Heap: 215592
[2214896][D][WebRequest.cpp:77] _onData(): SSL/TLS handshake detected: closing connection
[2214906][D][WebRequest.cpp:221] _onDisconnect(): Client disconnected
[2214913][D][WebServer.cpp:138] _handleDisconnect(): Deleting request pointer: 0x3ffd4234
[2215330][D][WebRequest.cpp:70] _onData(): Request pointer: 0x3ffd4918
[2215337][D][WebRequest.cpp:71] _onData(): Free Heap: 213808
[2215344][D][WebRequest.cpp:77] _onData(): SSL/TLS handshake detected: closing connection
[2215354][D][WebRequest.cpp:221] _onDisconnect(): Client disconnected
[2215361][D][WebServer.cpp:138] _handleDisconnect(): Deleting request pointer: 0x3ffd4918
[2215370][D][WebRequest.cpp:70] _onData(): Request pointer: 0x3ffd4234
[2215378][D][WebRequest.cpp:71] _onData(): Free Heap: 214668
[2215384][D][WebRequest.cpp:77] _onData(): SSL/TLS handshake detected: closing connection
[2215394][D][WebRequest.cpp:221] _onDisconnect(): Client disconnected
[2215401][D][WebServer.cpp:138] _handleDisconnect(): Deleting request pointer: 0x3ffd4234
[2216144][D][WebRequest.cpp:70] _onData(): Request pointer: 0x3ffd563c
[2216152][D][WebRequest.cpp:71] _onData(): Free Heap: 212884
[2216158][D][WebRequest.cpp:77] _onData(): SSL/TLS handshake detected: closing connection
[2216169][D][WebRequest.cpp:221] _onDisconnect(): Client disconnected
[2216176][D][WebServer.cpp:138] _handleDisconnect(): Deleting request pointer: 0x3ffd563c
[2216185][D][WebRequest.cpp:70] _onData(): Request pointer: 0x3ffd4234
[2216193][D][WebRequest.cpp:71] _onData(): Free Heap: 213520
[2216199][D][WebRequest.cpp:77] _onData(): SSL/TLS handshake detected: closing connection
[2216209][D][WebRequest.cpp:221] _onDisconnect(): Client disconnected
[2216216][D][WebServer.cpp:138] _handleDisconnect(): Deleting request pointer: 0x3ffd4234

http://192.168.4.1/ (several hits)

[2084760][D][WebRequest.cpp:70] _onData(): Request pointer: 0x3ffd4150
[2084767][D][WebRequest.cpp:71] _onData(): Free Heap: 216664
[2084809][D][WebRequest.cpp:221] _onDisconnect(): Client disconnected
[2084817][D][WebServer.cpp:138] _handleDisconnect(): Deleting request pointer: 0x3ffd4150
[2089123][D][WebRequest.cpp:70] _onData(): Request pointer: 0x3ffd411c
[2089130][D][WebRequest.cpp:71] _onData(): Free Heap: 216448
[2089163][D][WebRequest.cpp:221] _onDisconnect(): Client disconnected
[2089171][D][WebServer.cpp:138] _handleDisconnect(): Deleting request pointer: 0x3ffd411c
[2184575][D][WebRequest.cpp:70] _onData(): Request pointer: 0x3ffd42c0
[2184583][D][WebRequest.cpp:71] _onData(): Free Heap: 216808
[2184611][D][WebRequest.cpp:221] _onDisconnect(): Client disconnected
[2184618][D][WebServer.cpp:138] _handleDisconnect(): Deleting request pointer: 0x3ffd42c0
[2188501][D][WebRequest.cpp:70] _onData(): Request pointer: 0x3ffd42c0
[2188508][D][WebRequest.cpp:71] _onData(): Free Heap: 216808
[2188531][D][WebRequest.cpp:221] _onDisconnect(): Client disconnected
[2188538][D][WebServer.cpp:138] _handleDisconnect(): Deleting request pointer: 0x3ffd42c0
[2190425][D][WebRequest.cpp:70] _onData(): Request pointer: 0x3ffd42c0
[2190433][D][WebRequest.cpp:71] _onData(): Free Heap: 216808
[2190453][D][WebRequest.cpp:221] _onDisconnect(): Client disconnected
[2190461][D][WebServer.cpp:138] _handleDisconnect(): Deleting request pointer: 0x3ffd42c0
[2191723][D][WebRequest.cpp:70] _onData(): Request pointer: 0x3ffd42c0
[2191731][D][WebRequest.cpp:71] _onData(): Free Heap: 216808
[2191764][D][WebRequest.cpp:221] _onDisconnect(): Client disconnected
[2191772][D][WebServer.cpp:138] _handleDisconnect(): Deleting request pointer: 0x3ffd42c0
[2192965][D][WebRequest.cpp:70] _onData(): Request pointer: 0x3ffd42c0
[2192972][D][WebRequest.cpp:71] _onData(): Free Heap: 216808
[2193002][D][WebRequest.cpp:221] _onDisconnect(): Client disconnected
[2193010][D][WebServer.cpp:138] _handleDisconnect(): Deleting request pointer: 0x3ffd42c0
[2194146][D][WebRequest.cpp:70] _onData(): Request pointer: 0x3ffd42c0
[2194153][D][WebRequest.cpp:71] _onData(): Free Heap: 216808
[2194180][D][WebRequest.cpp:221] _onDisconnect(): Client disconnected
[2194187][D][WebServer.cpp:138] _handleDisconnect(): Deleting request pointer: 0x3ffd42c0```

@mathieucarbou
Copy link
Owner

mathieucarbou commented Dec 17, 2024

@vortigont : fyi I also checked line 1104 of AsyncTCP pbuf_free is correclty freeing the buffer.

I suspect the issue to be before ESPAsyncWS because the data from a new client arrives, a new request is created (AsyncWebServerRequest* r = new AsyncWebServerRequest((AsyncWebServer*)s, c);), then onData is called, the new code intercepts the SSL/TLS handshake and close the client and returns. Closing the client causes the disconnect handler to be called, which deletes the request, and the client.

All that within the call of _recv_cb(_recv_cb_arg, this, b->payload, b->len); in AsyncTCP line 1095.

Then, pbuf_free is called.

This one is a hard one lol!

@vortigont
Copy link
Collaborator

vortigont commented Dec 18, 2024

it's not that scary actually
for me it releases after some time, but maybe loosing just a dozen of bytes or so...
interval between heap: messages is 3 sec.
I'm using curl

heap:263976
heap:263976
heap:263976
heap:263976
heap:263976
[504805][D][WebRequest.cpp:76] _onData(): SSL/TLS handshake detected: closing connection
[506235][D][WebRequest.cpp:76] _onData(): SSL/TLS handshake detected: closing connection
heap:263496
[507259][D][WebRequest.cpp:76] _onData(): SSL/TLS handshake detected: closing connection
[508091][D][WebRequest.cpp:76] _onData(): SSL/TLS handshake detected: closing connection
[509109][D][WebRequest.cpp:76] _onData(): SSL/TLS handshake detected: closing connection
[510127][D][WebRequest.cpp:76] _onData(): SSL/TLS handshake detected: closing connection
heap:262600
[511357][D][WebRequest.cpp:76] _onData(): SSL/TLS handshake detected: closing connection
[512381][D][WebRequest.cpp:76] _onData(): SSL/TLS handshake detected: closing connection
heap:262152
[513812][D][WebRequest.cpp:76] _onData(): SSL/TLS handshake detected: closing connection
[514839][D][WebRequest.cpp:76] _onData(): SSL/TLS handshake detected: closing connection
[515656][D][WebRequest.cpp:76] _onData(): SSL/TLS handshake detected: closing connection
heap:261480
[516683][D][WebRequest.cpp:76] _onData(): SSL/TLS handshake detected: closing connection
[517909][D][WebRequest.cpp:76] _onData(): SSL/TLS handshake detected: closing connection
[518935][D][WebRequest.cpp:76] _onData(): SSL/TLS handshake detected: closing connection
heap:261704
[519753][D][WebRequest.cpp:76] _onData(): SSL/TLS handshake detected: closing connection
[520572][D][WebRequest.cpp:76] _onData(): SSL/TLS handshake detected: closing connection
[521597][D][WebRequest.cpp:76] _onData(): SSL/TLS handshake detected: closing connection
heap:261928
heap:262600
heap:263272
[530813][D][WebRequest.cpp:76] _onData(): SSL/TLS handshake detected: closing connection
heap:263272
[532451][D][WebRequest.cpp:76] _onData(): SSL/TLS handshake detected: closing connection
[533069][D][WebRequest.cpp:76] _onData(): SSL/TLS handshake detected: closing connection
[533683][D][WebRequest.cpp:76] _onData(): SSL/TLS handshake detected: closing connection
[534103][D][WebRequest.cpp:76] _onData(): SSL/TLS handshake detected: closing connection
heap:263496
[534704][D][WebRequest.cpp:76] _onData(): SSL/TLS handshake detected: closing connection
[535318][D][WebRequest.cpp:76] _onData(): SSL/TLS handshake detected: closing connection
[535933][D][WebRequest.cpp:76] _onData(): SSL/TLS handshake detected: closing connection
heap:263496
heap:263496
heap:263496
heap:263976
heap:263976
heap:263976
heap:263976

@mathieucarbou
Copy link
Owner

I am wondering if it is browser dependent. For exemple, Firefox seems to retry. Also, even if globally, issuing several refresh does decrease the heap, sometimes, I see the feee heap value that is going up again. But not to the point where it was at start.

Searching on Google, I saw some older issues regarding SO_LINGER not properly implemented: set to 0 by default, which should make the esp-idf layer discard the packets when connection closes, but those issues were reporting that the code is not cleaning, and users had to activate SO_LINGER and set the cleanup time to 0 in order to force an immediate buffer release once the connection closes.

But whether this issue is still there or not, we do not have control over that. on Arduino Core.

Strangely, esp-idf http server seems to have control over that:

https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/protocols/esp_http_server.html?highlight=linger#_CPPv4N12httpd_config16enable_so_lingerE

@mathieucarbou
Copy link
Owner

Out of curiosity, I tested with AsyncTCPSock, which is based on lwip/sockets, like the esp-idf implementation of http server.

Hitting refresh consecutively also does decreases the heap.

So I added this to activate so_linger with a 0 delay

        struct linger so_linger = {
            .l_onoff = true,
            .l_linger = 0,
        };
        setsockopt(accepted_sockfd, SOL_SOCKET, SO_LINGER, &so_linger, sizeof(struct linger));

And the free heap still decreases. But a little less than with AsyncTCP.

So its not that.

@vortigont
Copy link
Collaborator

my 5 cents that it's lwip's internal allocations and garbage collection. It should keep tcp struct lingering for some time by the standard.

It would be much easy to use _client->abort(); instead of _client->close();. It will simply send TCP RST and release all related structs instead of close via fin/ack sequence.

@mathieucarbou mathieucarbou changed the title [FEAT] Avoid crashing the ESP if SSL is used on HTTP port (i.e. https://192.168.4.1:80/) [BUG] Memory leak (lwip layer ?) when hit with SSL request (https://ip:80) Dec 19, 2024
@mathieucarbou mathieucarbou added bug help wanted Extra attention is needed and removed feature labels Dec 19, 2024
@mathieucarbou mathieucarbou linked a pull request Dec 19, 2024 that will close this issue
@mathieucarbou
Copy link
Owner

@BeaverUI : the memory leak is fixed in v3.4.4!

@BeaverUI
Copy link
Author

many thanks, works perfectly

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug help wanted Extra attention is needed
Projects
None yet
3 participants