drogonframework / drogon

Drogon: A C++14/17/20 based HTTP web application framework running on Linux/macOS/Unix/Windows
MIT License
11.62k stars 1.12k forks source link

OnError崩溃? #2099

Closed bethebest0622 closed 4 months ago

bethebest0622 commented 4 months ago

您好,在使用drogon的过程中,程序崩溃在onError这里,gdb core堆栈信息如下:

#0  0x00007f134e9dbe19 in std::_Function_handler<void (drogon::ReqResult, std::shared_ptr<drogon::HttpResponse> const&), util::curl_util::HttpClient::keep_hb(util::curl_util::Handler&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, drogon::HttpMethod, std::vector<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > >, std::allocator<std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > > > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)::{lambda(drogon::ReqResult const&, std::shared_ptr<drogon::HttpResponse> const&)#1}>::_M_invoke(std::_Any_data const&, drogon::ReqResult&&, std::shared_ptr<drogon::HttpResponse> const&) () from /usr/local/lib/libcurl_util.so
#1  0x00007f134ea39e90 in std::_Function_handler<void (drogon::ReqResult, std::shared_ptr<drogon::HttpResponse> const&), drogon::HttpClientImpl::sendRequestInLoop(std::shared_ptr<drogon::HttpRequest> const&, std::function<void (drogon::ReqResult, std::shared_ptr<drogon::HttpResponse> const&)>&&)::{lambda(drogon::ReqResult, std::shared_ptr<drogon::HttpResponse> const&)#3}>::_M_invoke(std::_Any_data const&, drogon::ReqResult&&, std::shared_ptr<drogon::HttpResponse> const&) () from /usr/local/lib/libcurl_util.so
#2  0x00007f134ea3ac9d in drogon::HttpClientImpl::onError(drogon::ReqResult) () from /usr/local/lib/libcurl_util.so
#3  0x00007f134ea3fdb5 in drogon::HttpClientImpl::createTcpClient()::{lambda(std::shared_ptr<trantor::TcpConnection> const&)#1}::operator()(std::shared_ptr<trantor::TcpConnection> const&) const ()
   from /usr/local/lib/libcurl_util.so
#4  0x00007f134eb17608 in trantor::TcpConnectionImpl::handleClose() () from /usr/local/lib/libcurl_util.so
#5  0x00007f134eb195e8 in trantor::TcpConnectionImpl::readCallback() () from /usr/local/lib/libcurl_util.so
#6  0x00007f134eb0d1e8 in trantor::Channel::handleEventSafely() () from /usr/local/lib/libcurl_util.so
#7  0x00007f134eb0d281 in trantor::Channel::handleEvent() () from /usr/local/lib/libcurl_util.so
#8  0x00007f134eafcf0c in trantor::EventLoop::loop() () from /usr/local/lib/libcurl_util.so
#9  0x00007f134eafde70 in trantor::EventLoopThread::loopFuncs() () from /usr/local/lib/libcurl_util.so
#10 0x00007f134e035b23 in execute_native_thread_routine () from /lib64/libstdc++.so.6
#11 0x00007f134e4ce1ca in start_thread () from /lib64/libpthread.so.0
#12 0x00007f134d64de73 in clone () from /lib64/libc.so.6

我的keep_hb函数如下:

void util::curl_util::HttpClient::keep_hb(Handler & h, const std::string & path, drogon::HttpMethod method, const std::vector<std::pair<std::string, std::string>> & header, const std::string & body) {
  auto req_ptr = drogon::HttpRequest::newHttpRequest();
  req_ptr->setPath(path);
  req_ptr->setMethod(method);
  if (!body.empty()) req_ptr->setBody(body);
  for (const auto & [key, value] : header) req_ptr->addHeader(key, value);

  req_ptr->setContentTypeString("application/json");

  h.ptr_->sendRequest(req_ptr, [] (const drogon::ReqResult & rq, const drogon::HttpResponsePtr & hr) { printf("hb body = %s\n", hr->getBody().data()); }); 
  timeval t;
  gettimeofday(&t, NULL);
  h.last_used_ts_ = t.tv_sec;
  etp_.start();
}

想请问您,什么时候会产生OnError呢?是不是需要对Error进行一些处理才能避免崩溃

an-tao commented 4 months ago

回调里的HttpResponsePtr 不一定有效,如果ReqResult不是Ok,它就会为空,你使用前要先判断一下。