Curious Minds

Clear answers to science and history questions

Why Netstat Shows Many CLOSE_WAIT Connections

· 6 min read · Science

Netstat shows many connections in CLOSE_WAIT status because the local application has received a TCP FIN (the remote peer has closed its end) but the local application has not yet called close() on its socket. In other words, CLOSE_WAIT is a waiting state on the local side: the kernel has acknowledged the remote close, but the application still holds the socket open, so the connection cannot fully terminate until the application closes it.

What CLOSE_WAIT Means in the TCP State Machine

TCP connections move through a defined set of states. CLOSE_WAIT occurs after the local host receives a FIN segment from the remote peer and sends an ACK in response. At that point the remote side has entered FIN_WAIT_2 (or a similar closing state), while the local side enters CLOSE_WAIT. The local application is expected to detect the end-of-stream condition on its socket (for example, a read returning zero bytes) and then call close() or shutdown(). Until that happens, the socket remains in CLOSE_WAIT and the connection is half-closed: data can still be sent from the local side to the remote side, but no more data will arrive.

The key point is that CLOSE_WAIT is not a network problem. It is an application-level condition. The kernel has done its job; the application has not. This is why seeing a few CLOSE_WAIT entries is normal and transient, while a large or growing number usually points to a bug or resource leak in the application.

Why Connections Accumulate in CLOSE_WAIT

When an application fails to close sockets after the remote peer disconnects, those sockets stay in CLOSE_WAIT. Over time, repeated connections from clients, load balancers, or health checks can cause the count to grow. Common causes include:

  • Missing close() calls: The code reads from the socket, sees the connection is finished, but never explicitly closes the socket or releases the associated resources.
  • Exception paths that skip cleanup: An error occurs during processing, and the cleanup code that would close the socket is bypassed.
  • Blocked or stuck threads: A thread responsible for closing the connection is blocked on another operation, so the close never happens.
  • Connection pool mismanagement: A pool returns connections to the pool without properly detecting that the peer has closed them, leaving stale sockets in CLOSE_WAIT.
  • Forked or multithreaded designs: A child process inherits a socket but never closes it, while the parent also does not close it, leaving the socket open in CLOSE_WAIT.
  • Improper use of shutdown(): Calling shutdown() on one direction without eventually calling close() can leave the socket in CLOSE_WAIT indefinitely.

Because CLOSE_WAIT sockets consume file descriptors and memory, a large accumulation can eventually exhaust the process's file descriptor limit, causing new connections to fail. This is often the first visible symptom: the application stops accepting connections, and netstat shows hundreds or thousands of CLOSE_WAIT entries.

How to Diagnose the Source

Diagnosing CLOSE_WAIT accumulation requires linking the sockets to the owning process. On Linux, tools such as ss (which is faster and more informative than netstat) and lsof can show which process holds each socket. A typical workflow:

  1. Count CLOSE_WAIT sockets: ss -tan state close-wait | wc -l or netstat -tan | grep CLOSE_WAIT | wc -l.
  2. Identify the owning process: ss -tanp state close-wait shows the process ID and name for each socket (requires appropriate privileges).
  3. Inspect the application's code paths for socket handling, especially error and timeout paths.
  4. Check for thread or connection pool starvation that could prevent cleanup.
  5. Monitor the count over time: a steady low number is normal; a rising trend indicates a leak.

On Windows, netstat -ano shows the owning process ID, which can be matched in Task Manager. On macOS, lsof -i and netstat -an provide similar information.

CLOSE_WAIT vs. TIME_WAIT and Other States

CLOSE_WAIT is often confused with TIME_WAIT, but they are different. TIME_WAIT occurs on the side that initiates the close, after the final ACK is sent, and lasts for a fixed period (typically twice the maximum segment lifetime, often around 60 seconds on many systems). TIME_WAIT is a normal part of TCP and is managed by the kernel; it does not indicate an application bug. CLOSE_WAIT, by contrast, is on the side that receives the close and is waiting for the local application to close. A large number of TIME_WAIT sockets can indicate a high rate of short-lived connections, but they clear on their own. A large number of CLOSE_WAIT sockets will not clear until the application acts.

