cpython icon indicating copy to clipboard operation
cpython copied to clipboard

gh-140287: Handle `PYTHONSTARTUP` script exceptions in the asyncio REPL

Open johnslavik opened this issue 4 months ago • 10 comments

This is generally a work in progress; tests are needed. The asyncio patch is simple and ready.

  • Issue: gh-140287

johnslavik avatar Oct 18 '25 10:10 johnslavik

Please merge #140298 first.

johnslavik avatar Oct 23 '25 14:10 johnslavik

This is likely it. I can't think of anything simpler but equally complete at the same time. This suite seems intentionally straightforward, as are the new tests proposed here. I'll give this a few more days in a daemon thread of my mind.

We can abstract as needed in the future. We could use some regexes. However, I don't think this is so much needed now, and the environments differ enough to justify some repetition.

johnslavik avatar Nov 08 '25 00:11 johnslavik

It seems that the asyncio REPL on Windows leaks the event loop:

❯ PYTHONSTARTUP='' python -m asyncio
Running Debug|x64 interpreter...
asyncio REPL 3.15.0a1+ (heads/asyncio-repl-handle-python-startup-dirty:ce03ccef689, Nov  8 2025, 02:06:1) [MSC v.1944 64 bit (AMD64)] on win32
Use "await" directly instead of "asyncio.run()".
Type "help", "copyright", "credits" or "license" for more information.
>>> import asyncio
>>> exit
exiting asyncio REPL...
C:\Users\zenon\Python\cpython\Lib\asyncio\base_events.py:758: ResourceWarning: unclosed event loop <ProactorEventLoop running=False closed=False debug=False>

Definitely a bug, we'll track it elsewhere soon.

EDIT: Tracking in #141262

johnslavik avatar Nov 08 '25 01:11 johnslavik

This is ready, but Windows doesn't pass because of GH-141262.

johnslavik avatar Dec 05 '25 12:12 johnslavik

It seems that these unclosed loop warnings regressed (stopped appearing) in the recent releases -- this is bad, but we can fix them separately and we can also fix the asyncio REPL in this regard by actually closing the loop (in a separate PR).

In the meantime, updating this branch should make the CI green.

johnslavik avatar Dec 16 '25 03:12 johnslavik

Looks like I was confused about the regression -- that's good news!

This will have to wait until the loop is properly closed in the asyncio REPL.

johnslavik avatar Dec 16 '25 03:12 johnslavik

Cherry-picked 92f629b (875fd2a) from GH-142785 to see if it fixes the problem.

johnslavik avatar Dec 16 '25 03:12 johnslavik

to see if it fixes the problem.

It does! Cool!

johnslavik avatar Dec 16 '25 04:12 johnslavik

I'll see if I can simplify this. I think the tests are slightly overcomplicated.

johnslavik avatar Dec 21 '25 16:12 johnslavik

I've realized that this isn't testing what it is supposed to test. I'll come back to this later.

johnslavik avatar Jan 04 '26 16:01 johnslavik