Skip to content
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

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

Closed
gottadiveintopython opened this issue Oct 19, 2023 · 5 comments · Fixed by #112428
Closed
Labels
3.12 bugs and security fixes 3.13 bugs and security fixes type-bug An unexpected behavior, bug, or error

Comments

@gottadiveintopython
Copy link

gottadiveintopython commented Oct 19, 2023

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

@gottadiveintopython gottadiveintopython added the type-bug An unexpected behavior, bug, or error label Oct 19, 2023
@terryjreedy
Copy link
Member

Same change on Windows. 3.13 like 3.12.

@Eclips4 Eclips4 added 3.13 bugs and security fixes 3.12 bugs and security fixes labels Oct 19, 2023
@Eclips4
Copy link
Member

Eclips4 commented Oct 19, 2023

Bisected to f02fa64
cc @markshannon

@markshannon markshannon changed the title Python 3.12 cannot close a just created coroutine Python 3.12 inspecting a coroutine using getcoroutinestate returns the wrong state. Oct 31, 2023
@markshannon
Copy link
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.

@iritkatriel
Copy link
Member

iritkatriel commented Nov 26, 2023

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.

@Eclips4
Copy link
Member

Eclips4 commented Dec 1, 2023

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

iritkatriel pushed a commit that referenced this issue Dec 1, 2023
aisk pushed a commit to aisk/cpython that referenced this issue Feb 11, 2024
Glyphack pushed a commit to Glyphack/cpython that referenced this issue Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 bugs and security fixes 3.13 bugs and security fixes type-bug An unexpected behavior, bug, or error
Projects
None yet
5 participants