Skip to content

Commit

Permalink
Fix task_status future to prevent erroneous RuntimeError (#707)
Browse files Browse the repository at this point in the history
Fixes #706.
  • Loading branch information
dominik-schwabe committed Apr 5, 2024
1 parent 30c0e68 commit e024e92
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
4 changes: 4 additions & 0 deletions docs/versionhistory.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ This library adheres to `Semantic Versioning 2.0 <http://semver.org/>`_.

**UNRELEASED**

- Fixed erroneous ``RuntimeError: called 'started' twice on the same task status``
when cancelling a task in a TaskGroup created with the ``start()`` method before
the first checkpoint is reached after calling ``task_status.started()``
(`#706 <https://github.com/agronholm/anyio/issues/706>`_; PR by Dominik Schwabe)
- Fixed erroneous ``TypedAttributeLookupError`` if a typed attribute getter raises
``KeyError``
- Fixed the asyncio backend not respecting the ``PYTHONASYNCIODEBUG`` environment
Expand Down
7 changes: 4 additions & 3 deletions src/anyio/_backends/_asyncio.py
Original file line number Diff line number Diff line change
Expand Up @@ -620,9 +620,10 @@ def started(self, value: T_contra | None = None) -> None:
try:
self._future.set_result(value)
except asyncio.InvalidStateError:
raise RuntimeError(
"called 'started' twice on the same task status"
) from None
if not self._future.cancelled():
raise RuntimeError(
"called 'started' twice on the same task status"
) from None

task = cast(asyncio.Task, current_task())
_task_states[task].parent_id = self._parent_id
Expand Down
11 changes: 11 additions & 0 deletions tests/test_taskgroups.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,17 @@ async def taskfunc(*, task_status: TaskStatus) -> None:
assert value is None


async def test_no_called_started_twice() -> None:
async def taskfunc(*, task_status: TaskStatus) -> None:
task_status.started()

# anyio>4.3.0 should not raise "RuntimeError: called 'started' twice on the same task status"
async with create_task_group() as tg:
coro = tg.start(taskfunc)
tg.cancel_scope.cancel()
await coro


async def test_start_with_value() -> None:
async def taskfunc(*, task_status: TaskStatus) -> None:
task_status.started("foo")
Expand Down

0 comments on commit e024e92

Please sign in to comment.