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

_thread.get_ident returns a result with an exception set #128189

Open
AraHaan opened this issue Dec 22, 2024 · 4 comments
Open

_thread.get_ident returns a result with an exception set #128189

AraHaan opened this issue Dec 22, 2024 · 4 comments
Labels
extension-modules C modules in the Modules dir pending The issue will be closed if no feedback is provided topic-C-API type-bug An unexpected behavior, bug, or error

Comments

@AraHaan
Copy link
Contributor

AraHaan commented Dec 22, 2024

Bug report

Bug description:

I am working on a python program that is async in nature and is carefully created using the C API as a C extension by using a generic awaitable object, Sadly, I had to implement what asyncio.run does manually due to it not liking how the awaitable object is designed because the only way to get the awaitable object the C api function returns is that it must first run the function in order for asyncio.run to then await the awaitable. However the 3.14 change to throw RuntimeError when get_event_loop() is called prior to asyncio.run actually creating it became a problem that I needed to solve with manually doing:

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)
loop.run_until_complete(awaitable)
loop.close()

from the C code. Everything works well, Except when I Ctrl+C the program to terminate it and loop.close() returns NULL, I then PyErr_Print()'d it and see that:

Traceback (most recent call last):
  File "asyncio\base_events.py", line 707, in run_until_complete
  File "asyncio\base_events.py", line 678, in run_forever
  File "asyncio\base_events.py", line 1995, in _run_once
  File "asyncio\windows_events.py", line 446, in select
  File "asyncio\windows_events.py", line 775, in _poll
KeyboardInterrupt

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "asyncio\proactor_events.py", line 688, in close
  File "threading.py", line 1435, in current_thread
SystemError: <built-in function get_ident> returned a result with an exception set
RuntimeError: Result of closing the event loop was 'NULL'.

And I seen the line and it looks that threading.py set's it's get_ident() to _thread.get_ident() and then uses it on the line the traceback mentions. The issue is, it's hard to debug why the _thread.get_ident() function did this.

See also: ZeroIntensity/pyawaitable#36

CPython versions tested on:

3.14, CPython main branch

Operating systems tested on:

Windows

@AraHaan AraHaan added the type-bug An unexpected behavior, bug, or error label Dec 22, 2024
@picnixz picnixz added interpreter-core (Objects, Python, Grammar, and Parser dirs) extension-modules C modules in the Modules dir and removed interpreter-core (Objects, Python, Grammar, and Parser dirs) labels Dec 22, 2024
@picnixz
Copy link
Contributor

picnixz commented Dec 22, 2024

cc @ZeroIntensity (since you're active and I'm not)

@ZeroIntensity
Copy link
Member

Will investigate a bit later.

@ZeroIntensity
Copy link
Member

I haven't look at it too hard, but this is probably just something that happens as a result of how you're dealing with CTRL+C. PyThreadHandleObject_get_ident is probably being called with a KeyboardInterrupt set. Are you missing a NULL check somewhere?

@AraHaan
Copy link
Contributor Author

AraHaan commented Dec 23, 2024

It could be because I don't call PyErr_Clear() after I check the errors from run_until_complete (which is obtained from PyErr_Occurred()). This is within the if error not NULL and then I do some error checks to filter out specific errors. That might be the problem.

@ZeroIntensity ZeroIntensity added the pending The issue will be closed if no feedback is provided label Dec 23, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
extension-modules C modules in the Modules dir pending The issue will be closed if no feedback is provided topic-C-API type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

3 participants