c++ - 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error>>

标签 c++ boost boost-asio

我创建了客户端应用程序。当我发送单个消息 client <=> sever 时它工作正常。但是当我出于性能目的发送大量消息时,客户端会以两种不同的方式崩溃:

(gdb) run
Starting program: /home/x64joxer/workerGenerators/Worker2/worker -t -i 192.168.0.6 -p 6000 -d 5 -l //home/x64joxer/workerGenerators/Worker2/
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error> >'
  what():  set_option: Bad file descriptor
[New Thread 0x7fffe77fe700 (LWP 19310)]
[New Thread 0x7ffff4f24700 (LWP 18720)]
[New Thread 0x7fffe7fff700 (LWP 18148)]
[New Thread 0x7ffff5725700 (LWP 17582)]
[New Thread 0x7ffff5f26700 (LWP 16613)]
[New Thread 0x7ffff6727700 (LWP 16612)]

Program received signal SIGABRT, Aborted.
[Switching to Thread 0x7ffff5f26700 (LWP 16613)]
0x00007ffff6965267 in __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:55
55      ../sysdeps/unix/sysv/linux/raise.c: Nie ma takiego pliku ani katalogu.
(gdb) bt
#0  0x00007ffff6965267 in __GI_raise (sig=sig@entry=6) at ../sysdeps/unix/sysv/linux/raise.c:55
#1  0x00007ffff6966eca in __GI_abort () at abort.c:89
#2  0x00007ffff749706d in __gnu_cxx::__verbose_terminate_handler() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#3  0x00007ffff7494ee6 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#4  0x00007ffff7494f31 in std::terminate() () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#5  0x00007ffff74f0ec1 in ?? () from /usr/lib/x86_64-linux-gnu/libstdc++.so.6
#6  0x00007ffff6d016aa in start_thread (arg=0x7ffff5f26700) at pthread_create.c:333
#7  0x00007ffff6a36eed in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:109





(gdb) run
Starting program: /home/x64joxer/workerGenerators/Worker4/worker -t -i 192.168.0.6 -p 6000 -d 5 -l //home/x64joxer/workerGenerators/Worker4/
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7ffff4f24700 (LWP 17302)]
[New Thread 0x7ffff5f26700 (LWP 16583)]
[New Thread 0x7ffff6727700 (LWP 16582)]

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7ffff6727700 (LWP 16582)]
0x000000000044b902 in boost::asio::detail::epoll_reactor::deregister_descriptor (this=0x6a1a10, descriptor=6, descriptor_data=@0x7fffffffe2b8: 0x6a0d20, closing=true) at /usr/include/boost/asio/detail/impl/epoll_reactor.ipp:309
309     /usr/include/boost/asio/detail/impl/epoll_reactor.ipp: Nie ma takiego pliku ani katalogu.
(gdb) by
Undefined command: "by".  Try "help".
(gdb) bt
#0  0x000000000044b902 in boost::asio::detail::epoll_reactor::deregister_descriptor (this=0x6a1a10, descriptor=6, descriptor_data=@0x7fffffffe2b8: 0x6a0d20, closing=true) at /usr/include/boost/asio/detail/impl/epoll_reactor.ipp:309
#1  0x000000000044ecc0 in boost::asio::detail::reactive_socket_service_base::close (this=0x6a11f8, impl=..., ec=...) at /usr/include/boost/asio/detail/impl/reactive_socket_service_base.ipp:104
#2  0x000000000045476e in boost::asio::stream_socket_service<boost::asio::ip::tcp>::close (this=0x6a11d0, impl=..., ec=...) at /usr/include/boost/asio/stream_socket_service.hpp:170
#3  0x0000000000452d60 in boost::asio::basic_socket<boost::asio::ip::tcp, boost::asio::stream_socket_service<boost::asio::ip::tcp> >::close (this=0x7fffffffe2b0) at /usr/include/boost/asio/basic_socket.hpp:356
#4  0x0000000000449556 in TCPSocketBody::DoClose (this=0x7fffffffe260) at TCP/TCPSocketBody.cpp:158
#5  0x000000000045afbd in boost::_mfi::mf0<void, TCPSocketBody>::operator() (this=0x7ffff6726be0, p=0x7fffffffe260) at /usr/include/boost/bind/mem_fn_template.hpp:49
#6  0x000000000045a77a in boost::_bi::list1<boost::_bi::value<TCPSocketBody*> >::operator()<boost::_mfi::mf0<void, TCPSocketBody>, boost::_bi::list0> (this=0x7ffff6726bf0, f=..., a=...) at /usr/include/boost/bind/bind.hpp:253
#7  0x0000000000459986 in boost::_bi::bind_t<void, boost::_mfi::mf0<void, TCPSocketBody>, boost::_bi::list1<boost::_bi::value<TCPSocketBody*> > >::operator() (this=0x7ffff6726be0) at /usr/include/boost/bind/bind_template.hpp:20
#8  0x0000000000458bef in boost::asio::asio_handler_invoke<boost::_bi::bind_t<void, boost::_mfi::mf0<void, TCPSocketBody>, boost::_bi::list1<boost::_bi::value<TCPSocketBody*> > > > (function=...)
    at /usr/include/boost/asio/handler_invoke_hook.hpp:69
