You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
SystemError: <method 'is_done' of '_thread._ThreadHandle' objects> returned a result with an exception set #125842
There is an unexpected SystemError which is completely outside of user code. I get it with Python 3.13.0 on Windows, but did never see it with Python 3.12.x:
Exception ignored on threading shutdown:
Traceback (most recent call last):
File "C:\Program Files\Python313\Lib\threading.py", line 1524, in _shutdown
if _main_thread._handle.is_done() and _is_main_interpreter():
SystemError: <method 'is_done' of '_thread._ThreadHandle' objects> returned a result with an exception set
Reproduction
The following minimal reproduction script:
importasyncioimportsysasyncdefrun():
p=awaitasyncio.create_subprocess_exec(*["ssh", "mek@192.168.37.128"])
try:
returnawaitp.wait() # leaving out "return" would helpfinally:
ifp.returncodeisNone: # leaving out this "if" statement or the complete "finally" block also would helpp.terminate()
sys.exit(asyncio.run(run()))
Run the it and leave the ssh session with sudo reboot now. You get the following output (note the last 5 lines):
mek@mek-ubuntu:~$ sudo reboot now
[sudo] password for mek:
Broadcast message from root@mek-ubuntu on pts/1 (Tue 2024-10-22 15:55:11 CEST):
The system will reboot now!
mek@mek-ubuntu:~$ Connection to 192.168.37.128 closed by remote host.
Connection to 192.168.37.128 closed.
Exception ignored on threading shutdown:
Traceback (most recent call last):
File "C:\Program Files\Python313\Lib\threading.py", line 1524, in _shutdown
if _main_thread._handle.is_done() and _is_main_interpreter():
SystemError: <method 'is_done' of '_thread._ThreadHandle' objects> returned a result with an exception set
There seems to be a strange interaction with async functions, local variables and return values:
leaving out the "return" keyword at return await p.wait() prevents the warning message
leaving out the "finally" block or just leave it empty with pass also omits the warning message
exiting the SSH session with exit also omits the warning message
EDIT : I think it's unlikely that this is a bug in _ThreadHandle.is_done. I don't see how it could set an exception. I think it's more likely that the problematic exception was set before the invocation of _ThreadHandle.is_done.
But since the C standard most probably defines int (at least Microsoft does), what to do?
Would it in this specific case be acceptable to reinterpret-cast the unsigned number, so that 0xffff_ffff renders as -1? The same thing seems to be done by the Windows command line interpreter (see my first link).
Yes, we should interpret 0xffff_ffff internally as -1 instead of failing with an overflow error. We will also need to fix a few places where _Py_HandleSystemExit can set an exception that isn't expected later on by _Py_Finalize.
Activity
colesbury commentedon Oct 22, 2024
cc @mpage.
EDIT : I think it's unlikely that this is a bug in
_ThreadHandle.is_done. I don't see how it could set an exception. I think it's more likely that the problematic exception was set before the invocation of_ThreadHandle.is_done.colesbury commentedon Oct 22, 2024
I can reproduce this on Windows with:
(Without the
import asyncio, the error message is "OverflowError: Python int too large to convert to C long")I think the problem is in
_Py_HandleSystemExit:PyLong_AsLongfor an error returnlongis a 32-bit signed integerGetExitCodeProcessreturns the exit code (via a pointer) as an unsigned 32-bit integer (DWORD).PyLong_AsLongfails due to overflowcpython/Python/pythonrun.c
Lines 603 to 605 in 91ddde4
kunom commentedon Oct 23, 2024
So the issue is that
sys.exit(0xffff_ffff)fails on Windows, even though Windows uses 32-bit unsigned integer exit codes.But since the C standard most probably defines
int(at least Microsoft does), what to do?Would it in this specific case be acceptable to reinterpret-cast the unsigned number, so that
0xffff_ffffrenders as -1? The same thing seems to be done by the Windows command line interpreter (see my first link).pythongh-125842: Fix `sys.exit(0xffff_ffff)` on Windows
colesbury commentedon Oct 23, 2024
Yes, we should interpret
0xffff_ffffinternally as-1instead of failing with an overflow error. We will also need to fix a few places where_Py_HandleSystemExitcan set an exception that isn't expected later on by_Py_Finalize.gh-125842: Fix `sys.exit(0xffff_ffff)` on Windows (#125896)
pythongh-125842: Fix `sys.exit(0xffff_ffff)` on Windows (pythonGH-125896
[3.13] gh-125842: Fix `sys.exit(0xffff_ffff)` on Windows (GH-125896) (G…
colesbury commentedon Oct 24, 2024
This is fixed now in the main and 3.13 branches, and it will be in the 3.13.1 release.
attheratearindam commentedon Dec 27, 2024
Guys Use Python version 3.12 its only issue with the latest version of python (3.13)
pythongh-125842: Fix `sys.exit(0xffff_ffff)` on Windows (python#125896)