Skip to content

Conversation

@78
Copy link
Owner

@78 78 commented Jan 5, 2026

This pull request updates the WebSocket implementation with several improvements and bug fixes, primarily focusing on better handling of message fragmentation, thread safety, and minor protocol correctness. It also updates the component metadata.

WebSocket implementation improvements:

  • Moved current_message, is_fragmented, and is_binary from static variables inside OnTcpData to member variables of the WebSocket class, improving thread safety and correctness for handling concurrent connections. (include/web_socket.h, src/web_socket.cc) [1] [2] [3]
  • Fixed payload length decoding for extended payloads by properly casting to uint64_t, ensuring correct handling of large WebSocket frames. (src/web_socket.cc)
  • Added a check to only copy and unmask payload data if payload_length > 0, preventing potential issues with zero-length frames. (src/web_socket.cc)
  • Removed unnecessary threading for sending Pong responses (Ping frames are now handled synchronously), simplifying the code. (src/web_socket.cc)

Component metadata updates:

  • Changed the license to Apache-2.0 and bumped the version to 3.5.4 in idf_component.yml.… and improve data processing logic.

@78
Copy link
Owner Author

78 commented Jan 5, 2026

Try to fix PR #34

@hushi55
Copy link
Contributor

hushi55 commented Jan 6, 2026

