Quick revision
The short answer
TIME_WAIT is the state of the side that closed the connection first; it is normal and lasts about a minute, so thousands of them usually mean we open and close connections instead of reusing them. CLOSE_WAIT means the peer has closed its side but our application has not yet closed its socket, so many CLOSE_WAIT sockets point to a leak in our Java code, typically HTTP response bodies or connections that are never closed. We fix leaks with try-with-resources and by consuming or closing every response body, and we reduce TIME_WAIT with connection pooling and keep-alive.
What to remember
In this answer, we saw that TIME_WAIT is the normal state of the side that closed first, held for about 60 seconds, and that thousands of them signal connections being opened and closed instead of reused, fixed with pooling and keep-alive. CLOSE_WAIT means the peer has closed but our application has not, so growing numbers point to a leak in our Java code, usually HTTP response bodies or sockets never closed, which we fix with try-with-resources and by always consuming or closing response bodies.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
TIME_WAIT is the state of the side that closed the connection first; it is normal and lasts about a minute, so thousands of them usually mean we open and close connections instead of reusing them. CLOSE_WAIT means the peer has closed its side but our application has not yet closed its socket, so many CLOSE_WAIT sockets point to a leak in our Java code, typically HTTP response bodies or connections that are never closed. We fix leaks with try-with-resources and by consuming or closing every response body, and we reduce TIME_WAIT with connection pooling and keep-alive.
Why this question is asked
Socket states show up in production when something is already wrong: a service starts failing to open connections, file descriptors run out, or a monitoring alert counts thousands of sockets in strange states. Knowing what each state means lets us tell in a minute whether we have a harmless side effect of our traffic pattern or a resource leak that will eventually take the service down. Interviewers ask this debugging question to see whether we understand TCP closing well enough to connect a state to a line of code. In this answer, we will recap how a TCP connection closes, explain TIME_WAIT and CLOSE_WAIT, find which side and which code is responsible, show the typical leak and its fix, and reduce TIME_WAIT the right way.
1A Quick Recap: How a TCP Connection Closes
A TCP connection has two directions, and each side closes its own direction separately by sending a FIN, a packet that says I have nothing more to send. The side that closes first is the active closer; the other is the passive closer. Each FIN is acknowledged with an ACK. Between those packets, each side's socket moves through named states, which tools such as ss or netstat show us.
Two of those states can pile up. The passive closer sits in CLOSE_WAIT from the moment it receives the peer's FIN until its own application calls close(). The active closer, after the whole exchange, sits in TIME_WAIT for a fixed period before the socket disappears.
6 messages in order. Play them, or step through them one at a time.
CLOSE_WAIT waits for our code; TIME_WAIT waits for a timer.
2What TIME_WAIT Means
TIME_WAIT exists to protect new connections. For a while after closing, delayed packets from the old connection could still be in the network; if a new connection reused the same addresses and ports immediately, those packets could be mistaken for new data. So the active closer keeps the old socket in TIME_WAIT for twice the maximum segment lifetime, which on Linux is a fixed 60 seconds. It also lets the final ACK be resent if it was lost.
TIME_WAIT is therefore normal, and it needs no action from our code. But thousands of them tell us something about our traffic: we are closing many connections per second. If our service is the client calling another service and opens a new connection per request, every request leaves one TIME_WAIT socket on our side for a minute. At 1,000 requests per second, that is about 60,000 sockets. Since each outgoing connection to the same destination uses a local port from a limited range, roughly 28,000 ports by default on Linux, we can run out and see connection errors such as Cannot assign requested address.
3What CLOSE_WAIT Means and Why It Points to Our Code
CLOSE_WAIT means the other side has closed, the kernel has acknowledged it, and the kernel is now waiting for our application to call close() on its socket. Nothing in the network can move it forward. A socket stays in CLOSE_WAIT until our code closes it, possibly forever.
A few CLOSE_WAIT sockets that come and go are just connections in the middle of closing. Thousands that keep growing mean our code is not closing sockets after the peer has gone, which is a leak. Each one holds a file descriptor and buffers, and when the process reaches its file descriptor limit, everything fails with Too many open files. So of the two states, CLOSE_WAIT is the one that points to a bug in our Java code.
The peer address is the clue. Here the CLOSE-WAIT sockets are outgoing connections from our Java process to 10.0.9.21 on port 443, a service we call. So the leak is in our HTTP client code for that service: connections that the server closed, for example after its idle timeout, but that our code never released.
4The Typical Leak: Response Bodies Not Closed
HTTP clients reuse connections through a pool, but a connection only goes back to the pool, or gets closed, when the response body has been fully read or explicitly closed. Code that reads the status and returns early leaves the body open, so the connection stays checked out. When the server later closes its idle side, our socket moves to CLOSE_WAIT and stays there.
The same pattern applies to every client: in Apache HttpClient, close the response or consume its entity; in OkHttp, close the Response or its body; with Spring's RestClient or WebClient, use the APIs that read the body for us, such as retrieve().body(...), and be careful with exchange-style methods that hand us the raw response. Server-side code can leak the same way: an accept loop that hands sockets to handlers which never close them after readLine() returns null leaves one CLOSE_WAIT per departed client. try-with-resources on the socket fixes that.
5Reducing TIME_WAIT the Right Way
Because TIME_WAIT follows from closing connections, the real fix is to close fewer of them. We use a single, shared HTTP client with a connection pool and keep-alive, so one connection carries thousands of requests. With HTTP/2, many requests even share a connection at once. A sign that pooling is not working is a client created per request, for example new HttpClient or a new RestTemplate inside a method, which throws its pool away each time.
| State | Which side | What it tells us | Action |
|---|---|---|---|
TIME_WAIT | closed first (active) | normal; many = connections not reused | pooling, keep-alive, shared client |
CLOSE_WAIT | peer closed first | our code has not called close() | find and fix the leak |
CLOSE_WAIT growing | our process | leak: file descriptors will run out | close bodies, try-with-resources |
| ESTABLISHED | both | open connections | check pool size limits |
Kernel settings are a last resort. On Linux, net.ipv4.tcp_tw_reuse lets new outgoing connections reuse TIME_WAIT ports safely using TCP timestamps, and widening the local port range gives more room. The old tcp_tw_recycle option broke clients behind NAT and was removed from Linux in version 4.12; advice to enable it is outdated. And we should never try to avoid TIME_WAIT by aborting connections with SO_LINGER set to zero, which sends a reset and can lose data.
6Follow-up questions
Can we shorten TIME_WAIT on Linux?
Not directly: the 60-second duration is fixed in the Linux kernel, not a tunable. We can allow safe reuse of TIME_WAIT ports for outgoing connections with tcp_tw_reuse and widen the ephemeral port range, but the real fix is reusing connections.
How do we find the leaking code once we know the peer address?
Map the peer address to the downstream service, then look at the code paths that call it, especially early returns and error handling where a response body might not be closed. A heap dump showing many unreleased response or connection objects, or connection-pool metrics showing leased connections that never return, confirm it.
Why do CLOSE_WAIT sockets often appear on the client side?
Because servers close idle keep-alive connections after their own timeout. If our client is holding a leaked connection, the server's close moves it to CLOSE_WAIT on our side, and it stays there because our code no longer has a reference that would close it.
7Conclusion
In this answer, we saw that TIME_WAIT is the normal state of the side that closed first, held for about 60 seconds, and that thousands of them signal connections being opened and closed instead of reused, fixed with pooling and keep-alive. CLOSE_WAIT means the peer has closed but our application has not, so growing numbers point to a leak in our Java code, usually HTTP response bodies or sockets never closed, which we fix with try-with-resources and by always consuming or closing response bodies.
Open with
Both are states from closing a TCP connection, and they sit on opposite sides. TIME_WAIT is on the side that closed first. After the FIN and ACK exchange, that side keeps the socket for twice the maximum segment lifetime, 60 seconds on Linux, so delayed packets from the old connection cannot confuse a new one.
Then, if they want more
It is normal and needs no code change. But thousands of them usually mean we open a new connection for each request instead of reusing one, and on the client side that can exhaust ephemeral ports, roughly 28,000 by default, which at a thousand requests a second happens quickly.
The fix is a shared client with a connection pool and keep-alive. CLOSE_WAIT is the one that points to a bug in our code. It means the peer has closed its side and the kernel is waiting for our application to call close.
Nothing else moves it on, so a growing number is a leak, and eventually the process hits its file descriptor limit and fails with too many open files. I would run ss to see which process owns them and which peer address they point to, then look at the code calling that service.
The classic cause is HTTP response bodies that are not consumed or closed, for example an early return on a non-200 status, so the connection is never released. I would fix it with try-with-resources on the body or the response, and on the server side by closing accepted sockets.
About this answer
- Last updated
- 12 November 2026
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- The Java Language Specification, Java SE 21, Java SE 21 API documentation
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A service has thousands of sockets in TIME_WAIT and CLOSE_WAIT states. Explain what each...": https://scrivio-interview-library.netlify.app/java/networking/62d35c3628/