[Bugfix][Core] Close underlying iterator in merge_async_iterators single-iterator fast path (#44726)
Signed-off-by: Ting Sun <suntcrick@gmail.com> Signed-off-by: Nick Hill <nickhill123@gmail.com> Co-authored-by: Nick Hill <nickhill123@gmail.com>
This commit is contained in:
@@ -40,3 +40,25 @@ async def test_merge_async_iterators():
|
||||
print("Iterator was cancelled normally")
|
||||
except (Exception, asyncio.CancelledError) as e:
|
||||
raise AssertionError() from e
|
||||
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_merge_async_iterators_single_closes_underlying():
|
||||
# The single-iterator fast path must close the underlying generator when
|
||||
# the merged generator is closed, matching the multi-iterator path. On the
|
||||
# buggy fast path the underlying generator is left running.
|
||||
closed = False
|
||||
|
||||
async def gen():
|
||||
nonlocal closed
|
||||
try:
|
||||
while True:
|
||||
yield "x"
|
||||
await asyncio.sleep(0.01)
|
||||
finally:
|
||||
closed = True
|
||||
|
||||
merged = merge_async_iterators(gen())
|
||||
assert await anext(merged) == (0, "x")
|
||||
await merged.aclose()
|
||||
assert closed
|
||||
|
||||
@@ -103,8 +103,15 @@ async def merge_async_iterators(
|
||||
"""
|
||||
if len(iterators) == 1:
|
||||
# Fast-path single iterator case.
|
||||
async for item in iterators[0]:
|
||||
yield 0, item
|
||||
iterator: AsyncGenerator[T, None] | None = iterators[0]
|
||||
try:
|
||||
async for item in iterator: # type: ignore[union-attr]
|
||||
yield 0, item
|
||||
iterator = None
|
||||
finally:
|
||||
if iterator is not None:
|
||||
with contextlib.suppress(BaseException):
|
||||
await iterator.aclose()
|
||||
return
|
||||
|
||||
loop = asyncio.get_running_loop()
|
||||
|
||||
Reference in New Issue
Block a user