Fix possible infinite loop in the control stream write template functions.