Semaphore fails if there is a build on the queue

To fix that we use the last non-pending build instead of the penultimate.