Windows 10 and Windows Server 2016 introduced the SO_REUSE_UNICASTPORT
socket option. It was made available for use in .NET starting from version 4.6 via the ServicePointManager.ReusePort
static property. I'm suffering from ephemeral port exhaustion on a .NET application during very high loads (many concurrent outgoing requests via HttpClient
), and I am considering using this option to deal with it. I'm aware of other ways to deal with the problem (such as editing the Windows Registry to modify the max number of ephemeral ports or shortening TIME_WAIT), but I'd like to fully under this solution too.
The documentation for ServicePointManager.ReusePort
is very minimal:
Setting this property value to true causes all outbound TCP connections from HttpWebRequest to use the native socket option SO_REUSE_UNICASTPORT on the socket. This causes the underlying outgoing ports to be shared. This is useful for scenarios where a large number of outgoing connections are made in a short time, and the app risks running out of ports.
Looking at the documentation for SO_REUSE_UNICASTPORT
doesn't provide any additional insights:
When set, allow ephemeral port reuse for Winsock API connection functions which require an explicit bind, such as ConnectEx. Note that connection functions with an implicit bind (such as connect without an explicit bind) have this option set by default. Use this option instead of SO_PORT_SCALABILITY on platforms where both are available.
I couldn't find any explanation on the web as to how exactly this "ephemeral port reuse" is achieved, how exactly it works on a technical level, and how well it reduces the risk of ephemeral port exhaustion. How much of an improvement can I expect? Using this feature, how can I calculate a new limit for my application? Are there any downsides to enabling this?
This is all shrouded in mystery and I'd love it if someone can explain this new mechanism and its implications.
System.Net.Sockets.SocketException: Only one usage of each socket address (protocol/network address/port) is normally permitted
exception, and runningnestat -a -o
on the machine that has that exception showed numerous ephemeral ports in use. – Fick