transitions icon indicating copy to clipboard operation
transitions copied to clipboard

Async TimeoutError gets caught inappropriately

Open Salier13 opened this issue 6 months ago • 0 comments

When using the AsyncMachine, any asyncio.TimeoutError that gets raised during a manipulation of the state machine gets eaten by it and process outside of state machine keep going as if nothing happened.

import asyncio
from transitions.extensions.asyncio import AsyncMachine

class Model:
    async def on_enter_on(self):
        await asyncio.sleep(1)
        assert False, "Shouldn't be reachable due to timeout"

machine = AsyncMachine(
    Model(),
    states=["off", "on"],
    transitions=[{"trigger": "start", "source": "off", "dest": "on"}],
    initial="off"
)

async def foo():
    await machine.model.start()
    assert False, "Shouldn't be reachable due to timeout" # <-- This assert trigger

async def main():
    try:
        await asyncio.wait_for(foo(), timeout=0.5)
    except asyncio.TimeoutError:
        return         # expected case
    assert False, "Expected a TimeoutError"

asyncio.run(main())

I expect the Timeout exception to be raised if no particular comportment for exceptions is indicated. Particularly in the case where the timeout extension is not used or is not specified for the current transition.

I did not test this with https://github.com/pytransitions/transitions/pull/686 so unsure if it is fixed with that timeout rework. But if it is could a release be planned soon?

Salier13 avatar Jun 13 '25 09:06 Salier13