#9  0x0000000000457b77 in boost_asio_handler_invoke_helpers::invoke<boost::_bi::bind_t<void, boost::_mfi::mf0<void, TCPSocketBody>, boost::_bi::list1<boost::_bi::value<TCPSocketBody*> > >, boost::_bi::bind_t<void, boost::_mfi::mf0<void, TCPSocketBody>, boost::_bi::list1<boost::_bi::value<TCPSocketBody*> > > > (function=..., context=...) at /usr/include/boost/asio/detail/handler_invoke_helpers.hpp:37
#10 0x0000000000456747 in boost::asio::detail::completion_handler<boost::_bi::bind_t<void, boost::_mfi::mf0<void, TCPSocketBody>, boost::_bi::list1<boost::_bi::value<TCPSocketBody*> > > >::do_complete (owner=0x6a1960, 
    base=0x7fffe8001c70) at /usr/include/boost/asio/detail/completion_handler.hpp:68
#11 0x000000000044a744 in boost::asio::detail::task_io_service_operation::complete (this=0x7fffe8001c70, owner=..., ec=..., bytes_transferred=0) at /usr/include/boost/asio/detail/task_io_service_operation.hpp:38
#12 0x000000000044d102 in boost::asio::detail::task_io_service::do_run_one (this=0x6a1960, lock=..., this_thread=..., ec=...) at /usr/include/boost/asio/detail/impl/task_io_service.ipp:384
#13 0x000000000044cb23 in boost::asio::detail::task_io_service::run (this=0x6a1960, ec=...) at /usr/include/boost/asio/detail/impl/task_io_service.ipp:153
#14 0x000000000044d4d4 in boost::asio::io_service::run (this=0x7fffffffe2a0) at /usr/include/boost/asio/impl/io_service.ipp:59
#15 0x000000000045c575 in boost::_mfi::mf0<unsigned long, boost::asio::io_service>::operator() (this=0x6a1008, p=0x7fffffffe2a0) at /usr/include/boost/bind/mem_fn_template.hpp:49
#16 0x000000000045c4bb in boost::_bi::list1<boost::_bi::value<boost::asio::io_service*> >::operator()<unsigned long, boost::_mfi::mf0<unsigned long, boost::asio::io_service>, boost::_bi::list0> (this=0x6a1018, f=..., a=...)
    at /usr/include/boost/bind/bind.hpp:243
#17 0x000000000045c2aa in boost::_bi::bind_t<unsigned long, boost::_mfi::mf0<unsigned long, boost::asio::io_service>, boost::_bi::list1<boost::_bi::value<boost::asio::io_service*> > >::operator() (this=0x6a1008)
    at /usr/include/boost/bind/bind_template.hpp:20
#18 0x000000000045bf84 in boost::detail::thread_data<boost::_bi::bind_t<unsigned long, boost::_mfi::mf0<unsigned long, boost::asio::io_service>, boost::_bi::list1<boost::_bi::value<boost::asio::io_service*> > > >::run (this=0x6a0e50)
    at /usr/include/boost/thread/detail/thread.hpp:117
#19 0x00007ffff79c809a in ?? () from /usr/lib/x86_64-linux-gnu/libboost_thread.so.1.55.0
#20 0x00007ffff6d016aa in start_thread (arg=0x7ffff6727700) at pthread_create.c:333
#21 0x00007ffff6a36eed in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:109
(gdb) 

