For the first time in my career, I ran into port exhaustion. On a Windows server, our
.NET 5.0 application kept exhausting the available TCP ports. The resolution, among
other things, involved increasing MaxUserPorts
and decreasing
TcpTimedWaitDelay
.
It looks like this in PowerShell:
Get-ItemProperty -Path "HKLM:\SYSTEM\CurrentControlSet\Services\Tcpip\Parameters"
TcpTimedWaitDelay : 30
MaxUserPorts : 65534
After modifying the registry, we DO need to restart the server.
After doing that, again from PowerShell, we can see fewer active TCP connections in the
TimeWait
state:
$props = [System.Net.NetworkInformation.IPGlobalProperties];
$props::GetIPGlobalProperties().GetActiveTcpConnections() |
Group-Object -Property State |
Select-Object Count, Name;
Note how few connections have the state of TimeWait
.
For more information about the various states, IBM
has a useful diagram and table and this answer
on StackOverflow explains how to set the TcpTimedWaitDelay
value
safely. From my beginners perspective, the TIME_WAIT
period helps to
ensure the conversation (and its echo, so to speak) has ended before reusing the same
connection.