fsm icon indicating copy to clipboard operation
fsm copied to clipboard

fsm.Event(ctx, ...) not work properly with context.WithTimeout()

Open qiulin opened this issue 2 years ago • 2 comments

When I use context.WithTimeout(ctx, time.Duration) with fsm.Event(ctx, ...) like:

Callbacks{
			"enter_end": func(ctx context.Context, e *Event) {
				go func() {
					<-ctx.Done()
					enterEndAsyncWorkDone = true
				}()
			},
		},

the internal block exec immediately, not after timeout.

qiulin avatar Aug 19 '22 09:08 qiulin

Interesting use case. I'll take a look at this. Are you constructing an FSM on the fly? A bit of pseudo code would help.

annismckenzie avatar Oct 06 '22 23:10 annismckenzie

This is possible. You'll need to make the callback async. The example in https://github.com/looplab/fsm/blob/main/examples/cancel_async_transition.go shows how to use this. That example is geared towards canceling the transition but you can just as easily block on your timeout context and then decide what to do (cancel the transition, trigger an event on the state machine).

annismckenzie avatar Oct 06 '22 23:10 annismckenzie