Skip to content

Commit c857358

Browse files
committed
Fix issue in async generator wrapper
1 parent 5693dd2 commit c857358

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

newrelic/common/async_wrapper.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ async def wrapper(*args, **kwargs):
9393
with trace:
9494
while True:
9595
try:
96-
g.asend(value).send(None)
96+
yielded = await g.asend(value)
9797
except StopAsyncIteration as e:
9898
# The underlying async generator has finished, return propagates a new StopAsyncIteration
9999
return
@@ -107,7 +107,7 @@ async def wrapper(*args, **kwargs):
107107
# An exception was thrown with .athrow(), propagate to the original async generator.
108108
# Return value logic must be identical to .asend()
109109
try:
110-
g.athrow(type(e), e).send(None)
110+
value = yield await g.athrow(type(e), e)
111111
except StopAsyncIteration as e:
112112
# The underlying async generator has finished, return propagates a new StopAsyncIteration
113113
return

tests/agent_features/_test_async_generator_trace.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,37 @@ async def _test():
260260
assert full_metrics[key].total_exclusive_call_time < 0.2
261261

262262

263+
@validate_transaction_metrics(
264+
"test_asend_receives_a_value",
265+
background_task=True,
266+
scoped_metrics=[("Function/agen", 1)],
267+
rollup_metrics=[("Function/agen", 1)],
268+
)
269+
def test_asend_receives_a_value(event_loop):
270+
_received = []
271+
@function_trace(name="agen")
272+
async def agen():
273+
value = yield
274+
_received.append(value)
275+
yield value
276+
277+
@background_task(name="test_asend_receives_a_value")
278+
async def _test():
279+
gen = agen()
280+
281+
# kickstart the coroutine
282+
await anext(gen)
283+
284+
assert await gen.asend("foobar") == "foobar"
285+
assert _received and _received[0] == "foobar"
286+
287+
# finish consumption of the coroutine if necessary
288+
async for _ in gen:
289+
pass
290+
291+
event_loop.run_until_complete(_test())
292+
293+
263294
@validate_transaction_metrics(
264295
"test_athrow_yields_a_value",
265296
background_task=True,

0 commit comments

Comments
 (0)