Skip to content

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

Closed

Description

@picnixz

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

Activity

  1. added
    interpreter-core(Objects, Python, Grammar, and Parser dirs)
    type-crashA hard crash of the interpreter, possibly with a core dump
    on Mar 24, 2025
  2. picnixz commented on Mar 24, 2025

    @picnixz
    MemberAuthor
  3. sobolevn commented on Mar 24, 2025

    @sobolevn
    Member

    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
  4. self-assigned this
    on Mar 24, 2025
  5. sobolevn commented on Mar 24, 2025

    @sobolevn
    Member

    And not just StopAsyncIteration, but any exception really.

  6. picnixz commented on Mar 24, 2025

    @picnixz
    MemberAuthor

    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...)

  7. added 2 commits that reference this issue on Mar 24, 2025
  8. added 2 commits that reference this issue on Mar 24, 2025
  9. added a commit that references this issue on Mar 24, 2025
  10. picnixz commented on Mar 24, 2025

    @picnixz
    MemberAuthor

    Thanks for the quick fix Nikita!

  11. added a commit that references this issue on Mar 24, 2025
  12. added a commit that references this issue on Apr 1, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

interpreter-core(Objects, Python, Grammar, and Parser dirs)type-crashA hard crash of the interpreter, possibly with a core dump

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions