Too Many FIN_WAIT2 Connections on Linux? Troubleshooting TCP Teardown, Unclosed Peers, and tcp_fin_timeout
Create Time:2026-09-01 15:50:42
浏览量
1019

Seeing a large number of FIN_WAIT2 connections on a Linux server can be alarming, especially when the count continues to rise or coincides with connection failures. However, FIN_WAIT2 is a normal part of TCP connection teardown. The real question is whether these sockets are short-lived and expected, or whether an application, proxy, or remote peer is failing to complete the close sequence.

This guide explains what FIN_WAIT2 means, how to identify the service and remote peers involved, how to distinguish a protocol problem from resource pressure, and when the Linux net.ipv4.tcp_fin_timeout setting is relevant.

What Does FIN_WAIT2 Mean?

TCP normally closes through a four-step exchange. When the local side actively closes a connection, it sends a FIN. After the peer acknowledges that FIN, the local socket enters FIN_WAIT2 and waits for the peer to send its own FIN. Once that FIN arrives, the local side acknowledges it and moves toward final cleanup.

Therefore, a FIN_WAIT2 socket usually means:

  • The local endpoint has finished sending data and initiated the close.

  • The remote endpoint acknowledged the local FIN.

  • The remote endpoint has not yet sent its own FIN.

A brief FIN_WAIT2 period is normal. A sustained or continuously growing count may indicate that the remote application is keeping its side open, an intermediary has mismatched timeout behavior, or the connection is an orphan that the kernel must eventually remove.

Confirm Whether the Count Is Actually Accumulating

Start by measuring the state rather than relying on a single snapshot:

ss -ant state fin-wait-2 | wc -l
ss -s
watch -n 5 'ss -ant state fin-wait-2 | wc -l'

Watch the trend during both normal and peak traffic. A large but stable count on a busy proxy may be expected. A count that rises without falling is more suspicious. Record the time, traffic volume, application error rate, and deployment events so that the socket trend can be correlated with actual service behavior.

List the affected connections:

ss -ant state fin-wait-2

Pay attention to local ports, remote addresses, and whether the same destination repeatedly appears.

Identify the Workload Behind the Connections

Group by remote endpoint

ss -ant state fin-wait-2 | awk 'NR>1 {print $5}' | sort | uniq -c | sort -nr | head -20

If one remote IP address or port dominates the list, investigate that peer, upstream service, load balancer, or proxy first.

Group by local endpoint

ss -ant state fin-wait-2 | awk 'NR>1 {print $4}' | sort | uniq -c | sort -nr | head -20

This often reveals the local service involved. For example, repeated connections from one local service port may point to a web server, reverse proxy, API gateway, database client, or application worker.

Show the owning process when available

sudo ss -antp state fin-wait-2

The process information is important because it separates sockets that are still attached to a live process from orphaned sockets whose file descriptor has already been released.

Check Whether a Process Still Owns the Socket

If ss -p reports a PID, inspect the process rather than immediately tuning the kernel:

pid=1234
ls -1 /proc/$pid/fd | wc -l
cat /proc/$pid/limits | grep -i 'open files'

Compare the open descriptor count with the process limit. Also inspect application logs, request latency, worker utilization, connection-pool behavior, and graceful-shutdown logic.

If a live process still owns the FIN_WAIT2 socket, the kernel's orphan timeout is not a universal cleanup switch for that socket. The application or peer behavior remains the primary investigation target.

Why Does the Peer Fail to Send Its FIN?

1. The peer application has not completed shutdown

The remote program may have acknowledged the local FIN but kept its own write side open. This can happen because of blocked workers, incomplete stream handling, connection leaks, or code that never closes the socket after receiving end-of-stream.

2. Proxy and load-balancer timeouts do not match

A reverse proxy, CDN, load balancer, service mesh, or NAT device may have different keep-alive and idle timeout values from the origin application. When one layer closes earlier than another, half-closed connections can remain visible until the remaining side finishes or times out.

Remember that a client-to-CDN connection and a CDN-to-origin connection are separate TCP sessions. A FIN_WAIT2 socket on the origin does not automatically prove that the end user's device is responsible.

3. The protocol intentionally uses long-lived connections

Streaming responses, WebSockets, server-sent events, long polling, and large uploads require careful interpretation. An aggressive timeout change can interrupt legitimate sessions even if it reduces the displayed socket count.

4. A teardown packet is delayed or dropped

Packet loss, asymmetric routing, firewall state, or a faulty intermediary can prevent the final FIN from reaching the local host. Capture traffic for one representative peer:

sudo tcpdump -nn -i any 'host 203.0.113.10 and tcp port 443'

