hydra-queue-runner: Fix a race keeping cancelled steps alive

If a step is cancelled just as its builder step is starting, doBuildStep() will return sRetry. This causes builder() to make the step runnable again, since the queue monitor may have added new builds referencing it. The idea is that if the latter condition is not true, the step's reference count will drop to zero and it will be deleted. However, if the dispatcher thread sees and locks the step before the reference count can drop to zero in the builder thread, the dispatcher thread will start a new builder thread for the step. Thus the step can be kept alive for an indefinite amount of time.

The fix is for State::builder() to use a weak pointer to the step, to ensure that the step's reference count can drop to zero before it's added to the runnable queue.

Created by  Eelco Dolstra  on November 8, 2016
DRC26KFBZIWIFT5CMAVMCHLDYWHUMKC33R3TW2KEZ5NTSVT2AFOAC
Change contents