If you want a subroutine to stop the program, as in an end of bar subroutine, for example, you must cancel the Cycle Repeat mode before the M30 command:
}2 (sub 2: no more bars)
m11 (open collet and feed)
m31 (cancel cycle repeat)
m30 (no more bars)
m99 (sub end)