The reason is that either
- the test starts a program, then gets the run id from the run history. This fails if the program has not started at the time of the second call; or
- the test stops the program, the immediately gets the history and asserts that is empty. This fails if the program has not fully stopped yet.
In both cases the fix is to add a waitState() to wait until the program is actually running or stopped.