For the mongod processes that resmoke.py creates, an unused port is requested from the OS
by creating a socket, closing it, and then handing the port number that was received from the OS to the mongod command line. Note that this is racy in a way that is not easy to fix.
However, another issue arises from the way resmoke.py starts multiple mongod processes when the --jobs=N flag is specified. The MongoDFixture.setup() method starts the process, but returns before attempting to establish a connection to the mongod. Instead, resmoke.py starts a mongod processes for each job without waiting to see if a connection can be established before starting the next. After all of the mongod processes are started, MongoDFixture.await_ready() is called on each to ensure a connection can be established. This was done so that the initialization of mongod processes can happen concurrently, which decreases the overall execution time.
The following sequence of events describes the problem:
The proposal is to have the UnusedPort class keep track of the set of ports that have been acquired by each instance and to request another port from the OS if a duplicate is received.