在 boost asio 套接字中似乎发生了崩溃,但我不确定发生了什么。我试图将所有 boost::asio 命令放入 try block 中,但看起来异常来自套接字线程。 这是 ma 套接字实现的一部分:

#include "TCPSocketBody.h"

TCPSocketBody::TCPSocketBody() : socket_(io_service_global),
                                 resolver(io_service_global),
                                 connected(false),
                                 expectedMessage(0)
{
    TRACE_FLAG_FOR_CLASS_TCPSocketBody Traces() << "\n" << "LOG: TCPSocketBody::TCPSocketBody(const std::string &adress, const std::string &port)";

    data_to_read = new char[MessageCoder::MaxMessageSize()];
}

void TCPSocketBody::Close()
{
  TRACE_FLAG_FOR_CLASS_TCPSocketBody Traces() << "\n" << "LOG: void TCPSocketBody::Close()";

  io_service_global.post(boost::bind(&TCPSocketBody::DoClose, this));
}

void TCPSocketBody::Connect(const std::string &adress, const std::string &port)
{
    TRACE_FLAG_FOR_CLASS_TCPSocketBody Traces() << "\n" << "LOG: void TCPSocketBody::Connect(const std::string &adress, const std::string &port)";

    io_service_global.reset();

    iterator = resolver.resolve({adress, port});

     boost::asio::async_connect(socket_, iterator,
           boost::bind(&TCPSocketBody::HandleConnect, this,
           boost::asio::placeholders::error));     

     socket_.set_option(boost::asio::ip::tcp::no_delay(true));
     socket_.set_option( boost::asio::socket_base::send_buffer_size( 65536 ) );
     socket_.set_option( boost::asio::socket_base::receive_buffer_size( 65536 ) );

     boost::thread(boost::bind(&boost::asio::io_service::run, &io_service_global));
}

void TCPSocketBody::HandleConnect(const boost::system::error_code& error)
{
  TRACE_FLAG_FOR_CLASS_TCPSocketBody Traces() << "\n" << "LOG: void TCPSocketBody::HandleConnect(const boost::system::error_code& error)";

  if (!error)
  {           
      connected = true;

      Message tempMessage;
      char *buffer = new char[MessageCoder::MaxMessageConnectedSize()];
      MessageCoder::ClearChar(buffer, MessageCoder::MaxMessageConnectedSize());
      MessageCoder::CreateConnectedMessage(buffer);
      tempMessage.CopyWsk(meWsk, buffer);
      messageQueue->PushBack(tempMessage);

      TRACE_FLAG_FOR_CLASS_TCPSocketBody Traces() << "\n" << "LOG: Sending Connected message to queue: " << buffer;

      boost::asio::async_read(socket_,
         boost::asio::buffer(data_to_read, MessageCoder::BufferSize()), boost::asio::transfer_all(),
         boost::bind(&TCPSocketBody::HandleReadHeader, this,
           boost::asio::placeholders::error));

  } else
  {
        Traces() << "\n" << "ERR: Connection error!";
        Close();
  }
}

void TCPSocketBody::HandleReadHeader(const boost::system::error_code& error)
{
    TRACE_FLAG_FOR_CLASS_TCPSocketBody Traces() << "\n" << "LOG: void TCPSocketBody::HandleReadHeader(const boost::system::error_code& error)";

    expectedMessage = MessageCoder::HeaderToVal(data_to_read);    

    TRACE_FLAG_FOR_CLASS_TCPSocketBody Traces() << "\n" << "LOG: Expecting message with lenn: " << expectedMessage;

    if (expectedMessage > MessageCoder::MaxMessageSize())
    {
       //TO_DO Traces() << "\n" << "ERR: Protocol error. Message too long:" << std::string(data_to_read);
       //TO_DO expectedMessage = 0;
       //TO_DO Close();
        expectedMessage = 0;

        boost::asio::async_read(socket_,
           boost::asio::buffer(data_to_read, MessageCoder::BufferSize()), boost::asio::transfer_all(),
           boost::bind(&TCPSocketBody::HandleReadHeader, this,
             boost::asio::placeholders::error));
    } else
    {
        boost::asio::async_read(socket_,
           boost::asio::buffer(data_to_read, expectedMessage), boost::asio::transfer_all(),
           boost::bind(&TCPSocketBody::HandleReadMessage, this,
             boost::asio::placeholders::error));
    }
}

