python-trio / python-trio/trio
cython tasks are KI protected
Open
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 7.3k
- Forks
- 431
- Avg merge
- 2d 17h
- Merged PRs (30d)
- 6
Description
if you write a python function:
# tests/cython/pyfunc.py
import threading
def sleep() -> None:
threading.Event().wait()
and call it from a cython task:
# cython: language_level=3
import trio
from .pyfunc import sleep
# the output of the prints are not currently checked, we only check
# if the program can be compiled and doesn't crash when run.
# The content of the program can easily be extended if there's other behaviour
# that might be likely to be problematic for cython.
async def foo() -> None:
print('.')
async def bad():
sleep()
async def trio_main() -> None:
print('hello...')
await trio.sleep(1)
print(' world !')
async with trio.open_nursery() as nursery:
nursery.start_soon(foo)
nursery.start_soon(foo)
nursery.start_soon(foo)
await bad()
trio.run(trio_main)
it hangs forever and can't be interrupted with ctrl+c
currently_ki_protected sees the following stack:
frame=<frame at 0x71d9198c7920, file '/usr/lib/python3.12/threading.py', line 355, code wait>
frame=<frame at 0x71d9198edd80, file '/usr/lib/python3.12/threading.py', line 655, code wait>
frame=<frame at 0x71d91992ed40, file '/home/graingert/projects/trio/tests/cython/pyfunc.py', line 4, code sleep>
frame=<frame at 0x71d91a083040, file '/home/graingert/projects/trio/src/trio/_core/_run.py', line 2720, code unrolled_run> (protected=True)
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start with the Cython reproducer in tests/cython/pyfunc.py and trace the protected frame handling in src/trio/_core/_run.py, especially unrolled_run. Reproduce the hang and inspect how the threading wait is classified by currently_ki_protected. Done means the Cython task can be interrupted without hanging, with regression coverage for the reported behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100