MacOS Monterey Port 5000 Already In Use

2022-01-03
Python
Flask
development
OSX
Unix ports

Development port

Many web development frameworks are using some abstract port on local machine for development purposes, such as serving the results (pages, API endpoints) locally for the testing/validation purposes. Those ports are usually chosen to be easy to remember, for instance 8080, 8000, 3000, 5000 etc. And since (somewhat) standardised port range is only those below 1024, the port usage was always a bit of a chaos.

The problem I faced recently was with a default development port (5000) suggested by Python Flask framework.

New conflict

I recently updated my laptop to OSX Monterey, and when trying to launch the Flask development server I faced the "Port 5000 already in use" error. At first, I thought it might be a previous dev server instance hanging around, but looking at it with

lsof -i tcp:5000

showed that it's something else:

[halien@airhydra:~/devel]# lsof -i tcp:5000
COMMAND     PID   USER   FD   TYPE             DEVICE SIZE/OFF NODE NAME
ControlCe 18536 halien   19u  IPv4 0x4008cac8d519c5c9      0t0  TCP *:commplex-main (LISTEN)
ControlCe 18536 halien   20u  IPv6 0x4008cad72f646471      0t0  TCP *:commplex-main (LISTEN)

After some googling, turned out that OSX Control Center is now listening on port 5000 as an AirPlay server. According to the comments in this thread, port 5000 usage for AirPlay is nothing new, it's just the OSX now supports it:

The Remote Audio Output Protocol, AKA AirTunes, AKA AirPlay, has been using port 5000 since the year 2004. This is nothing new. All AirPlay receivers including AirPort Express and Apple TV use port 5000. If you've ever used Airfoil on your Mac (Airfoil Speakers was released in 2008), that also uses 5000 for the same reason. AirPlay receiving is new to macOS Monterey, but AirPlay itself is very old, predating Flask and these other web development environments.

The solution

A more robust solution would be to use a different development port - even a small change like using 5001 would do. However, that could mean changing it in a lot of places, and if you're working in a team that could potentially (depending on the collaboration setup) affect other developers.

Another way is to disable AirPlay server via System Preferences › Sharing, and unchecking AirPlay Receiver