【问题标题】:boost::asio + std::future - Access violation after closing socketboost::asio + std::future - 关闭套接字后访问冲突
【发布时间】:2015-10-30 18:14:53
【问题描述】:

我正在编写一个简单的 tcp 客户端来发送和接收单行文本。异步操作由 std::future 处理,以促进具有超时的阻塞查询。不幸的是,我的测试应用程序在破坏服务器对象时因访问冲突而崩溃。 这是我的代码:

TCPClient.hpp

#ifndef __TCPCLIENT_H__
#define __TCPCLIENT_H__

#include <boost/asio.hpp>
#include <boost/asio/use_future.hpp>
#include <memory>
#include <vector>
#include <future>
#include <thread>
#include <chrono>
#include <iostream>
#include <iterator>

using namespace boost::asio;

class TCPClient {
public:
    TCPClient();
    ~TCPClient();

    void connect(const std::string& address, const std::string& port);
    void disconnect();

    std::string sendMessage(const std::string& msg);
private:
    boost::asio::io_service ioservice;
    boost::asio::io_service::work work;
    std::thread t;

    std::unique_ptr<boost::asio::ip::tcp::socket> socket;
};

inline TCPClient::TCPClient() : ioservice(), work(ioservice) {
    t = std::thread([&]() {
        try {
            ioservice.run();
        }
        catch (const boost::system::system_error& e) {
            std::cerr << e.what() << std::endl;
        }
    });
}

inline TCPClient::~TCPClient() {
    disconnect();
    ioservice.stop();
    if (t.joinable()) t.join();
}

inline void TCPClient::connect(const std::string& address, const std::string& port) {
    socket.reset(new ip::tcp::socket(ioservice));

    ip::tcp::resolver::query query(address, port);
    std::future<ip::tcp::resolver::iterator> conn_result = async_connect(*socket, ip::tcp::resolver(ioservice).resolve(query), use_future);

    if (conn_result.wait_for(std::chrono::seconds(6)) != std::future_status::timeout) {
        conn_result.get(); // throws boost::system::system_error if the operation fails
    }
    else {
        //socket->close();
        // throw timeout_error("Timeout");
        throw std::exception("timeout");
    }
}

inline void TCPClient::disconnect() {
    if (socket) {
        try {
            socket->shutdown(ip::tcp::socket::shutdown_both);
            std::cout << "socket points to " << std::addressof(*socket) << std::endl;
            socket->close();
        }
        catch (const boost::system::system_error& e) {
            // ignore
            std::cerr << "ignored error " << e.what() << std::endl;
        }
    }
}

inline std::string TCPClient::sendMessage(const std::string& msg) {
    auto time_over = std::chrono::system_clock::now() + std::chrono::seconds(4);

    /*
    // Doesn't affect the error
    std::future<size_t> write_fut = boost::asio::async_write(*socket, boost::asio::buffer(msg), boost::asio::use_future);

    try {
        write_fut.get();
    }
    catch (const boost::system::system_error& e) {
        std::cerr << e.what() << std::endl;
    }
    */
    boost::asio::streambuf response;

    std::future<std::size_t> read_fut = boost::asio::async_read_until(*socket, response, '\n', boost::asio::use_future);
    if (read_fut.wait_until(time_over) != std::future_status::timeout) {
        std::cout << "read " << read_fut.get() << " bytes" << std::endl;
        return std::string(std::istreambuf_iterator<char>(&response), std::istreambuf_iterator<char>());
    }
    else {
        std::cout << "socket points to " << std::addressof(*socket) << std::endl;
        throw std::exception("timeout");
    }
}
#endif

main.cpp

#include <iostream>

#include "TCPClient.hpp"

int main(int argc, char* argv[]) {
    TCPClient client;
    try {
        client.connect("localhost", "27015");
        std::cout << "Response: " << client.sendMessage("Hello!") << std::endl;
    }
    catch (const boost::system::system_error& e) {
        std::cerr << e.what() << std::endl;
    }
    catch (const std::exception& e) {
        std::cerr << e.what() << std::endl;
    }
    system("pause");
    return 0;
}