void TCPSocketBody::HandleReadMessage(const boost::system::error_code& error)
{
    TRACE_FLAG_FOR_CLASS_TCPSocketBody Traces() << "\n" << "LOG: void TCPSocketBody::HandleReadMessage(const boost::system::error_code& error)";

    TRACE_FLAG_FOR_CLASS_TCPSocketBody Traces() << "\n" << "LOG: Message received: " << std::string(data_to_read);

    Message tempMessage;
    tempMessage.CopyData(meWsk, data_to_read);
    messageQueue->PushBack(tempMessage);
    expectedMessage = 0;        

    boost::asio::async_read(socket_,
       boost::asio::buffer(data_to_read, MessageCoder::BufferSize()), boost::asio::transfer_all(),
       boost::bind(&TCPSocketBody::HandleReadHeader, this,
         boost::asio::placeholders::error));
}

void TCPSocketBody::WriteMessage(char *dataToSend)
{
    io_service_global.post(boost::bind(&TCPSocketBody::Write, this, dataToSend));
}

void TCPSocketBody::Write(char *dataToSend)
{
    TRACE_FLAG_FOR_CLASS_TCPSocketBody Traces() << "\n" << "LOG: void TCPSocketBody::Write(char *dataToSend)";

    writeMutex.lock();
    data = dataToSend;

    boost::asio::async_write(socket_,
        boost::asio::buffer(data, std::strlen(data)),
        boost::bind(&TCPSocketBody::HandleWrite, this,
          boost::asio::placeholders::error));

}

void TCPSocketBody::HandleWrite(const boost::system::error_code& error)
{
  TRACE_FLAG_FOR_CLASS_TCPSocketBody Traces() << "\n" << "LOG: void TCPSocketBody::HandleWrite(const boost::system::error_code& error)";

  writeMutex.unlock();

  if (!error)
  {

      /*boost::asio::async_write(socket_,
          boost::asio::buffer(data,
            std::strlen(data)),
          boost::bind(&TCPSocketBody::HandleWrite, this,
            boost::asio::placeholders::error));*/
  }
  else
  {
        Traces() << "\n" << "ERR: Write error!";
        Close();
  }
}

void TCPSocketBody::DoClose()
{
  TRACE_FLAG_FOR_CLASS_TCPSocketBody Traces() << "\n" << "LOG: void TCPSocketBody::DoClose()";

  socket_.close();  
  connected = false;
  Message tempMessage;
  char *buffer = new char[MessageCoder::MaxMessageConnectionCloseSize()];
  MessageCoder::ClearChar(buffer, MessageCoder::MaxMessageConnectionCloseSize());
  MessageCoder::CreateCloseConnectionMessage(buffer);
  tempMessage.CopyWsk(meWsk, buffer);
  messageQueue->PushBack(tempMessage);  

}

TCPSocketBody::~TCPSocketBody()
{
    delete [] data_to_read;
}

如有任何帮助,我将不胜感激。

最佳答案

socket_.close();

运行时抛出异常!

您可以使用 boost::system::error_code 来正确捕捉错误:

boost::system::error_code lErrorCode;
socket_.close(lErrorCode);
std::cout <<  lErrorCode.message() << std::endl;

更多信息在这里: http://www.boost.org/doc/libs/1_66_0/doc/html/boost_asio/reference/basic_stream_socket/close/overload2.html

关于c++ - 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::system::system_error>>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40266238/

相关文章:

c++ - 断开连接后重新连接 boost beast (asio) websocket 和 http 连接时出错

c++ - 默认析构函数在互斥体上被阻止

c++ - 'if constexpr'理解错误

c++ - boost::stacktrace 打印地址

Python - 导入 C++ 模块接口(interface) - 无法打开共享对象文件

c++ - boost asio iostream - 如何获取本地IP地址

android - 从 C++ 调用 Android 调试方法

c++ - 如果 cpp 中的数据类型条目不正确,如何允许多个输入?

c++ - 使用 Boost.Preprocessor 生成一组带标签和索引的类模板特化

c++ - 在 MinGW 中使用 boost::asio::local