Stéphane
Stéphane

Reputation: 20350

boost::asio::async_read() of stream_descriptor now returning EOF

Upgraded Ubuntu today from 14.10 to 15.04. Now seeing different behaviour either in boost::asio::async_read(), boost::asio::posix::stream_descriptor, or tap/tun interfaces. Calling async_read() immediately returns boost::asio::error::eof. If I ignore the error and loop back up to start a new async_read() it does eventually read when bytes are available, and the application continues to work.

The problem with doing this workaround loop is the application now consumes 100% of a core as it sits in a tight loop continuously restarting the call to async_read().

This is how I'm setting things up:

fd = open("/dev/net/tun", O_RDWR);
....
boost::asio::posix::stream_descriptor my_stream( io_service);
my_stream.assign(fd, ec);
...
boost::asio::async_read(my_stream, my_buffer, boost::asio::transfer_at_least(16),
    [=](const EC &error, std::size_t bytes_read)
    {
        if (error) // <- this triggers with EOF error

Anyone know what may have changed in the newer kernels (tun/tap), or boost 1.55, to cause this end-of-file error when doing asynchronous reads?

Upvotes: 1

Views: 1441

Answers (1)

Tanner Sansbury
Tanner Sansbury

Reputation: 51891

Ubuntu 15.04 contains the 3.19 kernel, which has a reported regression in the TUN/TAP user API:

With kernel 3.19, a read() from a TUN/TAP file descriptor in non-blocking mode will return 0 when no data is available, rather than fail with EAGAIN.

Per the documentation, the return value from read() should only be 0 when no message has been read and the the peer has performed an orderly shutdown. Hence, the Boost.Asio implementation treats a return of 0 as an indication that the peer has shutdown, and completes the async_read() operation with an error code of boost::asio::error::eof:

// Read some data.
signed_size_type bytes = socket_ops::recv(s, bufs, count, flags, ec);

// Check for end of stream.
if (is_stream && bytes == 0)
{
  ec = boost::asio::error::eof;
  return true;
}

Upvotes: 2

Related Questions