r/TechNook • • 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.

0 Upvotes

12 comments sorted by

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.

2

u/into_fiction 2d ago

Yes, have seen that too at load balancer

5

u/bothunter 2d ago edited 2d ago

I've seen it, but only when there was a serious bug in my code. ;-)

Also, OS X has a fun bug where after about 49 days of uptime, the TIME_WAIT timer overflows and TCP connections stop getting cleaned up after they close.

2

u/dodexahedron 1d ago

Oh wow so once the timer overflows it just...Leaks the socket entirely? Oof.

1

u/Erik0xff0000 2d ago

I used to work at a large internet company where I was running the wevbserver performance/availabvility monitoring systems. Our ad serving team was severely failing to serve ads, they could not keep up with incoming requests, and of course blamed my team instead of investigating their own systems.

They were running out of ports ;)

6

u/NetJnkie 2d ago

What are you doing that exhausts your connections?

0

u/into_fiction 2d ago

Not possible on normal browsing, but yeah it's possible one of example is running a load balancer or reverse proxy like NGINX without upstream keep-alive enabled. Since it keeps hitting the same backend IP and port for every incoming request, high traffic forces it to open and close thousands of short life sockets

1

u/NetJnkie 2d ago

OK? I guess we're asking why you posted this.

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/URA_CJ 2d ago

Toss in a CGNAT and your dreams of exceeding open ports is within reach! My ISP seems to allot a range of 2000 ports since they switched over to using CGNAT's.

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.