Avoid race conditions when using get-port
mainThere are two types of race conditions to be aware of:
In-process race conditions
To prevent multiple calls within the same process (e.g., parallel Jest tests) from receiving the same port, get-port uses a lightweight locking mechanism. Returned ports are held for 15-30 seconds before they can be reused.
Solution: If your delay between calling getPort() and binding to the port exceeds 30 seconds (common in long-running test suites), use the {reserve: true} option to lock the port for the entire lifetime of the process.
Multi-process race conditions
There is a very small chance another external process might claim a port between the time you request it and the time you bind to it. This will result in an EADDRINUSE error, which your application should handle by retrying.