Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

It takes quite a moment for socket.read_some to return after socket.close has been called, in linux

I'm using Boost.Asio as a simple socket library.

When I open a socket, I create a thread which keeps reading on that socket, and returns when the socket has been closed, or some other errors occured.

while((read = socket->read_some(buf, ec)) != 0) {
    // deal with bytes read
}

This code works well on Windows and Mac. However with linux, when the socket is closed from the main thread, it takes quite long time for socket::read_some to return - I found it's more than 2 minutes.

Is there anything I can do to improve this?

like image 828
tactoth Avatar asked Nov 22 '25 07:11

tactoth


1 Answers

If you desire cancel-ability, use asynchronous sockets. Don't use synchronous methods such as read_some. This has been discussed ad infinitum on the asio-users mailing list. There's also a ticket on the boost bug tracker discussing it.

Also see my answer to a similar question.

like image 169
Sam Miller Avatar answered Nov 23 '25 22:11

Sam Miller



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!