Skip to content

Commit

Permalink
fix: set and reset contextvar.current_context in _processing_timeout
Browse files Browse the repository at this point in the history
context parameter not available in create_task prior to Python 3.11
  • Loading branch information
aleneum committed Aug 20, 2024
1 parent 23fed9f commit 5f77cc2
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions transitions/extensions/asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -686,13 +686,13 @@ async def _timeout():
await asyncio.shield(self._process_timeout(event_data))
except asyncio.CancelledError:
pass

return asyncio.create_task(_timeout(), context=contextvars.Context())
return asyncio.create_task(_timeout())

async def _process_timeout(self, event_data):
_LOGGER.debug("%sTimeout state %s. Processing callbacks...", event_data.machine.name, self.name)
event_data = AsyncEventData(event_data.state, AsyncEvent("timeout", event_data.machine),
event_data.machine, event_data.model, args=tuple(), kwargs={})
token = AsyncMachine.current_context.set(None)
try:
await event_data.machine.callbacks(self.on_timeout, event_data)
except BaseException as err:
Expand All @@ -708,6 +708,8 @@ async def _process_timeout(self, event_data):
_LOGGER.error("%sHandling timeout exception '%s' caused another exception: %s. "
"Cancel running transitions...", event_data.machine.name, repr(err), repr(err2))
await event_data.machine.cancel_running_transitions(event_data.model, "timeout")
finally:
AsyncMachine.current_context.reset(token)
_LOGGER.info("%sTimeout state %s processed.", event_data.machine.name, self.name)

@property
Expand Down

0 comments on commit 5f77cc2

Please sign in to comment.