ZLToolKit icon indicating copy to clipboard operation
ZLToolKit copied to clipboard

TcpServer.cpp weak_session.lock() SIGSEGV coredump

Open kubbo opened this issue 6 months ago • 6 comments

the coredump stack is

Program terminated with signal SIGSEGV, Segmentation fault.
#0  __GI_abort () at abort.c:107
107     abort.c: No such file or directory.
[Current thread is 1 (Thread 0x7f87a6ffd000 (LWP 112))]
(gdb) bt
#0  __GI_abort () at abort.c:107
#1  0x00007f87dca9e911 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#2  0x00007f87dcaaa38c in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3  0x00007f87dcaa9369 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#4  0x00007f87dcaa9d21 in __gxx_personality_v0 () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5  0x00007f87dc8a6bef in ?? () from /usr/lib/x86_64-linux-gnu/libgcc_s.so.1
#6  0x00007f87dc8a75aa in _Unwind_Resume () from /usr/lib/x86_64-linux-gnu/libgcc_s.so.1
#7  0x000055de7f9c37b5 in std::_Function_base::~_Function_base (this=<optimized out>, __in_chrg=<optimized out>) at /usr/include/c++/8/bits/std_function.h:257
#8  std::function<void ()>::~function() (this=<optimized out>, __in_chrg=<optimized out>) at /usr/include/c++/8/bits/std_function.h:370
#9  toolkit::onceToken::~onceToken (this=<optimized out>, __in_chrg=<optimized out>) at /home/jingansi/apps/code/3rdpart/ZLToolKit/src/Util/onceToken.h:34
#10 toolkit::SSL_Box::flush (this=0x7f879c023288) at /home/jingansi/apps/code/3rdpart/ZLToolKit/src/Util/SSLBox.cpp:447
#11 0x000055de7fa5b2a0 in toolkit::TcpSessionWithSSL<mediakit::HttpSession>::~TcpSessionWithSSL (this=0x7f879c022b40, __in_chrg=<optimized out>) at /home/jingansi/apps/code/3rdpart/ZLToolKit/src/Network/TcpSession.h:35
#12 __gnu_cxx::new_allocator<toolkit::TcpSessionWithSSL<mediakit::HttpSession> >::destroy<toolkit::TcpSessionWithSSL<mediakit::HttpSession> > (this=0x7f879c022b40, __p=0x7f879c022b40) at /usr/include/c++/8/ext/new_allocator.h:140
#13 std::allocator_traits<std::allocator<toolkit::TcpSessionWithSSL<mediakit::HttpSession> > >::destroy<toolkit::TcpSessionWithSSL<mediakit::HttpSession> > (__a=..., __p=0x7f879c022b40) at /usr/include/c++/8/bits/alloc_traits.h:487
#14 std::_Sp_counted_ptr_inplace<toolkit::TcpSessionWithSSL<mediakit::HttpSession>, std::allocator<toolkit::TcpSessionWithSSL<mediakit::HttpSession> >, (__gnu_cxx::_Lock_policy)2>::_M_dispose (this=0x7f879c022b30)
    at /usr/include/c++/8/bits/shared_ptr_base.h:554
