This loop need not terminate in all cases.
For example: required 2 cores per executor; cluster has 10 nodes, each with 3 cores - and 2 cores of each is already being used.
Then we are stuck in an infinite loop.