Listeners on Private Ports – race condition edition

Ha!  I have here a service X that is listens for it’s clients on private port N.  It failed to start because another activity Z was using N.  Looking at the state of things it’s clear that Z got N randomly assigned when it established a connection to a service on another machine.

This is the kind of bug you discover only if you get a short debug loop around what is typically a rare activity – rebooting the server in this case.  Gosh the chances of this happening are small.

I have been parking my private listeners in the private port range (49152 to 65535) for almost 40 years.  Maybe, I need to stop doing that.  Or at least assure that all listeners get started before any other activities start using the net – yeah right.

But now I’m confused.  I see that there is a concept of “ephemeral ports,” but the Wikipedia article leaves one with the impression that in practice you don’t know what range they are being drawn from.

So now I’m a bit confused what best practice might be.  Advocates of resource discovery score a few more points?

Leave a Reply

Your email address will not be published. Required fields are marked *