Boost Asio,异步UDP客户端 - 关闭时崩溃

时间:2015-05-01 06:22:46

标签: c++ boost boost-asio

我在我的应用程序中使用UDP客户端服务器进行IPC

它工作正常,但在尝试关闭客户端时会发生一些竞争条件,这会导致应用程序崩溃或死锁。

UDP客户端:

// IOServiceBase class contain boost::asio::io_service instance
// it is accessible by service() protected method
class AsyncUDPClient : public IOServiceBase
{
public:

    /// @brief Create a network client
    AsyncUDPClient(const std::string& host, const std::string port)
        : _host(host)
        , _port(port)
        , _reply()
        , _work(service())
        , _sock(service(), ba_ip::udp::endpoint(ba_ip::udp::v4(), 0)) {

        run();
    }

    /// @brief Start async packets processing
    void run(){
        std::thread t([&]{ service().run(); });
        t.detach();
    }

    /// @brief Async request to server
    void send(uint8_t* message, size_t length) {
        std::vector<uint8_t> packet(message, message + length);
        service().post(boost::bind(&AsyncUDPClient::do_send, this, packet));
    }

    /// @brief Cleanup io_service to dismiss already putted tasks
    ~AsyncUDPClient() {
        close();

        // trying to wait until service is stopped, but it does not help
        while (!service().stopped()){
            std::this_thread::sleep_for(std::chrono::milliseconds(100));
        }
    }

    /// @brief Cleanup io_service to dismiss already putted tasks
    void close(){
        std::thread t([&]{ service().stop(); });
        t.join();
    }
protected:

// send-response methods are pretty standard

private:

    std::string _host;
    std::string _port;
    std::array<uint8_t, max_length> _reply;
    ba::io_service::work _work;
    ba_ip::udp::socket _sock;
};

用法示例:

{
    AsyncUDPClient ipc(addr, port);
    ipc.send(&archive_data[0], archive_data.size());
    // it seems client is destroyed before some internal processing is finished?
}

行为不是确定性的,有时工作正常,有时会崩溃,有时会冻结。 Stacktrace在boost.asio内部的某处显示崩溃点

1 个答案:

答案 0 :(得分:2)

AsyncUDPClient的销毁无法与运行io_service的线程正确同步。当处理io_service的线程在其生命周期结束后尝试与AsyncUDPClient及其io_service进行交互时,这可能导致调用未定义的行为。

要解决此问题,请不要与处理io_service的线程分离,并在io_service停止后显式加入线程。

class AsyncUDPClient
 : public IOServiceBase
{
public:

  // ...

  void run()
  {
    _threads.emplace_back([&]{ service().run(); })
  }

  // ...

  ~AsyncUDPClient()
  {
    close();
  }

  void close()
  {
    // Stop the io_service.  This changes its state and return immediately.
    service().stop();

    // Explicitly synchronize with threads running the io_service.
    for (auto& thread: _threads)
    {
      thread.join();
    }
  }

private:

  // ...
  std::vector<std::thread> _threads;
};

正如上面的评论所暗示的,io_service::stop()并未阻止。它将io_service的状态更改为已停止,立即返回,并使run()run_one()的所有调用尽快返回。致io_service::stopped()的电话会立即返回io_service的状态。这两个调用都没有指出调用run()run_one()中是否存在线程。