(base) m4:mm-esp32-voice hushi.hs$ xtensa-esp32-elf-addr2line -pfiaC -e build/mm-audio.elf 0x403882c9
0x403882c9: panic_abort at /Users/hushi.hs/Documents/WorkSpace/esp/esp-idf/components/esp_system/panic.c:477
(base) m4:mm-esp32-voice hushi.hs$ xtensa-esp32-elf-addr2line -pfiaC -e build/mm-audio.elf 0x40388291
0x40388291: esp_system_abort at /Users/hushi.hs/Documents/WorkSpace/esp/esp-idf/components/esp_system/port/esp_system_chip.c:87
(base) m4:mm-esp32-voice hushi.hs$ xtensa-esp32-elf-addr2line -pfiaC -e build/mm-audio.elf 0x40390031
0x40390031: __assert_func at /Users/hushi.hs/Documents/WorkSpace/esp/esp-idf/components/newlib/src/assert.c:80
(base) m4:mm-esp32-voice hushi.hs$ xtensa-esp32-elf-addr2line -pfiaC -e build/mm-audio.elf 0x4038f159
0x4038f159: search_suitable_block at /Users/hushi.hs/Documents/WorkSpace/esp/esp-idf/components/heap/tlsf/tlsf_control_functions.h:359 (discriminator 1)
(inlined by) block_locate_free at /Users/hushi.hs/Documents/WorkSpace/esp/esp-idf/components/heap/tlsf/tlsf_control_functions.h:612 (discriminator 1)
(inlined by) tlsf_malloc at /Users/hushi.hs/Documents/WorkSpace/esp/esp-idf/components/heap/tlsf/tlsf.c:444 (discriminator 1)
(base) m4:mm-esp32-voice hushi.hs$ xtensa-esp32-elf-addr2line -pfiaC -e build/mm-audio.elf 0x4038ed11
0x4038ed11: multi_heap_malloc_impl at /Users/hushi.hs/Documents/WorkSpace/esp/esp-idf/components/heap/multi_heap.c:216
(base) m4:mm-esp32-voice hushi.hs$ xtensa-esp32-elf-addr2line -pfiaC -e build/mm-audio.elf 0x4037a267
0x4037a267: aligned_or_unaligned_alloc at /Users/hushi.hs/Documents/WorkSpace/esp/esp-idf/components/heap/heap_caps_base.c:93
(inlined by) heap_caps_aligned_alloc_base at /Users/hushi.hs/Documents/WorkSpace/esp/esp-idf/components/heap/heap_caps_base.c:175
(base) m4:mm-esp32-voice hushi.hs$ xtensa-esp32-elf-addr2line -pfiaC -e build/mm-audio.elf 0x4037a281
0x4037a281: heap_caps_malloc_base at /Users/hushi.hs/Documents/WorkSpace/esp/esp-idf/components/heap/heap_caps_base.c:202
(base) m4:mm-esp32-voice hushi.hs$ xtensa-esp32-elf-addr2line -pfiaC -e build/mm-audio.elf 0x40379f79
0x40379f79: heap_caps_malloc_default at /Users/hushi.hs/Documents/WorkSpace/esp/esp-idf/components/heap/heap_caps.c:124
(base) m4:mm-esp32-voice hushi.hs$ xtensa-esp32-elf-addr2line -pfiaC -e build/mm-audio.elf 0x403900ad
0x403900ad: malloc at /Users/hushi.hs/Documents/WorkSpace/esp/esp-idf/components/newlib/src/heap.c:24
(base) m4:mm-esp32-voice hushi.hs$ xtensa-esp32-elf-addr2line -pfiaC -e build/mm-audio.elf 0x4218f1ed
0x4218f1ed: operator new(unsigned int) at /builds/idf/crosstool-NG/.build/xtensa-esp-elf/src/gcc/libstdc++-v3/libsupc++/new_op.cc:50
(base) m4:mm-esp32-voice hushi.hs$ xtensa-esp32-elf-addr2line -pfiaC -e build/mm-audio.elf 0x42013711
0x42013711: std::__detail::_MakeUniq::__single_object std::make_unique<AudioStreamPacket, AudioStreamPacket>(AudioStreamPacket&&) at /Users/hushi.hs/.espressif/tools/xtensa-esp-elf/esp-14.2.0_20251107/xtensa-esp-elf/xtensa-esp-elf/include/c++/14.2.0/bits/unique_ptr.h:1076
(base) m4:mm-esp32-voice hushi.hs$ xtensa-esp32-elf-addr2line -pfiaC -e build/mm-audio.elf 0x4203141a
0x4203141a: std::_Function_handler<void (char const*, unsigned int, bool), MMWebsocketProtocol::OpenAudioChannel()::{lambda(char const*, unsigned int, bool)#1}>::_M_invoke(std::_Any_data const&, char const*&&, unsigned int&&, bool&&) at /Users/hushi.hs/Documents/WorkSpace/Firmware/mm-esp32-voice/main/protocols/mm_websocket_protocol.cc:187 (discriminator 1)
(inlined by) __invoke_impl<void, MMWebsocketProtocol::OpenAudioChannel()::<lambda(char const*, size_t, bool)>&, char const*, unsigned int, bool> at /Users/hushi.hs/.espressif/tools/xtensa-esp-elf/esp-14.2.0_20251107/xtensa-esp-elf/xtensa-esp-elf/include/c++/14.2.0/bits/invoke.h:61 (discriminator 1)
(inlined by) __invoke_r<void, MMWebsocketProtocol::OpenAudioChannel()::<lambda(char const*, size_t, bool)>&, char const*, unsigned int, bool> at /Users/hushi.hs/.espressif/tools/xtensa-esp-elf/esp-14.2.0_20251107/xtensa-esp-elf/xtensa-esp-elf/include/c++/14.2.0/bits/invoke.h:111 (discriminator 1)
(inlined by) _M_invoke at /Users/hushi.hs/.espressif/tools/xtensa-esp-elf/esp-14.2.0_20251107/xtensa-esp-elf/xtensa-esp-elf/include/c++/14.2.0/bits/std_function.h:290 (discriminator 1)
(base) m4:mm-esp32-voice hushi.hs$ xtensa-esp32-elf-addr2line -pfiaC -e build/mm-audio.elf 0x4208a9fd
0x4208a9fd: std::function<void (char const*, unsigned int, bool)>::operator()(char const*, unsigned int, bool) const at /Users/hushi.hs/.espressif/tools/xtensa-esp-elf/esp-14.2.0_20251107/xtensa-esp-elf/xtensa-esp-elf/include/c++/14.2.0/bits/std_function.h:591
(inlined by) WebSocket::OnTcpData(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&) at /Users/hushi.hs/Documents/WorkSpace/Github/esp-ml307/src/web_socket.cc:377
(base) m4:mm-esp32-voice hushi.hs$ xtensa-esp32-elf-addr2line -pfiaC -e build/mm-audio.elf 0x4208ab2d
0x4208ab2d: std::_Function_handler<void (std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&), WebSocket::Connect(char const*)::{lambda(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&)#1}>::_M_invoke(std::_Any_data const&, std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&) at /Users/hushi.hs/Documents/WorkSpace/Github/esp-ml307/src/web_socket.cc:159
(inlined by) __invoke_impl<void, WebSocket::Connect(char const*)::<lambda(const std::string&)>&, const std::__cxx11::basic_string<char, std::char_traits, std::allocator >&> at /Users/hushi.hs/.espressif/tools/xtensa-esp-elf/esp-14.2.0_20251107/xtensa-esp-elf/xtensa-esp-elf/include/c++/14.2.0/bits/invoke.h:61
(inlined by) __invoke_r<void, WebSocket::Connect(char const*)::<lambda(const std::string&)>&, const std::__cxx11::basic_string<char, std::char_traits, std::allocator >&> at /Users/hushi.hs/.espressif/tools/xtensa-esp-elf/esp-14.2.0_20251107/xtensa-esp-elf/xtensa-esp-elf/include/c++/14.2.0/bits/invoke.h:111
(inlined by) _M_invoke at /Users/hushi.hs/.espressif/tools/xtensa-esp-elf/esp-14.2.0_20251107/xtensa-esp-elf/xtensa-esp-elf/include/c++/14.2.0/bits/std_function.h:290
(base) m4:mm-esp32-voice hushi.hs$ xtensa-esp32-elf-addr2line -pfiaC -e build/mm-audio.elf 0x42088f5e
0x42088f5e: std::function<void (std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&)>::operator()(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&) const at /Users/hushi.hs/.espressif/tools/xtensa-esp-elf/esp-14.2.0_20251107/xtensa-esp-elf/xtensa-esp-elf/include/c++/14.2.0/bits/std_function.h:591
(inlined by) EspTcp::ReceiveTask() at /Users/hushi.hs/Documents/WorkSpace/Github/esp-ml307/src/esp/esp_tcp.cc:146
(base) m4:mm-esp32-voice hushi.hs$ xtensa-esp32-elf-addr2line -pfiaC -e build/mm-audio.elf 0x42088f69
0x42088f69: EspTcp::Connect(std::__cxx11::basic_string<char, std::char_traits, std::allocator > const&, int)::{lambda(void*)#1}::_FUN(void*) at /Users/hushi.hs/Documents/WorkSpace/Github/esp-ml307/src/esp/esp_tcp.cc:66
(inlined by) _FUN at /Users/hushi.hs/Documents/WorkSpace/Github/esp-ml307/src/esp/esp_tcp.cc:69
(base) m4:mm-esp32-voice hushi.hs$

@hushi55
Copy link
Contributor

hushi55 commented Jan 6, 2026

还是会 奔溃

@hushi55
Copy link
Contributor

hushi55 commented Jan 6, 2026

给另外一个线索,我上次的 PR 好像这么修复不会奔溃
我现在把你的这个 PR merge 到我 PR 上,再测试测试

@78
Copy link
Owner Author

78 commented Jan 7, 2026

你贴上来的只有堆栈,没有错误信息,我猜测应该是内存不足。可以打印一下每次接收到数据的内容以及实时的可用内存大小。

@hushi55
Copy link
Contributor

hushi55 commented Jan 12, 2026

[14:50:06.578] I (206267) SystemInfo: free sram: 31603 minimal sram: 30219
[14:50:16.610] I (216287) SystemInfo: free sram: 31367 minimal sram: 30219
[14:50:26.577] I (226267) SystemInfo: free sram: 31927 minimal sram: 30219
[14:50:36.579] I (236267) SystemInfo: free sram: 32619 minimal sram: 30219
[14:50:46.577] I (246267) SystemInfo: free sram: 32379 minimal sram: 30219
[14:50:56.577] I (256267) SystemInfo: free sram: 32307 minimal sram: 30219
[14:51:14.593] I (274267) SystemInfo: free sram: 31791 minimal sram: 30219
[14:51:24.577] I (284267) SystemInfo: free sram: 32423 minimal sram: 30219
[14:51:34.578] I (294267) SystemInfo: free sram: 32655 minimal sram: 30219
[14:51:44.577] I (304267) SystemInfo: free sram: 32387 minimal sram: 30219
[14:52:00.588] I (320277) SystemInfo: free sram: 31423 minimal sram: 30219
[14:52:10.577] I (330267) SystemInfo: free sram: 31719 minimal sram: 30219

@hushi55
Copy link
Contributor

hushi55 commented Jan 12, 2026

提供一份 系统 信息
这个是merge 了你的这个 PR 在我之前的分支上的信息
这样修复测试了接近一周,没有 crash 过

@78
Copy link
Owner Author

78 commented Jan 12, 2026

[14:50:06.578] I (206267) SystemInfo: free sram: 31603 minimal sram: 30219 [14:50:16.610] I (216287) SystemInfo: free sram: 31367 minimal sram: 30219 [14:50:26.577] I (226267) SystemInfo: free sram: 31927 minimal sram: 30219 [14:50:36.579] I (236267) SystemInfo: free sram: 32619 minimal sram: 30219 [14:50:46.577] I (246267) SystemInfo: free sram: 32379 minimal sram: 30219 [14:50:56.577] I (256267) SystemInfo: free sram: 32307 minimal sram: 30219 [14:51:14.593] I (274267) SystemInfo: free sram: 31791 minimal sram: 30219 [14:51:24.577] I (284267) SystemInfo: free sram: 32423 minimal sram: 30219 [14:51:34.578] I (294267) SystemInfo: free sram: 32655 minimal sram: 30219 [14:51:44.577] I (304267) SystemInfo: free sram: 32387 minimal sram: 30219 [14:52:00.588] I (320277) SystemInfo: free sram: 31423 minimal sram: 30219 [14:52:10.577] I (330267) SystemInfo: free sram: 31719 minimal sram: 30219

这里有什么有价值的信息吗?

@hushi55
Copy link
Contributor

hushi55 commented Jan 13, 2026

这里显示还有 30k 的 sram,你猜测的内存不足,应该不是这个问题引起的

@hushi55
Copy link
Contributor

hushi55 commented Jan 13, 2026

而且结合我说的,如果按照我的 copy 内存的做法,没有奔溃现象来看
我怀疑是 C++ 内存管理的问题,因为手动管理内存应该避免啥问题
具体是啥问题,我还没有研究出来

@78
Copy link
Owner Author

78 commented Jan 13, 2026

而且结合我说的,如果按照我的 copy 内存的做法,没有奔溃现象来看 我怀疑是 C++ 内存管理的问题,因为手动管理内存应该避免啥问题 具体是啥问题,我还没有研究出来

目前应该是不支持多个任务操作一个网络连接对象的。

@78 78 merged commit 2da4896 into main Jan 13, 2026
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.

3 participants