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 d709f73 commit 7b7ae5c
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions src/asynkit/coroutine.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,12 @@ 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 @@ -123,9 +128,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 7b7ae5c

Please sign in to comment.