Operations 5 min read

Fix 'Cannot Assign Requested Address': Two Kernel Tweaks to Recycle TIME_WAIT Fast

The article explains that 'Cannot assign requested address' errors under high concurrency stem from slow TIME_WAIT recycling, not port exhaustion, and shows how enabling tcp_tw_reuse and tcp_timestamps instantly solves it without a restart, while warning against the deprecated tcp_tw_recycle.

Engineer Tang's Implementation Study Notes
Engineer Tang's Implementation Study Notes
Engineer Tang's Implementation Study Notes
Fix 'Cannot Assign Requested Address': Two Kernel Tweaks to Recycle TIME_WAIT Fast

Seeing the error "Cannot assign requested address" at 2 a.m. is a familiar panic. Running ss -s reveals tens of thousands of connections stuck in TIME_WAIT.

Contrary to intuition, the port pool is not the bottleneck. Linux defaults the local port range to roughly 28,000–60,000 (about 32,000 usable ports). A short‑lived connection opens and closes in milliseconds; if the kernel recycled ports fast enough, 32,000 ports would suffice.

The real choke point is the 60‑second TIME_WAIT timeout. The author likens it to a convenience store with ten cashiers where each cashier must stare into space for 60 seconds after every transaction — queues build up even though all registers are open.

The fix is two sysctl settings that take effect immediately, no reboot required:

net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_timestamps = 1

With tcp_tw_reuse enabled, the kernel checks the TCP timestamp of a TIME_WAIT socket; if the timestamp proves the connection is truly dead, the port is reused instantly instead of waiting the full 60 seconds.

Critical warning: Many guides suggest enabling tcp_tw_recycle. Do not use it. Kernels 4.12 and later have removed it entirely. On older kernels, enabling it behind any NAT (which is virtually all production environments) breaks new connections — a classic case of good intentions causing harm.

To verify the fix, run: ss -ant | grep TIME-WAIT | wc -l The count may still show thousands of TIME_WAIT entries; that is normal. The problem is solved as long as "Cannot assign requested address" stops appearing.

For high‑concurrency short‑connection workloads, these two parameters are essentially mandatory. Skipping them leads to mysterious pressure‑test failures that waste hours of debugging, only to discover the kernel knobs were never turned.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

network troubleshootingHigh ConcurrencyLinux kernelTIME_WAITtcp_tw_recyclesysctlTCP tuningtcp_tw_reuse
Engineer Tang's Implementation Study Notes
Written by

Engineer Tang's Implementation Study Notes

Old Tang | Pseudo-full-stack tech player: writes code without going bald, manages projects without going crazy, delivers without staying up late. No formal tutorials here, only a "slacking-off" practical manual that cures system pain points. Focuses on Shell, SQL, Java, and delivery, turning complexity into simplicity and making efficiency fun.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.