cloudfoundry-collaboration/vcap — comment on commit ce5cc9d53822ea90be91694f60f44dad4d599b87
File: common/lib/vcap/common.rb line 38
By pbozeman:

This code is open to a race condition, especially if the port range is small.

While this code checks to make sure a port isn't in use, the port isn't reserved in anyway. 2 successive calls to this method can return the same port. If the port range is large, the probability of that happening is low, but with a small port range, the possibility increases. Consider the case that port N is allocated to AppA, then the launch of app A occurs, but before it binds to the port we allocate the same port N to app B and launch AppB. Then app A or B actually binds to the port and the other fails. If this happens, one of the apps will be considered to have crashed. If the port range is small on all of the DEAs, this can happen several times in a row and the system will think that the app is bad and will fail to continue trying to start it.

In order to take this change, you would need to address the above issue in someway. The ideal solution would be to provide a pool of ports that ports are taken from, reserved, an then returned to when they are done, similar to what is done for the secure user pool.