An optimal control problem with a random stopping time