Other states worth knowing: ESTABLISHED means the connection is active; FIN_WAIT_1 and FIN_WAIT_2 are intermediate states on the closing side; LAST_ACK is the final state before the socket is released. Each state has a distinct meaning, and only CLOSE_WAIT points squarely at the application's failure to close.

Common Misconceptions

A frequent misconception is that CLOSE_WAIT connections are caused by network problems or by the remote peer. In fact, the remote peer has already done its part by sending FIN. The local kernel has acknowledged it. The only missing step is the local application's close(). Another misconception is that adjusting kernel parameters (such as tcp_fin_timeout) will fix CLOSE_WAIT. It will not: those parameters affect TIME_WAIT and other kernel-managed states, but CLOSE_WAIT persists until the application closes the socket. Restarting the application will clear the sockets temporarily, but the underlying bug will recur unless the code is fixed.

It is also worth noting that a small number of CLOSE_WAIT sockets is normal and expected in any busy server, because there is always a brief window between the remote FIN arriving and the application processing the end-of-stream and closing. The problem is a large number or a steadily growing number.

How to Fix and Prevent CLOSE_WAIT Accumulation

Fixing CLOSE_WAIT requires changes in the application, not the kernel. The general approach:

  • Ensure every socket is closed in a finally block or equivalent cleanup path, even when exceptions occur.
  • Detect end-of-stream correctly: a read returning zero bytes means the peer has closed; the application should then close its socket.
  • Use timeouts on blocking operations so that a stuck thread cannot hold a socket indefinitely.
  • For connection pools, validate connections before reuse and discard those that have been closed by the peer.
  • Monitor the CLOSE_WAIT count as a health metric and alert on sustained growth.
  • Review multithreaded or forked designs to ensure sockets are not inherited and left open by processes that never use them.

In some cases, a library or framework may be responsible for the leak. Upgrading to a patched version or switching to a better-maintained library can resolve the issue. When in doubt, reproducing the problem under a debugger or with socket-level tracing can reveal exactly where the close is missing.

Frequently Asked Questions

Is it normal to see some CLOSE_WAIT connections?

Yes. A small number is normal because there is always a short interval between the remote FIN arriving and the application closing the socket. The concern is a large number or one that grows over time.

Can I clear CLOSE_WAIT connections without restarting the application?

No, not directly. The kernel cannot force the application to close a socket. You can identify and kill the owning process, but that is a restart in effect. The proper fix is in the application code.

Does increasing the file descriptor limit help?

It postpones the failure but does not fix the leak. Raising the limit gives the application more headroom, but the CLOSE_WAIT sockets will still accumulate until the limit is reached again.

Why do CLOSE_WAIT connections sometimes disappear on their own?

They disappear only when the application eventually closes the socket, for example when a garbage collector finalizes an object, a timeout fires, or the process exits. Without such an event, they persist indefinitely.

Are CLOSE_WAIT connections a security risk?

They are not a direct security vulnerability, but they can be a denial-of-service risk if they exhaust file descriptors and prevent new connections. They can also indicate a bug that might have other consequences.

What is the difference between CLOSE_WAIT and FIN_WAIT_2?

CLOSE_WAIT is on the side that received the FIN and is waiting for the local application to close. FIN_WAIT_2 is on the side that sent the FIN and is waiting for the remote side to close. Both are half-closed states, but they occur on opposite ends of the connection.

In this article
  1. What CLOSE_WAIT Means in the TCP State Machine
  2. Why Connections Accumulate in CLOSE_WAIT
  3. How to Diagnose the Source
  4. CLOSE_WAIT vs. TIME_WAIT and Other States
  5. Common Misconceptions
  6. How to Fix and Prevent CLOSE_WAIT Accumulation
  7. Frequently Asked Questions