2
votes

I am getting EOF error from boost::asio::async_read. I have already went through the following posts (and some other similar posts) and nothing helped me.

  1. EOF in async_read() in boost::asio
  2. Random EOF in boost asio in multi thread
  3. EOF in boost::async_read with thread_pull and boost 1.54

About my application: I have developed a WebBrowser (using IE WebBrowser control) and proxy server with which browser communicates. Nothing could be check in the browser as everything happens internally in IE engine. And in the proxy server I am randomly getting EOF error with async_read and also with any other read version (async_read_some(), read()). And this problem I am only facing in release build and not in debug build (atleast not yet). In the debug build everything works fine.

The following is my read function. I don't know if there is any race condition or anything I am missing.

void SocksProxyConnection::HandleRead(const boost::system::error_code& errorCode, std::size_t bytesTransferred)
{
    //Check for the errors
    if (errorCode)
    {
        std::string connectionID = boost::lexical_cast<std::string>(m_connectionID);
        UNUSED(connectionID);

        if ((errorCode.value() == boost::asio::error::connection_reset))
        {
            TVLOG_INFO(LOG("SocksProxyConnection: Connection Reset ConnectionID: %1%") % (boost::lexical_cast<std::string>(m_connectionID)).c_str());
            m_removeConnectionFromMapFunction(m_connectionID);
            SendConnectionCloseCommand();
        } 
        else if (errorCode.value() == boost::asio::error::eof)
        {
            LOG_ERRORCODE(L"SocksProxyConnection: End Of File error", errorCode);
        }
        else if (errorCode.value() != boost::asio::error::operation_aborted)
        {
            //Case: Error occurred while reading
            LOG_ERRORCODE(L"SocksProxyConnection: Error in reading the data from the webBrowser", errorCode);
        }

        return;
    }

    //No errors

    //Send the data to the remote server
    ForwardBuffer(bytesTransferred);

    //Perform the async read operation.
    boost::asio::async_read(m_socket, boost::asio::buffer(m_readDataBuffer.get(), DataBufferSize), boost::asio::transfer_at_least(1),
                            m_Strand.wrap(boost::bind(&SocksProxyConnection::HandleRead,
                                        shared_from_this(),
                                        boost::asio::placeholders::error,
                                        boost::asio::placeholders::bytes_transferred)));

}

There is a dedicated io_service for this proxy server which includes two asynchronous calls async_accept() and async_read(). Sending data to the browser is not async. I am using blocking write() function. I don't know if that makes any difference.

There was also this bug in boost 1.54 which was causing the similar problem. But that was 3 years ago and has been fixed. Currently, I am using Booset 1.62. I haven't found any similar bug report for this version.

I have been trying to solve this problem for over a week. Would appreciate if anyone could help me out now.

1

1 Answers

1
votes

Can you show us a self contained example?

I think you're basically running into short reads, which are perfectly normal for non-framed protocols. It even explains why EOF is an "error":

Why EOF is an Error

The end of a stream can cause read, async_read, read_until or async_read_until functions to violate their contract. E.g. a read of N bytes may finish early due to EOF. An EOF error may be used to distinguish the end of a stream from a successful read of size 0.