# Networking: Thousands of sockets in CLOSE_WAIT — the application is not closing its side of the connection

**Code:** `Many sockets in CLOSE_WAIT` · **Category:** Networking · **Seen on:** Linux, Java, Networking

The other end closed the connection, but your program never called close(), so the socket stays open.

## Meaning

CLOSE_WAIT is entirely local: the kernel is waiting for the application to close. It points to a connection or file-descriptor leak (missing `close()`/`finally`, unreleased HTTP responses, pooled connections). Find the process with `ss -tanp`, fix the leak, and watch for "Too many open files".

## Common causes

- Application never closes sockets (leak)
- HTTP client response body not consumed/closed
- Connection pool not releasing
- Exception paths skip close()
- Thread stuck so it never reaches close()

## Quick fix

1. Find the owner: `ss -tanp state close-wait`
2. Fix the leak: close in finally/using/defer
3. Restart as a stopgap and monitor the count

## Detailed fix by platform

### Linux

1. `ss -tanp state close-wait | awk "{print $NF}" | sort | uniq -c | sort -rn | head`

## How to diagnose

1. **State** — CLOSE_WAIT?
2. **Process** — Which one?
3. **FD limit** — Near?

---
Source: [WhatIsMyError](https://whatismyerror.com/errors/networking/close-wait-sockets-pile-up)
