refactor(proxy): use asyncio.create_task in _iter_with_keepalive

asyncio.ensure_future is soft-deprecated since Python 3.10 for the
"schedule a coroutine on the running loop" use case. create_task is
the modern, idiomatic equivalent and is what every other call site in
proxy_server.py uses — the ensure_future call in _iter_with_keepalive
was the outlier. Functionally identical here since aiter.__anext__()
always returns a coroutine.

Test updated to spy on asyncio.create_task instead of ensure_future.
This commit is contained in:
Arun Mittal 2026-06-10 15:32:21 -04:00
parent f28c16ae58
commit 641faa9662
2 changed files with 5 additions and 5 deletions

View file

@ -7051,7 +7051,7 @@ async def _iter_with_keepalive(aiter, keepalive_seconds: float):
try:
while True:
if pending is None:
pending = asyncio.ensure_future(aiter.__anext__())
pending = asyncio.create_task(aiter.__anext__())
done, _ = await asyncio.wait({pending}, timeout=keepalive_seconds)
if not done:
yield _STREAM_KEEPALIVE

View file

@ -273,10 +273,10 @@ def test_iter_with_keepalive_cancels_pending_task_on_early_close():
# Track the Tasks ``_iter_with_keepalive`` creates so we can assert on
# cancellation after the wrapper is closed.
created_tasks = []
original_ensure_future = asyncio.ensure_future
original_create_task = asyncio.create_task
def _spy_ensure_future(coro_or_future, *args, **kwargs):
task = original_ensure_future(coro_or_future, *args, **kwargs)
def _spy_create_task(coro, *args, **kwargs):
task = original_create_task(coro, *args, **kwargs)
created_tasks.append(task)
return task
@ -287,7 +287,7 @@ def test_iter_with_keepalive_cancels_pending_task_on_early_close():
yield "unreachable" # pragma: no cover
async def _run_test():
with patch.object(asyncio, "ensure_future", _spy_ensure_future):
with patch.object(asyncio, "create_task", _spy_create_task):
wrapper = proxy_server_module._iter_with_keepalive(
_never_yields().__aiter__(), keepalive_seconds=0.05
)