Skip to content

Python 3.12 inspecting a coroutine using getcoroutinestate returns the wrong state. #111058

Closed

Description

@gottadiveintopython

Bug report

Bug description:

Prior to Python 3.12, we can close a just created coroutine, thus, the following test passes:

from inspect import getcoroutinestate, CORO_CLOSED


async def do_nothing():
    pass


def test_immediate_close():
    coro = do_nothing()
    coro.close()
    assert getcoroutinestate(coro) == CORO_CLOSED

But in Python 3.12, the assertion fails and getcoroutinestate(coro) still is CORO_CREATED after the coro.close(). You can confirm it from https://github.com/gottadiveintopython/py312/actions/runs/6571303025/job/17850194150.

I don't know if this is an intensional change or not, but I just wanted to tell you.

CPython versions tested on:

3.12

Operating systems tested on:

Linux

Linked PRs

Activity

  1. terryjreedy commented on Oct 19, 2023

    @terryjreedy
    Member

    Same change on Windows. 3.13 like 3.12.

  2. added
    3.13only security fixes
    3.12only security fixes
    on Oct 19, 2023
  3. Eclips4 commented on Oct 19, 2023

    @Eclips4
    Member

    Bisected to f02fa64
    cc @markshannon

  4. changed the title [-]Python 3.12 cannot close a just created coroutine[/-] [+]Python 3.12 inspecting a coroutine using `getcoroutinestate` returns the wrong state.[/+] on Oct 31, 2023
  5. markshannon commented on Oct 31, 2023

    @markshannon
    Member

    The coroutine is closed, as coro.send(None) fails. It is just that getcoroutinestate returns the wrong value.
    I've updated the title accordingly.

  6. iritkatriel commented on Nov 26, 2023

    @iritkatriel
    Member

    getcoroutinestate does this:

        if coroutine.cr_running:
            return CORO_RUNNING
        if coroutine.cr_suspended:
            return CORO_SUSPENDED
        if coroutine.cr_frame is None:
            return CORO_CLOSED
        return CORO_CREATED
    

    coroutine.cr_frame returns None for frames which have state FRAME_CLEARED, but not for FRAME_COMPLETED.

  7. added 2 commits that reference this issue on Nov 26, 2023
  8. added a commit that references this issue on Dec 1, 2023
  9. added a commit that references this issue on Dec 1, 2023
  10. Eclips4 commented on Dec 1, 2023

    @Eclips4
    Member

    Thanks @gottadiveintopython for the report and Irit for fixing it!

  11. added a commit that references this issue on Dec 1, 2023
  12. added a commit that references this issue on Feb 11, 2024
  13. added a commit that references this issue on Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.12only security fixes3.13only security fixestype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions