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

主宰稳场 提交于 2020-01-03 05:12:32

问题


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?


回答1:


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;
}


来源:https://stackoverflow.com/questions/29841168/boostasioasync-read-of-stream-descriptor-now-returning-eof

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!