Skip to content

anext(A(), default) crashes when StopAsyncIteration is raised by a synchronous __anext__ #131670

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
picnixz opened this issue Mar 24, 2025 · 5 comments
Assignees
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-crash A hard crash of the interpreter, possibly with a core dump

Comments

@picnixz
Copy link
Member

picnixz commented Mar 24, 2025

Crash report

What happened?

Python 3.14.0a6+ (heads/main:0e53038ea82, Mar 24 2025, 14:56:34) [GCC 7.5.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> class A:
...     def __anext__(self):
...         raise StopAsyncIteration
...
>>> anext(A(), "a")
Segmentation fault (core dumped)

There is no crash with anext(A()) or when directly calling __anext__(). Or when __anext__ is declared with async def, it's fine. Or when __anext__() doesn't raise, it's also fine.

NB: I haven't bisected the change but it might be related to what I wrote in 76ffaef.

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Output from running 'python -VV' on the command line:

No response

Linked PRs

@picnixz picnixz added interpreter-core (Objects, Python, Grammar, and Parser dirs) type-crash A hard crash of the interpreter, possibly with a core dump labels Mar 24, 2025
@picnixz
Copy link
Member Author

picnixz commented Mar 24, 2025

cc @kumaraditya303

@sobolevn
Copy link
Member

sobolevn commented Mar 24, 2025

This happens because awaitable here is NULL:

cpython/Python/bltinmodule.c

Lines 1839 to 1842 in d16f455

awaitable = (*t->tp_as_async->am_anext)(aiterator);
if (default_value == NULL) {
return awaitable;
}

Looks like simple if (awaitable == NULL) check might solve the issue:

» PYTHONFAULTHANDLER=1 ./python.exe ex.py
Traceback (most recent call last):
  File "/Users/sobolev/Desktop/cpython/ex.py", line 5, in <module>
    print(anext(A(), "a"))
          ~~~~~^^^^^^^^^^
  File "/Users/sobolev/Desktop/cpython/ex.py", line 3, in __anext__
    raise StopAsyncIteration
StopAsyncIteration

@sobolevn sobolevn self-assigned this Mar 24, 2025
@sobolevn
Copy link
Member

And not just StopAsyncIteration, but any exception really.

@picnixz
Copy link
Member Author

picnixz commented Mar 24, 2025

Yes, that's what I observed (note that I actually found this bug because it was a typo I made when creating tests for another PR...)

sobolevn added a commit to sobolevn/cpython that referenced this issue Mar 24, 2025
sobolevn added a commit that referenced this issue Mar 24, 2025
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Mar 24, 2025
…raises (pythonGH-131682)

(cherry picked from commit 929afd1)

Co-authored-by: sobolevn <[email protected]>
Co-authored-by: Bénédikt Tran <[email protected]>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Mar 24, 2025
…raises (pythonGH-131682)

(cherry picked from commit 929afd1)

Co-authored-by: sobolevn <[email protected]>
Co-authored-by: Bénédikt Tran <[email protected]>
sobolevn added a commit that referenced this issue Mar 24, 2025
… raises (GH-131682) (#131687)

gh-131670: Fix crash in `anext()` when `__anext__` is sync and raises (GH-131682)
(cherry picked from commit 929afd1)

Co-authored-by: sobolevn <[email protected]>
Co-authored-by: Bénédikt Tran <[email protected]>
@picnixz picnixz closed this as completed Mar 24, 2025
@picnixz
Copy link
Member Author

picnixz commented Mar 24, 2025

Thanks for the quick fix Nikita!

sobolevn added a commit that referenced this issue Mar 24, 2025
… raises (GH-131682) (#131686)

gh-131670: Fix crash in `anext()` when `__anext__` is sync and raises (GH-131682)
(cherry picked from commit 929afd1)

Co-authored-by: sobolevn <[email protected]>
Co-authored-by: Bénédikt Tran <[email protected]>
diegorusso pushed a commit to diegorusso/cpython that referenced this issue Apr 1, 2025
seehwan pushed a commit to seehwan/cpython that referenced this issue Apr 16, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) type-crash A hard crash of the interpreter, possibly with a core dump
Projects
None yet
Development

No branches or pull requests

2 participants