3

I have a situation where I am running into ephemeral port exhaustion, which is then causing exceptions. I would like to throttle the connections to avoid this. Is there a way to detect the number of ephemeral ports in use?

Thanks, Erick

Erick T
  • 7,009
  • 9
  • 50
  • 85
  • If you keep track of how many your application has opened, that'll be a good first-level approximation. I have strong doubts that you're running into ephemeral port exhaustion, since UDP and TCP keep track of sessions via (local ip, peer ip, local port, remote port) -- any single port could be used for hundreds of thousands of connections to a handful of peers or hundreds of thousands of peers. Check `netstat` output or your platform's equivalent of `netstat` to debug this before looking into code to work around this. Maybe a file-descriptor or socket limit is more likely. – sarnold Apr 19 '12 at 22:00
  • 1
    We ran into this situation on Azure, using Azure storage from an Azure VM. We confirmed that it was a ephemeral port issue from the error. Also, increasing the limit (from 5k -> 2k) and reducing the time to reuse solved the problem. The issue is that we don't want to do this on every VM. – Erick T Apr 20 '12 at 21:28
  • Azure doesn't by-default re-use existing connections??? – sarnold Apr 20 '12 at 21:39
  • It does appear to reuse connections, but not nearly enough. To be fair, this problem mostly occurs when we are doing an ETL style process that is very heavy on IO. – Erick T Apr 20 '12 at 22:21

2 Answers2

1

To get list of open connection you may use netstat command. Contrary to popular believe (of other answers) number of Ephemeral ports is limited (4000 on Windows 2003, some 16000 on later versions, 16000 on most Linuxes). Note also that it take 4 minutes to release port once it become unused therefore it really can be an issue.

There is article on MSDN dealing with it: http://msdn.microsoft.com/en-us/library/aa560610(v=bts.20).aspx

Lukas Kucera
  • 895
  • 7
  • 3
-1

The only way you can run out of ephemeral TCP ports is if you are a client and you have opened nearly 64k outbound connections, and not closed them within the last two minutes.

This sometimes happens in testing scenarios, but it doesn't represent a real-world case so it is of no real interest.

The symptom of this is bind errors when connecting. If you aren't getting bind errors you don't have this problem.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • 3
    We ran into this situation on Azure, using Azure storage from an Azure VM. We confirmed that it was a ephemeral port issue from the error. Also, increasing the limit (from 5k -> 2k) and reducing the time to reuse solved the problem. Going to Azure storage from an Azure VM is using a very close (1 hop) 10 GigE connection. Each storage operation uses a new TCP connection. By default, windows only reserves 5k ports for ephemeral ports. We can increase it, but that requires registry modifications and reboots. I'd much rather throttle when we approach the limit. – Erick T Apr 20 '12 at 21:31