1

TL;DR

I see that my sockets are in TIME_WAIT with the ss tool in Ubuntu 1804, but I can't find in the docs for boost sockets or on SO how to set the time delay to 0 such that the socket immediately closes (or better yet, set it to an arbitrarily small value for my application.

I am writing a socket application with Boost asio. Both the client and the server are using boost sockets. I see that, when the client sends a shutdown connection command: mysocket.shutdown( boost::asio::ip::tcp::socket::shutdown_both, error_code);, and my client C++ application closes down a 2 seconds later, I get TIME_WAIT on the output of ss -ap | grep application_port. I have been looking around SO and the internet looking for ways to set TIME_WAIT with Boost C++, but instead, I keep finding questions for why a TIME_WAIT happens. Here are some:

https://stackoverflow.com/questions/14666740/receive-data-on-socket-in-time-wait-state
https://stackoverflow.com/questions/35006324/time-wait-with-boost-asio
https://stackoverflow.com/questions/47528798/closing-socket-cause-time-wait-pending-state

If I am interpreting the internet correctly (and the TCP protocol), the reason why TIME_WAIT happens is because the server connection is waiting for an ACK to allow for the socket conn to die, while the client-side socket has already died.

The question, again:

Is there a way to set the TIME_WAIT delay option locally for a C++ executable using Boost sockets? If so, how?

Community
  • 1
  • 1
robotsfoundme
  • 418
  • 4
  • 18
  • You don't want to do this. The way to avoid the TIME_WAIT, if it is a problem, which it isn't, is to ensure that you are the end that receives the first close. In other words if you're a server have the client close the socket, and have the server react to that close by closing, with a read timeout of course to catch rogue clients. You want the TIME_WAIT states to accumulate at the clients, not the server. – user207421 Nov 22 '19 at 01:14

1 Answers1

0

Changing the SO_LINGER option should help: here's the discussion about it: TCP option SO_LINGER (zero) - when it's required

Boost has api for this reason so you can play around with changing linger to 0:

boost::asio::ip::tcp::socket socket(io_service);
boost::asio::socket_base::linger option(true, 30);
socket.set_option(option);

https://www.boost.org/doc/libs/1_50_0/doc/html/boost_asio/reference/socket_base/linger.html

Anyway, it's good to make sure if you really really have to do that. In case if you have a large number of sockets in TIME_WAIT it most probably means that the client site did not close connection gently. So if you can modify the client code, you can consider it as a first option. Here's nice explanation about how to gently finish the communication on TCP socket (it's not about boost but the logic is the same) Properly close a TCP socket

Most probably you are not closing the socket. The shutdown operation only informs that peer that there will be no more data to read. So most probably you have to: - call shutdown - make sure there's nothng more to read - close the socket

Vicctor
  • 803
  • 6
  • 13