Replace the example address and port with the affected connection. Check whether the local FIN is acknowledged and whether the peer later sends its own FIN or a reset.

Can tcp_fin_timeout Fix FIN_WAIT2 Accumulation?

Check the current value:

sysctl net.ipv4.tcp_fin_timeout

Linux kernel documentation describes tcp_fin_timeout as the time an orphaned connection can remain in FIN_WAIT2 before the local endpoint aborts it. The word orphaned is essential: this parameter does not apply identically to every FIN_WAIT2 socket, especially sockets still owned by an application.

A temporary test may look like this:

sudo sysctl -w net.ipv4.tcp_fin_timeout=30

The value 30 is only an example, not a universal recommendation. Before changing it:

  • Record the original value and prepare a rollback command.

  • Confirm that the affected sockets are orphaned.

  • Test during a controlled maintenance window.

  • Monitor application errors, resets, retries, and long-lived sessions.

  • Make a persistent change only after the temporary test proves safe.

Reducing the timeout may shorten the lifetime of orphaned FIN_WAIT2 sockets, but it does not repair a peer that fails to close, an application leak, or mismatched proxy timeouts.

Check Whether System Resources Are Under Pressure

A high FIN_WAIT2 count alone does not prove ephemeral-port exhaustion. Verify the surrounding resource indicators:

ss -s
cat /proc/sys/net/ipv4/ip_local_port_range
cat /proc/sys/fs/file-nr
ulimit -n

Look for connection errors, file-descriptor pressure, failed outbound connects, and heavy concentration in one source-address and destination tuple. Ephemeral ports are selected from the configured local range, but actual exhaustion depends on traffic patterns, destination combinations, socket reuse, and other TCP states.

Avoid widening the local port range or raising file limits as the first response. Those changes can provide headroom, but they may also hide a connection-management defect.

Recommended Troubleshooting Order

  1. Measure the trend. Confirm whether FIN_WAIT2 is stable, temporary, or continuously accumulating.

  2. Group the sockets. Identify the dominant local ports, remote peers, and affected traffic path.

  3. Find the owner. Use ss -p and application logs to determine whether sockets remain attached to a process.

  4. Inspect the close sequence. Capture representative traffic and confirm which side fails to complete TCP teardown.

  5. Compare timeout settings. Review application, reverse-proxy, CDN, load-balancer, firewall, and NAT timeouts as one chain.

  6. Check resource impact. Verify port, descriptor, memory, and connection-error evidence instead of assuming exhaustion.

  7. Test kernel tuning last. Change tcp_fin_timeout only when orphaned FIN_WAIT2 sockets are genuinely involved and rollback is ready.

Frequently Asked Questions

Is FIN_WAIT2 the same as TIME_WAIT?

No. FIN_WAIT2 waits for the peer's FIN after the local FIN has been acknowledged. TIME_WAIT occurs after the active closer has received the peer's FIN and sent the final acknowledgment. The causes and relevant tuning parameters are different.

Does many FIN_WAIT2 connections mean the server is under attack?

Not necessarily. The count may reflect normal traffic, a slow or faulty peer, proxy timeout differences, application shutdown behavior, or packet loss. Use peer distribution and traffic captures before drawing a security conclusion.

Should I immediately lower tcp_fin_timeout?

No. First determine whether the sockets are orphaned and whether they cause measurable resource pressure. A lower value can remove orphaned sockets sooner, but it does not fix every FIN_WAIT2 condition.

Can a CDN cause FIN_WAIT2 on the origin?

A CDN or proxy can be part of the path, but the origin sees a separate TCP session from the client-facing session. Compare origin keep-alive behavior, upstream timeout settings, connection reuse, and packet captures before assigning the cause.

How can I tell whether ephemeral ports are exhausted?

Check the configured local port range, active connection distribution, failed-connect errors, and the exact local-to-remote tuples. Socket count alone is insufficient evidence.

Conclusion

FIN_WAIT2 is a normal TCP state, not an error by itself. The most useful investigation starts with trend measurement, socket grouping, process ownership, and packet-level confirmation. If the remote peer or an intermediary is not completing the close sequence, fix that behavior or align timeout settings first.

Use tcp_fin_timeout carefully and only with a clear understanding that it targets orphaned FIN_WAIT2 connections. Controlled testing, monitoring, and rollback are more reliable than copying a single kernel value from an unrelated environment.

References

  1. RFC Editor: RFC 9293, Transmission Control Protocol (TCP). Reviewed September 1, 2026.

  2. Linux Kernel Documentation: IP Sysctl — tcp_fin_timeout. Reviewed September 1, 2026.

  3. man7.org: ss(8) — another utility to investigate sockets. Reviewed September 1, 2026.