2

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.

Community
  • 1
  • 1
Arpit
  • 767
  • 7
  • 20

1 Answers1

1

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.

sehe
  • 374,641
  • 47
  • 450
  • 633
  • In Self-contained example, you want the small running code? I will try to trim my code. I have also tried using `async_read_some`. With that also I am getting `EOF`. Not sure if the above situation "Why EOF is an Error" applies to that or not! – Arpit Dec 28 '16 at 16:45
  • Is it normal to get an EOF error one in a million reads? It happens with my application after a 8-10 hours of run reading and writing like 6-7 packets per second. But then I am nor sure how to deal with. Sounds I should try to reconnect if I am the client? Looking at the official doc on the link you provided, it does not sound like I can ignore and continue to the next read? – TheWaterProgrammer May 09 '19 at 08:38