Routine stop and reset: Race condition?

Hi list,

I hope this hasn’t been discussed to death yet. But…

I have a the following Routine:
~kik = Routine({ inf.do{“bang”.postln; 0.5.wait}; })

which I can .play .stop and .reset.

When I want to .stop and .reset in a single line such as
~kik.stop; ~kik.reset
will neither stop nor reset it.

Calling only .stop and later
~kik.reset; ~kik.play
works.

Is that some kind of a race condition preventing it from working the way
I imagine?

Is there even a better way to stop and reset a Routine using a single
method, or to reset and start?

Start this:

(
~kik = Routine({
	var count = 0;
	inf.do {
		count = count + 1;
		count.postln;
		0.5.wait;
	};
}).play;
)

And try to stop and reset immediately:
~kik.stop; ~kik.reset;

Counter was set back to 0.

I think that when you do this, the wait command is still ‘in the air’, even if stopped, and when it yields next, since the Routine was stopped but reset, it starts again.

See what happens using those:
~kik.stop; { ~kik.reset }.defer(0.4);
~kik.stop; { ~kik.reset }.defer(0.6);

First behaves ‘incorrectly’, second is working. This is because the second resets after the next yield. So this is a solution.

IMO, you should instead create a function that stops current routine and create a brand new one every time you need it.