diff --git a/codex/utils/asyncstatemachine.nim b/codex/utils/asyncstatemachine.nim index cc2a0341..9df86294 100644 --- a/codex/utils/asyncstatemachine.nim +++ b/codex/utils/asyncstatemachine.nim @@ -25,8 +25,11 @@ template makeStateMachine*(MachineType, StateType) = discard proc run(machine: MachineType, state: StateType) {.async.} = - if next =? await state.run(): - machine.schedule(Event.transition(state, next)) + try: + if next =? await state.run(): + machine.schedule(Event.transition(state, next)) + except CancelledError: + discard proc scheduler(machine: MachineType) {.async.} = try: @@ -37,6 +40,7 @@ template makeStateMachine*(MachineType, StateType) = await machine.running.cancelAndWait() machine.state = next machine.running = machine.run(machine.state) + asyncSpawn machine.running except CancelledError: discard diff --git a/tests/codex/utils/testasyncstatemachine.nim b/tests/codex/utils/testasyncstatemachine.nim index 56dbd9a0..89b5943a 100644 --- a/tests/codex/utils/testasyncstatemachine.nim +++ b/tests/codex/utils/testasyncstatemachine.nim @@ -27,6 +27,7 @@ method run(state: State2): Future[?State] {.async.} = await sleepAsync(1.hours) except CancelledError: inc cancellations[1] + raise method onMoveToNextStateEvent(state: State2): ?State = some State(State3.new())