Skip to content

Commit

Permalink
Fix the suspension state checker of async generators
Browse files Browse the repository at this point in the history
  • Loading branch information
kristjanvalur committed Sep 3, 2023
1 parent 9e3528f commit 65c38f9
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/asynkit/coroutine.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,10 @@ def coro_is_new(coro: Suspendable) -> bool:
elif inspect.isgenerator(coro):
return inspect.getgeneratorstate(coro) == inspect.GEN_CREATED
elif inspect.isasyncgen(coro):
return coro.ag_frame is not None and not coro.ag_running
# async generators have an ag_await if they are suspended
# ag_running() means that it is inside an anext() or athrow()
# but it may be suspended.
return coro.ag_frame is not None and coro.ag_await is None and not coro.ag_running
else:
raise TypeError(
f"a coroutine or coroutine like object is required. Got: {type(coro)}"
Expand All @@ -124,9 +127,7 @@ def coro_is_suspended(coro: Suspendable) -> bool:
elif inspect.isgenerator(coro):
return inspect.getgeneratorstate(coro) == inspect.GEN_SUSPENDED
elif inspect.isasyncgen(coro):
# This is true only if we are inside an anext() or athrow(), not if the
# inner coroutine is itself doing an await before yielding a value.
return coro.ag_running
return coro.ag_await is not None
else:
raise TypeError(
f"a coroutine or coroutine like object is required. Got: {type(coro)}"
Expand Down

0 comments on commit 65c38f9

Please sign in to comment.