After calling socket.close in linux, socket.read_some takes a long time to return
I use Boost.Asio as a simple socket library.
When I open a socket
, I create a thread that keeps reading the socket and returns when the socket is closed or some other error occurs.
while((read = socket->read_some(buf, ec)) != 0) {
deal with bytes read
}
This code works fine on Windows and Mac. However, for Linux, socket::read_some
takes a long time to return when the socket is closed from the main thread – I found it to be more than 2 minutes.
Is there anything I can do to improve it?
Solution
If you will< a href="http://www.boost.org/doc/libs/1_46_0/doc/html/boost_asio/reference/basic_stream_socket/cancel/overload1.html" rel="noreferrer noopener nofollow" > cancel-ability , using asynchronous sockets. Do not use synchronous methods, such as read_some
. This has been discussed an unlimited number of times on the asio-users mailing list. There is also a ticket discussing it on the Boost bug tracker.
See also my answer Similar question.