boost asio async_connect success after close

前端 未结 2 1915
天涯浪人
天涯浪人 2021-02-11 08:35

Single-threaded application.

It happens not every time, only after 1.5 hours of high load.

  1. tcp::socket::async_connect
  2. tcp::socket::close (by deadl
2条回答
  •  醉酒成梦
    2021-02-11 09:13

    I accept twsansbury's answer, just want to add some more info.

    About shutdown():

    void async_recv_handler( boost::system::error_code ec_recv, std::size_t count )
    {
        if ( !m_socket.is_open() )
            return; // first time don't trust to ec_recv
        if ( ec_recv )
        {
            // oops, we have error
            // log
            // close
            return;
        }
        // seems that we are just fine, no error in ec_recv, we can gracefully shutdown the connection
        // but shutdown may fail! this check is working for me
        boost::system::error_code ec_shutdown;
        // second time don't trusting to ec_recv
        m_socket.shutdown( t, ec_shutdown );
        if ( !ec_shutdown )
            return;
        // this error code is expected
        if ( ec_shutdown == boost::asio::error::not_connected )
           return;
        // other error codes are unexpected for me
        // log << ec_shutdown.message()
        throw boost::system::system_error(ec_shutdown);
    }
    

提交回复
热议问题