TIME_WAIT consumes file descriptors

kernelsockettcpipunix

In many TCPIP and Web tunning guide, recommends increasing max number of file descriptors
when getting the error "Too many open files"

but, i can't see TIME_WAIT in outputs of "lsof -i"

Does anyone know that TIME_WAIT consumes file descriptors? or not

Best Answer

The file descriptor is used by application to read/write from the socket. Thus if the application call close(), the file descriptor is immediately released.

On the other hand, if the application call shutdown(), file descriptor will remain effective so that application can still read/write from/to the socket.

Quotes from https://oroboro.com/file-handle-leaks-server/:

Myth: Sockets in TCP TIME_WAIT are holding file handles Hostage

When you close a TCP/IP socket the operating system does not release the socket right away. For complex reasons, the socket structure must be kept out of circulation for a few minutes because there is a small chance that an IP packet may arrive on that socket after it has been closed. If the operating system re-used the socket then the new user of that connection would have their session affected by someone else’s lost packets.

But this does not hold a file handle open. When you close the socket’s file descriptor, the file descriptor itself is closed. You will not get the “Too many files open” error. If you have too many sockets open, then your server may stop accepting new connections. There are ways to deal with that ( allowing sockets to be re-used, or lowering TCP TIME_WAIT )- but raising the file handle limit isn’t one of them.

Myth: It takes time for file handles to be released

This is related to the TCP TIME_WAIT myth. The mistaken belief that when you close a file handle that you must wait some time for the operating system to release the handle.

Closing a file handle will call into whatever os method releases the resource, and the OS will release that resource either immediately, or sometimes later as in the case with sockets, but close() will release the file handle in the file handle table immediately. Your process is in complete control of its file handle table, and doesn’t need to wait for anything to free a slot in its own file descriptor table.