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

CI: test_on_completed_callback_refcount failed on Python 3.12 #46452

Closed
hongchaodeng opened this issue Jul 5, 2024 · 1 comment · Fixed by #46488
Closed

CI: test_on_completed_callback_refcount failed on Python 3.12 #46452

hongchaodeng opened this issue Jul 5, 2024 · 1 comment · Fixed by #46488
Assignees
Labels
bug Something that is supposed to be working; but isn't core Issues that should be addressed in Ray Core P0 Issues that should be fixed in short order

Comments

@hongchaodeng
Copy link
Member

What happened + What you expected to happen

test_on_completed_callback_refcount failed on Python 3.12

Versions / Dependencies

Python 3.12

Reproduction script

python -m pytest -vvs python/ray/tests/test_asyncio.py -k "test_on_completed_callback_refcount"

Issue Severity

None

@hongchaodeng hongchaodeng added bug Something that is supposed to be working; but isn't triage Needs triage (eg: priority, bug/not-bug, and owning component) core Issues that should be addressed in Ray Core labels Jul 5, 2024
@hongchaodeng hongchaodeng self-assigned this Jul 5, 2024
@hongchaodeng hongchaodeng added P0 Issues that should be fixed in short order and removed triage Needs triage (eg: priority, bug/not-bug, and owning component) labels Jul 6, 2024
@hongchaodeng
Copy link
Member Author

hongchaodeng commented Jul 8, 2024

I can reproduce this using only python 3.12 + cython:

# main.py

import asyncio
import sys

from my_module.func import SignalActor

async def main():
    signal = SignalActor()

    def callback():
        raise ValueError("error")

    refcount = sys.getrefcount(callback)
    print(f"{refcount=}")

    fut = asyncio.create_task(signal.wait(callback))

    refcount2 = sys.getrefcount(callback)
    print(f"{refcount2=}")
    assert refcount2 > refcount

    try:
        signal.send(callback)
    except Exception:
        print("send exception")
    finally:
        print("finally 2")

    try:
        await fut
    except Exception:
        print("wait exception")
    finally:
        print("finally 3")


    while True:
        refcount3 = sys.getrefcount(callback)
        print(f"{refcount3=}")
        if refcount3 == refcount:
            print("success")
            return
        else:
            print("sleep")
            await asyncio.sleep(1)


asyncio.run(main())

# my_module/func.pyx 

import asyncio

cimport cpython

class SignalActor:
    def __init__(self):
        self.ready_event = asyncio.Event()

    def send(self, callback):
        self.ready_event.set()

    async def wait(self, callback):
        cpython.Py_INCREF(callback)
        await self.ready_event.wait()
        try:
            callback()
        # except Exception:
        #     print("unblocking this comment will make it work")
        finally:
            print("finally")
            cpython.Py_DECREF(callback)

# setup.py

from setuptools import setup, Extension
from Cython.Build import cythonize

extensions = [
    Extension(
        name="my_module.func",
        sources=["my_module/func.pyx"],
    )
]

setup(
    name="cython",
    ext_modules=cythonize(extensions),
)

Then run:

python setup.py build_ext --inplace
python main.py

This doesn't work on Python 3.12 but works on prior versions. Seems like a regression in Python/Cython.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something that is supposed to be working; but isn't core Issues that should be addressed in Ray Core P0 Issues that should be fixed in short order
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant