pytransitions / transitions

A lightweight, object-oriented finite state machine implementation in Python with many extensions
MIT License
5.49k stars 525 forks source link

State transition during on_exception callback causes RecursionError #616

Closed e0lithic closed 1 year ago

e0lithic commented 1 year ago

Describe the bug This issue is in continuance with the following discussion https://github.com/pytransitions/transitions/issues/485#issuecomment-1493360030. where trying to make a state transition during on_exception causes a RecursionError

The modifications made from the example cited by @aleneum in the example are as follows :

  1. The exception is getting raised in the on_exit callback.
  2. Ordered transactions are being used.

Minimal working example

from transitions import Machine

class Model:

    def handle_error(self):
        self.to_failure()

    def on_exit_failing(self):
        raise RuntimeError("Failing!")

    def __init__(self):
        self.machine = Machine(
            model=self, 
            states=[
                'initial', 
                {'name' : 'start', 'on_exit':'on_exit_failing'}, 
                'stop' 
                ], initial='initial',
            on_exception='handle_error')
        self.machine.add_ordered_transitions(loop=False)
        self.machine.add_state("failure")

model = Model()        
assert model.is_initial()
model.next_state()
assert model.is_start()
# The next command will cause RecursionError
model.next_state()
assert model.is_failure()

Expected behavior Post exception the model should have transitioned to the failure state.