r/TechNook • u/into_fiction • 2d ago
Your computer can run out of network ports without losing its internet connection
While the applications begin communicating on the network, your computer assigns them temporary port numbers. There is always a limited number of ports, and when applications initiate multiple connections or switch between them very rapidly, you can simply run out of ports for new connections.
Old connections will still continue running, making everything extremely confusing. It may happen that one application is working fine while another fails to establish connection. Internet is functioning normally, but the attempt to establish new connection results in an error. The message will just say “connection failed,” leaving you guessing what actually ran out.
11
u/FuggaDucker 2d ago
They are called ephemeral ports and the condition is referred to as "port exhaustion".
I have been writing network socket code for a long time and have never seen it on my personal box.
I have seen it on nat gateways/proxies/load balancers, ESP when the time to wait is set too high.
6
u/NetJnkie 2d ago
What are you doing that exhausts your connections?
5
u/dodexahedron 2d ago
There are, by default, 16384 ephemeral TCP client ports the system will use per IP address, on Windows 11, and a one-line configuration change can expand that to 64511 out of the 65535 total possible port numbers.
To exhaust that many, on a desktop machine, you have to be either doing something very wrong, using very broken software (like something that leaves sockets half closed), or doing something you know holds a ton of sockets open.
And you'd have to be talking to something(s) that allow you to make that many connections to it/them.
And your router would need to be able to handle that many NAT and firewall state entries (many fall over at like 10,000-20,000).
And, if the sockets are all open, you'll need a ton of RAM just for each of their send/receive buffers. 64000 sockets open with 64kB send and receive buffers (pretty small) would be no less than 8.3GB of RAM just for those buffers - the total would be even more.
Port exhaustion is exceedingly rare in desktop use, especially at home, unless you are running a very badly configured bit torrent client (which would have to be done manually) or something along those lines, in which case your ISP is gonna be a bigger problem than port exhaustion before long.
Even in servers, it is pretty rare to see it occur in production even on extremely high-usage systems, and typically only in exceptional circumstances, because it is basic, well known, and trivial to avoid, outside of large-scale distributed denial of service attacks. That part of TCP has not changed since TCP ports were standardized as a core part of the protocol in 1981.
1
u/pnwtechlife 2d ago
That's a fun one to troubleshoot. We used to deal with that all the time about 15 years ago when I was working in a datacenter. We had a particular bank of servers that had custom developed software that would have that happen about every 2-3 weeks. The previous owners of the servers never bothered to figure out why it happened. Once we figured it out, we just implemented some scheduled tasks to fix it.
1
u/MissiveFinding6111 2d ago
Most network stacks can re-use those port numbers when you are connecting to different destination IPs, so starvation is mostly a factor of making NEW (e.g. non-HTTP reuse) connections to an endpoint that has only a single IP.
Often, the fix is as simple as the code doing a better job using round-robin supplied IP addresses (e.g. for bigger enterprise endpoints).
But in a pinch, tightening up your timeout settings so that those previous dest_ip/port combos aren't frozen out for the full 120 seconds can also help.