输出如预期的那样是“超时”(测试服务器故意不发送数据),但ioservice.run() 在关闭TCPClient::disconnect() 中的套接字后立即崩溃(访问冲突)。我在这里做一些内存管理不善吗?

编译器是 MSVC 12.0.31101.00 Update 4 (Visual Studio 2013)

【问题讨论】:

    标签: c++ c++11 asynchronous boost-asio


    【解决方案1】:

    recvmsg 正在接收缓冲区 (streambuf),该缓冲区在 TCPClient::sendMessage 中引发异常后被释放(第 105 行,范围结束)。

    您忘记取消在第 97 行开始的异步操作 (async_read_until)。修复它:

    else {
        socket->cancel(); // ADDED
        std::cout << "socket points to " << std::addressof(*socket) << std::endl;
        throw std::runtime_error("timeout");
    }
    

    甚至,只是

        socket.reset(); // ADDED
    

    其他超时路径也是如此。

    【讨论】:

      【解决方案2】:

      other answer 解决了问题所在。

      不过,在更高的层次上,您正在使用期货,只是立即等待它们的回归。

      令我震惊的是,这实际上根本不是异步,你应该能够做到:

      • 无需穿线和连接
      • 没有.stop()
      • 没有workwork.reset()
      • 没有显式构造函数或析构函数
      • 没有 unique_ptr&lt;socket&gt; 以及随之而来的生命周期管理
      • 没有future&lt;&gt;,以及.get()future_status 的检查

      总而言之,您可以做很多简单的事情,例如使用这样的简单辅助函数:

      class TCPClient {
      public:
          void        disconnect();
          void        connect(const std::string& address, const std::string& port);
          std::string sendMessage(const std::string& msg);
      
      private:
          using error_code = boost::system::error_code;
      
          template<typename AllowTime> void await_operation(AllowTime const& deadline_or_duration) {
              using namespace boost::asio;
      
              ioservice.reset();
              {
                  high_resolution_timer tm(ioservice, deadline_or_duration);
                  tm.async_wait([this](error_code ec) { if (ec != error::operation_aborted) socket.cancel(); });
                  ioservice.run_one();
              }
              ioservice.run();
          }
      
          boost::asio::io_service      ioservice { };
          boost::asio::ip::tcp::socket socket { ioservice };
      };
      

      例如connect(...)曾经是:

      socket.reset(new ip::tcp::socket(ioservice));
      
      ip::tcp::resolver::query query(address, port);
      std::future<ip::tcp::resolver::iterator> conn_result = async_connect(*socket, ip::tcp::resolver(ioservice).resolve(query), use_future);
      
      if (conn_result.wait_for(std::chrono::seconds(6)) != std::future_status::timeout) {
          conn_result.get(); // throws boost::system::system_error if the operation fails
      }
      else {
          socket->cancel();
          // throw timeout_error("Timeout");
          throw std::runtime_error("timeout");
      }
      

      现在变成:

      async_connect(socket, 
              ip::tcp::resolver(ioservice).resolve({address, port}),
              [&](error_code ec, ip::tcp::resolver::iterator it) { if (ec) throw std::runtime_error(ec.message()); });
      
      await_operation(std::chrono::seconds(6));
      

      同样,sendMessage 变为:

      streambuf response;
      async_read_until(socket, response, '\n', [&](error_code ec, size_t bytes_read) {
              if (ec) throw std::runtime_error(ec.message());
              std::cout << "read " << bytes_read << " bytes" << std::endl;
          });
      
      await_operation(std::chrono::system_clock::now() + std::chrono::seconds(4));
      
      return {std::istreambuf_iterator<char>(&response), {}};
      

      请注意,这些明显更简单。另请注意,现在会抛出正确的异常消息,具体取决于失败的原因。

      完整演示

      Live On Coliru

      #ifndef __TCPCLIENT_H__
      #define __TCPCLIENT_H__
      
      #include <boost/asio.hpp>
      #include <boost/asio/high_resolution_timer.hpp>
      #include <iostream>
      
      class TCPClient {
      public:
          void        disconnect();
          void        connect(const std::string& address, const std::string& port);
          std::string sendMessage(const std::string& msg);
      
      private:
          using error_code = boost::system::error_code;
      
          template<typename AllowTime> void await_operation(AllowTime const& deadline_or_duration) {
              using namespace boost::asio;
      
              ioservice.reset();
              {
                  high_resolution_timer tm(ioservice, deadline_or_duration);
                  tm.async_wait([this](error_code ec) { if (ec != error::operation_aborted) socket.cancel(); });
                  ioservice.run_one();
              }
              ioservice.run();
          }
      
          boost::asio::io_service      ioservice { };
          boost::asio::ip::tcp::socket socket { ioservice };
      };
      
      
      inline void TCPClient::connect(const std::string& address, const std::string& port) {
          using namespace boost::asio;
      
          async_connect(socket, 
                  ip::tcp::resolver(ioservice).resolve({address, port}),
                  [&](error_code ec, ip::tcp::resolver::iterator it) { if (ec) throw std::runtime_error(ec.message()); });
      
          await_operation(std::chrono::seconds(6));
      }
      
      inline void TCPClient::disconnect() {
          using namespace boost::asio;
      
          if (socket.is_open()) {
              try {
                  socket.shutdown(ip::tcp::socket::shutdown_both);
                  socket.close();
              }
              catch (const boost::system::system_error& e) {
                  // ignore
                  std::cerr << "ignored error " << e.what() << std::endl;
              }
          }
      }
      
      inline std::string TCPClient::sendMessage(const std::string& msg) {
          using namespace boost::asio;
      
          streambuf response;
          async_read_until(socket, response, '\n', [&](error_code ec, size_t bytes_read) {
                  if (ec) throw std::runtime_error(ec.message());
                  std::cout << "read " << bytes_read << " bytes" << std::endl;
              });
      
          await_operation(std::chrono::system_clock::now() + std::chrono::seconds(4));
      
          return {std::istreambuf_iterator<char>(&response), {}};
      }
      #endif
      
      #include <iostream>
      
      //#include "TCPClient.hpp"
      
      int main(/*int argc, char* argv[]*/) {
          TCPClient client;
          try {
              client.connect("127.0.0.1", "27015");
              std::cout << "Response: " << client.sendMessage("Hello!") << std::endl;
          }
          catch (const boost::system::system_error& e) {
              std::cerr << e.what() << std::endl;
          }
          catch (const std::exception& e) {
              std::cerr << e.what() << std::endl;
          }
      }
      

      奖金

      如果您想要更方便,请使用一个仅引发异常的通用回调处理程序:

      struct raise {
          template <typename... A> void operator()(error_code ec, A...) const {
              if (ec) throw std::runtime_error(ec.message()); 
          }
      };
      

      现在,在没有 lambda 的情况下,主体变得更加简单:

      inline void TCPClient::connect(const std::string& address, const std::string& port) {
          async_connect(socket, ip::tcp::resolver(ioservice).resolve({address, port}), raise());
          await_operation(std::chrono::seconds(6));
      }
      
      inline std::string TCPClient::sendMessage(const std::string& msg) {
          streambuf response;
          async_read_until(socket, response, '\n', raise());
          await_operation(std::chrono::system_clock::now() + std::chrono::seconds(4));
      
          return {std::istreambuf_iterator<char>(&response), {}};
      }
      

      查看改编的演示:Live On Coliru

      【讨论】:

      • 简化 Asio 使用,查找所有内容 in this Gist(带有修订历史记录)。
      • 非常感谢您的精彩解释!我很快就会试试看。接受您的其他答案,因为它直接解决了我犯的错误。
      • 我看到了在堆栈上分配套接字的一个问题,请参阅这个答案:stackoverflow.com/a/3073354/4313550 套接字关闭后无法重用。
      • @TheBender 在这种情况下,关闭后不要重复使用它:) 我不确定该声明是否非常准确,但对于给出的样本并不重要(它没有'关闭后不重复使用)
      猜你喜欢
      • 1970-01-01
      • 2016-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-07
      • 2023-03-25
      • 2016-05-25
      • 1970-01-01
      相关资源
      最近更新 更多