#15 0x000055de7fcf868f in std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release (this=0x7f879c022b30) at /usr/include/c++/8/bits/shared_ptr_base.h:148
#16 std::_Sp_counted_base<(__gnu_cxx::_Lock_policy)2>::_M_release (this=0x7f879c022b30) at /usr/include/c++/8/bits/shared_ptr_base.h:148
#17 std::__shared_count<(__gnu_cxx::_Lock_policy)2>::~__shared_count (this=<synthetic pointer>, __in_chrg=<optimized out>) at /usr/include/c++/8/bits/shared_ptr_base.h:728
#18 std::__shared_ptr<toolkit::Session, (__gnu_cxx::_Lock_policy)2>::~__shared_ptr (this=<synthetic pointer>, __in_chrg=<optimized out>) at /usr/include/c++/8/bits/shared_ptr_base.h:1167
#19 std::shared_ptr<toolkit::Session>::~shared_ptr (this=<synthetic pointer>, __in_chrg=<optimized out>) at /usr/include/c++/8/bits/shared_ptr.h:103
#20 toolkit::TcpServer::<lambda(const Ptr&, sockaddr*, int)>::operator() (buf=..., __closure=<optimized out>) at /home/jingansi/apps/code/3rdpart/ZLToolKit/src/Network/TcpServer.cpp:113
#21 std::_Function_handler<void(const std::shared_ptr<toolkit::Buffer>&, sockaddr*, int), toolkit::TcpServer::onAcceptConnection(const Ptr&)::<lambda(const Ptr&, sockaddr*, int)> >::_M_invoke(const std::_Any_data &, const std::shared_ptr<toolkit::Buffer> &, sockaddr *&&, int &&) (__functor=..., __args#0=..., __args#1=<optimized out>, __args#2=<optimized out>) at /usr/include/c++/8/bits/std_function.h:297
#22 0x000055de7fcee26b in std::function<void (std::shared_ptr<toolkit::Buffer> const&, sockaddr*, int)>::operator()(std::shared_ptr<toolkit::Buffer> const&, sockaddr*, int) const (__args#2=<optimized out>, __args#1=<optimized out>, 
    __args#0=std::shared_ptr<class toolkit::Buffer> (use count 9, weak count 1) = {...}, this=0x7f8664d5cba8) at /usr/include/c++/8/bits/std_function.h:682
#23 toolkit::Socket::onRead (this=0x7f8664d5cab0, sock=..., is_udp=<optimized out>) at /home/jingansi/apps/code/3rdpart/ZLToolKit/src/Network/Socket.cpp:299
#24 0x000055de7fcefe21 in toolkit::Socket::<lambda(int)>::operator() (event=3, __closure=<optimized out>) at /home/jingansi/apps/code/3rdpart/ZLToolKit/src/Network/Socket.cpp:246
#25 std::_Function_handler<void(int), toolkit::Socket::attachEvent(const Ptr&, bool)::<lambda(int)> >::_M_invoke(const std::_Any_data &, int &&) (__functor=..., __args#0=<optimized out>) at /usr/include/c++/8/bits/std_function.h:297
#26 0x000055de7fd0d5af in std::function<void (int)>::operator()(int) const (__args#0=<optimized out>, this=<optimized out>) at /usr/include/c++/8/bits/std_function.h:682
#27 toolkit::EventPoller::runLoop (this=0x55de81cde0f0, blocked=<optimized out>, regist_self=<optimized out>) at /home/jingansi/apps/code/3rdpart/ZLToolKit/src/Poller/EventPoller.cpp:310

the line 133 is below Image

I suspect that when the code weak_session.lock() is executed, the session has already been destructed. So why use weak_ptr instead of shared_ptr here?

kubbo avatar May 22 '25 15:05 kubbo

Hi, please update the code to the latest version. Your code version is too early

xia-chu avatar May 23 '25 03:05 xia-chu

I upgraded to the latest code, but there are still occasional crashes with a similar stack trace as mentioned above. I suspect that the crash is caused by the following section:

Image During the _on_multi_read callback, it does not check whether the current socket is in an abnormal state. If an exception occurs, such as triggering emitErr, the session will eventually be destructed. When the _on_multi_read callback is invoked again at this point, the exception mentioned above will occur.

kubbo avatar May 28 '25 14:05 kubbo

It seems that an exception was thrown during destruction. Please debug it with gdb, then use catch throw to catch the exception.

xia-chu avatar May 29 '25 08:05 xia-chu

The exception was caused by WebSocket parsing, but could I trouble you to ask one more question: Why would a WebSocket parsing exception lead to the crash mentioned earlier Image

kubbo avatar May 29 '25 14:05 kubbo

A minimal reproducible example will be very useful.

wasphin avatar May 30 '25 00:05 wasphin

If an exception is thrown in a destructor, it will by default lead to an abort crash. Exceptions should not be thrown from destructors.

xia-chu avatar May 30 '25 04:05 xia-chu