ithewei / libhv

🔥 比libevent/libuv/asio更易用的网络库。A c/c++ network library for developing TCP/UDP/SSL/HTTP/WebSocket/MQTT client/server.

Home Page:https://github.com/ithewei/libhv/wiki

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

hv crash when use as a library in linux

Newleep opened this issue · comments

stack

image

build from master
90e3b2f

image

os
image

You should use the debug library to print the stack.

I'm sure I have build debug library, I manually configured the debug mode in CMakeLists.txt
image
image
Debug library file size is bigger than release libray as in following picture
image
When I run the program, library crash again
image
in ConnectTimeout
image

I rebuild my project with new debug library and include files, new crash stack like this
image
It crash when post
image
At the beginning, it was successful, but it will crash after running for a period of time
image
I have use libhv library in many project, this is the first time I have encountered this problem

before post, i print the url
image
url looks like ok, and crash again
image

I try to use sendAsync, code like this
image
the last log and crash log is:

I0403 15:28:30.509676 3855407 DataUploadThread.cpp:1472] begin post:http://172.16.45.34:9051/upload/data data len:269 7125
[New LWP 3859915]
--Type for more, q to quit, c to continue without paging--

Thread 492 "15_upload_t" received signal SIGSEGV, Segmentation fault.
[Switching to LWP 3859915]
0x00007ffff26d91fd in hloop_now_hrtime (loop=0x0) at /egova_apps/3rd-src/libhv-master/event/hloop.c:534
534 /egova_apps/3rd-src/libhv-master/event/hloop.c: 没有那个文件或目录.
(gdb) bt
#0 0x00007ffff26d91fd in hloop_now_hrtime (loop=0x0) at /egova_apps/3rd-src/libhv-master/event/hloop.c:534
#1 0x00007ffff2754da9 in hv::AsyncHttpClient::doTask (this=0x7ffa6056f8e0,
task=std::shared_ptr (count 2, weak 0) 0x7ffa600020f0)
at /egova_apps/3rd-src/libhv-master/http/client/AsyncHttpClient.cpp:28
#2 0x00007ffff2756fe2 in hv::AsyncHttpClient::sendInLoop (this=0x7ffa6056f8e0,
task=std::shared_ptr (count 2, weak 0) 0x7ffa600020f0)
at /egova_apps/3rd-src/libhv-master/http/client/AsyncHttpClient.h:125
#3 0x00007ffff275ba21 in std::__invoke_impl<void, void (hv::AsyncHttpClient::&)(std::shared_ptrhv::HttpClientTask ), hv::AsyncHttpClient&, std::shared_ptrhv::HttpClientTask&> (__f=
@0x7ffa60570060: (void (hv::AsyncHttpClient::)(class hv::AsyncHttpClient * const, class std::shared_ptr<hv::Http ClientTask>)) 0x7ffff2756fbe <hv::AsyncHttpClient::sendInLoop(std::shared_ptrhv::HttpClientTask)>,
__t=@0x7ffa60570080: 0x7ffa6056f8e0) at /usr/local/include/c++/7.5.0/bits/invoke.h:73
#4 0x00007ffff275b1bb in std::__invoke<void (hv::AsyncHttpClient::
&)(std::shared_ptrhv::HttpClientTask), hv::Asyn cHttpClient*&, std::shared_ptrhv::HttpClientTask&> (__fn=
@0x7ffa60570060: (void (hv::AsyncHttpClient::)(class hv::AsyncHttpClient * const, class std::shared_ptr<hv::Http ClientTask>)) 0x7ffff2756fbe <hv::AsyncHttpClient::sendInLoop(std::shared_ptrhv::HttpClientTask)>)
at /usr/local/include/c++/7.5.0/bits/invoke.h:95
#5 0x00007ffff275a363 in std::_Bind<void (hv::AsyncHttpClient::
(hv::AsyncHttpClient*, std::shared_ptr<hv::HttpClien tTask>))(std::shared_ptrhv::HttpClientTask)>::__call<void, , 0ul, 1ul>(std::tuple<>&&, std::_Index_tuple<0ul, 1ul>) (this=0x7ffa60570060, __args=...) at /usr/local/include/c++/7.5.0/functional:467
#6 0x00007ffff2758cda in std::_Bind<void (hv::AsyncHttpClient::(hv::AsyncHttpClient, std::shared_ptr<hv::HttpClien tTask>))(std::shared_ptrhv::HttpClientTask)>::operator()<, void>() (this=0x7ffa60570060)
at /usr/local/include/c++/7.5.0/functional:551
#7 0x00007ffff275805f in std::_Function_handler<void (), std::_Bind<void (hv::AsyncHttpClient::(hv::AsyncHttpClient , std::shared_ptrhv::HttpClientTask))(std::shared_ptrhv::HttpClientTask)> >::_M_invoke(std::_Any_data const&) (
__functor=...) at /usr/local/include/c++/7.5.0/bits/std_function.h:316
#8 0x00007ffff7c20a9e in std::function<void ()>::operator()() const (this=0x7ffa60570030)
at /usr/local/include/c++/7.5.0/bits/std_function.h:706
#9 0x00007ffff7cfb872 in hv::EventLoop::queueInLoop(std::function<void ()>)::{lambda(hv::Event
)#1}::operator()(hv:: --Type for more, q to quit, c to continue without paging--bt
Event
) const (__closure=0x7ffa60570030, ev=0x7ffa6056feb0) at /egova_apps/ZTVA_Back/ztva/ztva/src/ZTVA/../../include/libhv/hv/EventLoop.h:172
#10 0x00007ffff7cff95e in std::_Function_handler<void (hv::Event*), hv::EventLoop::queueInLoop(std::function<void ()>)::{lambda(hv::Event*)#1}>::_M_invoke(std::_Any_data const&, hv::Event*&&) (__functor=..., __args#0=@0x7ffc5dfefe50: 0x7ffa6056feb0) at /usr/local/include/c++/7.5.0/bits/std_function.h:316
#11 0x00007ffff7cfe35f in std::function<void (hv::Event*)>::operator()(hv::Event*) const (this=0x7ffa6056fef0, __args#0=0x7ffa6056feb0)
at /usr/local/include/c++/7.5.0/bits/std_function.h:706
#12 0x00007ffff7cfbbaa in hv::EventLoop::onCustomEvent (hev=0x7ffc5dfefef0)
at /egova_apps/ZTVA_Back/ztva/ztva/src/ZTVA/../../include/libhv/hv/EventLoop.h:224
#13 0x00007ffff26d86ca in eventfd_read_cb (io=0x7ffaf4000cc0, buf=0x7ffaf4005c40, readbytes=8) at /egova_apps/3rd-src/libhv-master/event/hloop.c:225
#14 0x00007ffff26d55fa in hio_read_cb (io=0x7ffaf4000cc0, buf=0x7ffaf4005c40, len=8) at /egova_apps/3rd-src/libhv-master/event/hevent.c:404
#15 0x00007ffff26d54a1 in hio_handle_read (io=0x7ffaf4000cc0, buf=0x7ffaf4005c40, readbytes=8) at /egova_apps/3rd-src/libhv-master/event/hevent.c:371
#16 0x00007ffff26dae28 in __read_cb (io=0x7ffaf4000cc0, buf=0x7ffaf4005c40, readbytes=8) at /egova_apps/3rd-src/libhv-master/event/nio.c:48
#17 0x00007ffff26db99d in nio_read (io=0x7ffaf4000cc0) at /egova_apps/3rd-src/libhv-master/event/nio.c:336
#18 0x00007ffff26dbc53 in hio_handle_events (io=0x7ffaf4000cc0) at /egova_apps/3rd-src/libhv-master/event/nio.c:414
#19 0x00007ffff26d820b in hloop_process_pendings (loop=0x7ffa60002b40) at /egova_apps/3rd-src/libhv-master/event/hloop.c:122
#20 0x00007ffff26d84a0 in hloop_process_events (loop=0x7ffa60002b40, timeout_ms=100) at /egova_apps/3rd-src/libhv-master/event/hloop.c:185
#21 0x00007ffff26d8f39 in hloop_run (loop=0x7ffa60002b40) at /egova_apps/3rd-src/libhv-master/event/hloop.c:464
#22 0x00007ffff7cfb619 in hv::EventLoop::run (this=0x7ffa60005000) at /egova_apps/ZTVA_Back/ztva/ztva/src/ZTVA/../../include/libhv/hv/EventLoop.h:54
#23 0x00007ffff7cfc1fd in hv::EventLoopThread::loop_thread(std::function<int ()> const&, std::function<int ()> const&) (this=0x7ffa6056f8e0, pre=...,
post=...) at /egova_apps/ZTVA_Back/ztva/ztva/src/ZTVA/../../include/libhv/hv/EventLoopThread.h:95
#24 0x00007ffff7d0bae4 in std::__invoke_impl<void, void (hv::EventLoopThread::)(std::function<int ()> const&, std::function<int ()> const&), hv::EventLoopThread, std::function<int ()>, std::function<int ()> >(std::__invoke_memfun_deref, void (hv::EventLoopThread::&&)(std::function<int ()> const&, std::function<int ()> const&), hv::EventLoopThread&&, std::function<int ()>&&, std::function<int ()>&&) (__f=
@0x7ffa6151b860: (void (hv::EventLoopThread::)(class hv::EventLoopThread * const, const class std::function<int()> &, const class std::function<int()> &)) 0x7ffff7cfc0e0 <hv::EventLoopThread::loop_thread(std::function<int ()> const&, std::function<int ()> const&)>, __t=@0x7ffa6151b858: 0x7ffa6056f8e0)
at /usr/local/include/c++/7.5.0/bits/invoke.h:73
#25 0x00007ffff7d0a5b9 in std::__invoke<void (hv::EventLoopThread::
)(std::function<int ()> const&, std::function<int ()> const&), hv::EventLoopThread*, std::function<int ()>, std::function<int ()> >(void (hv::EventLoopThread::&&)(std::function<int ()> const&, std::function<int ()> const&), hv::EventLoopThread&&, std::function<int ()>&&, std::function<int ()>&&) (__fn=
@0x7ffa6151b860: (void (hv::EventLoopThread::)(class hv::EventLoopThread * const, const class std::function<int()> &, const class std::function<int()> &)) 0x7ffff7cfc0e0 <hv::EventLoopThread::loop_thread(std::function<int ()> const&, std::function<int ()> const&)>)
at /usr/local/include/c++/7.5.0/bits/invoke.h:95
#26 0x00007ffff7d0dacf in std::thread::_Invoker<std::tuple<void (hv::EventLoopThread::
)(std::function<int ()> const&, std::function<int ()> const&), hv::EventLoopThread*, std::function<int ()>, std::function<int ()> > >::_M_invoke<0ul, 1ul, 2ul, 3ul>(std::_Index_tuple<0ul, 1ul, 2ul, 3ul>) (this=0x7ffa6151b818)
at /usr/local/include/c++/7.5.0/thread:234
#27 0x00007ffff7d0d8db in std::thread::_Invoker<std::tuple<void (hv::EventLoopThread::)(std::function<int ()> const&, std::function<int ()> const&), hv::EventLoopThread, std::function<int ()>, std::function<int ()> > >::operator()() (this=0x7ffa6151b818) at /usr/local/include/c++/7.5.0/thread:243
#28 0x00007ffff7d0d1ac in std::thread::_State_impl<std::thread::_Invoker<std::tuple<void (hv::EventLoopThread::)(std::function<int ()> const&, std::function<int ()> const&), hv::EventLoopThread, std::function<int ()>, std::function<int ()> > > >::_M_run() (this=0x7ffa6151b810)
at /usr/local/include/c++/7.5.0/thread:186
#29 0x00007ffff7df49af in std::execute_native_thread_routine (__p=0x7ffa6151b810) at ../../../.././libstdc++-v3/src/c++11/thread.cc:83
#30 0x00007ffff7f8ef2b in ?? () from /usr/lib64/libpthread.so.0
#31 0x00007fffebabc34f in clone () from /usr/lib64/libc.so.6
(gdb) bt
#0 0x00007ffff26d91fd in hloop_now_hrtime (loop=0x0) at /egova_apps/3rd-src/libhv-master/event/hloop.c:534
#1 0x00007ffff2754da9 in hv::AsyncHttpClient::doTask (this=0x7ffa6056f8e0, task=std::shared_ptr (count 2, weak 0) 0x7ffa600020f0)
at /egova_apps/3rd-src/libhv-master/http/client/AsyncHttpClient.cpp:28
#2 0x00007ffff2756fe2 in hv::AsyncHttpClient::sendInLoop (this=0x7ffa6056f8e0, task=std::shared_ptr (count 2, weak 0) 0x7ffa600020f0)
at /egova_apps/3rd-src/libhv-master/http/client/AsyncHttpClient.h:125
#3 0x00007ffff275ba21 in std::__invoke_impl<void, void (hv::AsyncHttpClient::&)(std::shared_ptrhv::HttpClientTask), hv::AsyncHttpClient&, std::shared_ptrhv::HttpClientTask&> (__f=
@0x7ffa60570060: (void (hv::AsyncHttpClient::)(class hv::AsyncHttpClient * const, class std::shared_ptrhv::HttpClientTask)) 0x7ffff2756fbe <hv::AsyncHttpClient::sendInLoop(std::shared_ptrhv::HttpClientTask)>, __t=@0x7ffa60570080: 0x7ffa6056f8e0) at /usr/local/include/c++/7.5.0/bits/invoke.h:73
#4 0x00007ffff275b1bb in std::__invoke<void (hv::AsyncHttpClient::
&)(std::shared_ptrhv::HttpClientTask), hv::AsyncHttpClient*&, std::shared_ptrhv::HttpClientTask&> (__fn=
@0x7ffa60570060: (void (hv::AsyncHttpClient::)(class hv::AsyncHttpClient * const, class std::shared_ptrhv::HttpClientTask)) 0x7ffff2756fbe <hv::AsyncHttpClient::sendInLoop(std::shared_ptrhv::HttpClientTask)>) at /usr/local/include/c++/7.5.0/bits/invoke.h:95
#5 0x00007ffff275a363 in std::_Bind<void (hv::AsyncHttpClient::
(hv::AsyncHttpClient*, std::shared_ptrhv::HttpClientTask))(std::shared_ptrhv::HttpClientTask)>::__call<void, , 0ul, 1ul>(std::tuple<>&&, std::_Index_tuple<0ul, 1ul>) (this=0x7ffa60570060, __args=...)
at /usr/local/include/c++/7.5.0/functional:467
#6 0x00007ffff2758cda in std::_Bind<void (hv::AsyncHttpClient::(hv::AsyncHttpClient, std::shared_ptrhv::HttpClientTask))(std::shared_ptrhv::HttpClientTask)>::operator()<, void>() (this=0x7ffa60570060) at /usr/local/include/c++/7.5.0/functional:551
#7 0x00007ffff275805f in std::_Function_handler<void (), std::_Bind<void (hv::AsyncHttpClient::(hv::AsyncHttpClient, std::shared_ptrhv::HttpClientTask))(std::shared_ptrhv::HttpClientTask)> >::_M_invoke(std::_Any_data const&) (__functor=...) at /usr/local/include/c++/7.5.0/bits/std_function.h:316
#8 0x00007ffff7c20a9e in std::function<void ()>::operator()() const (this=0x7ffa60570030) at /usr/local/include/c++/7.5.0/bits/std_function.h:706
#9 0x00007ffff7cfb872 in hv::EventLoop::queueInLoop(std::function<void ()>)::{lambda(hv::Event*)#1}::operator()(hv::Event*) const (
__closure=0x7ffa60570030, ev=0x7ffa6056feb0) at /egova_apps/ZTVA_Back/ztva/ztva/src/ZTVA/../../include/libhv/hv/EventLoop.h:172
#10 0x00007ffff7cff95e in std::_Function_handler<void (hv::Event*), hv::EventLoop::queueInLoop(std::function<void ()>)::{lambda(hv::Event*)#1}>::_M_invoke(std::_Any_data const&, hv::Event*&&) (__functor=..., __args#0=@0x7ffc5dfefe50: 0x7ffa6056feb0) at /usr/local/include/c++/7.5.0/bits/std_function.h:316
#11 0x00007ffff7cfe35f in std::function<void (hv::Event*)>::operator()(hv::Event*) const (this=0x7ffa6056fef0, __args#0=0x7ffa6056feb0)
at /usr/local/include/c++/7.5.0/bits/std_function.h:706
#12 0x00007ffff7cfbbaa in hv::EventLoop::onCustomEvent (hev=0x7ffc5dfefef0)
at /egova_apps/ZTVA_Back/ztva/ztva/src/ZTVA/../../include/libhv/hv/EventLoop.h:224
#13 0x00007ffff26d86ca in eventfd_read_cb (io=0x7ffaf4000cc0, buf=0x7ffaf4005c40, readbytes=8) at /egova_apps/3rd-src/libhv-master/event/hloop.c:225
#14 0x00007ffff26d55fa in hio_read_cb (io=0x7ffaf4000cc0, buf=0x7ffaf4005c40, len=8) at /egova_apps/3rd-src/libhv-master/event/hevent.c:404
#15 0x00007ffff26d54a1 in hio_handle_read (io=0x7ffaf4000cc0, buf=0x7ffaf4005c40, readbytes=8) at /egova_apps/3rd-src/libhv-master/event/hevent.c:371
#16 0x00007ffff26dae28 in __read_cb (io=0x7ffaf4000cc0, buf=0x7ffaf4005c40, readbytes=8) at /egova_apps/3rd-src/libhv-master/event/nio.c:48
#17 0x00007ffff26db99d in nio_read (io=0x7ffaf4000cc0) at /egova_apps/3rd-src/libhv-master/event/nio.c:336
#18 0x00007ffff26dbc53 in hio_handle_events (io=0x7ffaf4000cc0) at /egova_apps/3rd-src/libhv-master/event/nio.c:414
#19 0x00007ffff26d820b in hloop_process_pendings (loop=0x7ffa60002b40) at /egova_apps/3rd-src/libhv-master/event/hloop.c:122
#20 0x00007ffff26d84a0 in hloop_process_events (loop=0x7ffa60002b40, timeout_ms=100) at /egova_apps/3rd-src/libhv-master/event/hloop.c:185
#21 0x00007ffff26d8f39 in hloop_run (loop=0x7ffa60002b40) at /egova_apps/3rd-src/libhv-master/event/hloop.c:464
#22 0x00007ffff7cfb619 in hv::EventLoop::run (this=0x7ffa60005000) at /egova_apps/ZTVA_Back/ztva/ztva/src/ZTVA/../../include/libhv/hv/EventLoop.h:54
#23 0x00007ffff7cfc1fd in hv::EventLoopThread::loop_thread(std::function<int ()> const&, std::function<int ()> const&) (this=0x7ffa6056f8e0, pre=...,
post=...) at /egova_apps/ZTVA_Back/ztva/ztva/src/ZTVA/../../include/libhv/hv/EventLoopThread.h:95
#24 0x00007ffff7d0bae4 in std::__invoke_impl<void, void (hv::EventLoopThread::)(std::function<int ()> const&, std::function<int ()> const&), hv::EventLoopThread, std::function<int ()>, std::function<int ()> >(std::__invoke_memfun_deref, void (hv::EventLoopThread::&&)(std::function<int ()> const&, std::function<int ()> const&), hv::EventLoopThread&&, std::function<int ()>&&, std::function<int ()>&&) (__f=
--Type for more, q to quit, c to continue without paging--bt
@0x7ffa6151b860: (void (hv::EventLoopThread::)(class hv::EventLoopThread * const, const class std::function<int()> &, const class std::function<int()> &)) 0x7ffff7cfc0e0 <hv::EventLoopThread::loop_thread(std::function<int ()> const&, std::function<int ()> const&)>, __t=@0x7ffa6151b858: 0x7ffa6056f8e0)
at /usr/local/include/c++/7.5.0/bits/invoke.h:73
#25 0x00007ffff7d0a5b9 in std::__invoke<void (hv::EventLoopThread::
)(std::function<int ()> const&, std::function<int ()> const&), hv::EventLoopThread*, std::function<int ()>, std::function<int ()> >(void (hv::EventLoopThread::&&)(std::function<int ()> const&, std::function<int ()> const&), hv::EventLoopThread&&, std::function<int ()>&&, std::function<int ()>&&) (__fn=
@0x7ffa6151b860: (void (hv::EventLoopThread::)(class hv::EventLoopThread * const, const class std::function<int()> &, const class std::function<int()> &)) 0x7ffff7cfc0e0 <hv::EventLoopThread::loop_thread(std::function<int ()> const&, std::function<int ()> const&)>)
at /usr/local/include/c++/7.5.0/bits/invoke.h:95
#26 0x00007ffff7d0dacf in std::thread::_Invoker<std::tuple<void (hv::EventLoopThread::
)(std::function<int ()> const&, std::function<int ()> const&), hv::EventLoopThread*, std::function<int ()>, std::function<int ()> > >::_M_invoke<0ul, 1ul, 2ul, 3ul>(std::_Index_tuple<0ul, 1ul, 2ul, 3ul>) (this=0x7ffa6151b818)
at /usr/local/include/c++/7.5.0/thread:234
#27 0x00007ffff7d0d8db in std::thread::_Invoker<std::tuple<void (hv::EventLoopThread::)(std::function<int ()> const&, std::function<int ()> const&), hv::EventLoopThread, std::function<int ()>, std::function<int ()> > >::operator()() (this=0x7ffa6151b818) at /usr/local/include/c++/7.5.0/thread:243
#28 0x00007ffff7d0d1ac in std::thread::_State_impl<std::thread::_Invoker<std::tuple<void (hv::EventLoopThread::)(std::function<int ()> const&, std::function<int ()> const&), hv::EventLoopThread, std::function<int ()>, std::function<int ()> > > >::_M_run() (this=0x7ffa6151b810)
at /usr/local/include/c++/7.5.0/thread:186
#29 0x00007ffff7df49af in std::execute_native_thread_routine (__p=0x7ffa6151b810) at ../../../.././libstdc++-v3/src/c++11/thread.cc:83
#30 0x00007ffff7f8ef2b in ?? () from /usr/lib64/libpthread.so.0
#31 0x00007fffebabc34f in clone () from /usr/lib64/libc.so.6

before post, i print the url image url looks like ok, and crash again image

host=0x6800000000
<error: Cannot access memory at address 0x6800000000>
Maybe frame->event_upload_url was freed by other thread? You can try use a local url save frame->event_upload_url first

const std::string url = frame->event_upload_url;
print url
requests::post(url, ...);

before post, i print the url image url looks like ok, and crash again image

host=0x6800000000 <error: Cannot access memory at address 0x6800000000> Maybe frame->event_upload_url was freed by other thread? You can try use a local url save frame->event_upload_url first

const std::string url = frame->event_upload_url;
print url
requests::post(url, ...);

Here already assign frame->event_upload_url to req->url, i think maybe error occured in req->url?
image

before post, i print the url image url looks like ok, and crash again image

host=0x6800000000 <error: Cannot access memory at address 0x6800000000> Maybe frame->event_upload_url was freed by other thread? You can try use a local url save frame->event_upload_url first

const std::string url = frame->event_upload_url;
print url
requests::post(url, ...);

Here already assign frame->event_upload_url to req->url, i think maybe error occured in req->url? image

Pay attention to the life cycle of HttpClient, sendAsync is non-blocking.

Perhaps it's related to the file fd. as I have to be busy with other projects, I use cpp http lib on this project