From 88614c9f3fa0424aaf5e35ade1606fe1bbe002fb Mon Sep 17 00:00:00 2001 From: Jukka Lehtosalo Date: Wed, 14 Oct 2026 18:06:23 +0100 Subject: [PATCH] [mypyc] Fix over-decref in async for over a non-async iterable CPy_GetAIter called Py_DECREF on its argument when the object has no `__aiter__`. The code was adapted from ceval.c, where GET_AITER owns the stack reference, but the primitive receives a borrowed reference, so each failing 'async for' freed the iterable one reference too early and could later segfault. --- mypyc/lib-rt/misc_ops.c | 1 - mypyc/test-data/run-async.test | 26 +++++++++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/mypyc/lib-rt/misc_ops.c b/mypyc/lib-rt/misc_ops.c index e5f51b4e3c444..870cd4682ea77 100644 --- a/mypyc/lib-rt/misc_ops.c +++ b/mypyc/lib-rt/misc_ops.c @@ -1032,7 +1032,6 @@ PyObject *CPy_GetAIter(PyObject *obj) "'async for' requires an object with " "__aiter__ method, got %.100s", type->tp_name); - Py_DECREF(obj); return NULL; } diff --git a/mypyc/test-data/run-async.test b/mypyc/test-data/run-async.test index ba45eda60dcbb..f0170eddcb1ac 100644 --- a/mypyc/test-data/run-async.test +++ b/mypyc/test-data/run-async.test @@ -485,8 +485,10 @@ async def sleep(t: float) -> None: ... import asyncio import gc import platform +import sys +from typing import Any, cast -from testutil import is_gil_disabled +from testutil import assertRaises, is_gil_disabled def immediate_deallocation_checks_are_reliable() -> bool: # Free-threaded builds can defer deallocation, so weakref liveness is not deterministic. @@ -697,6 +699,28 @@ async def test_completed_mixed_coroutine_releases_environment() -> None: assert await coro == 1 assert ref() is None +async def iterate(x: Any) -> int: + n = 0 + async for y in x: + n += 1 + return n + +async def comprehension(x: Any) -> list[Any]: + return [y async for y in x] + +def test_async_for_over_sync_iterator() -> None: + getrefcount = getattr(sys, "getrefcount") + it = iter([1, 2]) + rc = getrefcount(it) + msg = "'async for' requires an object with __aiter__ method, got list_iterator" + for i in range(10): + with assertRaises(TypeError, msg): + cast(Any, iterate(it)).send(None) + with assertRaises(TypeError, msg): + cast(Any, comprehension(it)).send(None) + assert getrefcount(it) == rc + assert list(it) == [1, 2] + [file asyncio/__init__.pyi] async def sleep(t: float) -> None: ...