python / cpython

The Python programming language
https://www.python.org
Other
63.85k stars 30.56k forks source link

concurrent.future.thread._python_exit: continuing blocking while exceptions occureded on windows. #127282

Open GoodenoughPhysicsLab opened 6 days ago

GoodenoughPhysicsLab commented 6 days ago

Bug report

Bug description:

on windows, Thread.join is sometimes terrible because exception can not interrupt its blocking.

import threading
import time

def task():
    while True:
        time.sleep(1)

t = threading.Thread(target=task)
t.start()
t.join()

you can't exit this program by Ctrl+C on windows.

we can easily fix it to let exceptions been raisen.


import threading
import time

def task():
    while True:
        time.sleep(1)

t = threading.Thread(target=task)
t.start()
while t.is_alive():
    t.join(timeout=2)

this is what I want to fix in _python_exit, which been registered in atexit-call and it costs me a lot of time to realize what's problem with my program.

I'll send pr soon.

CPython versions tested on:

3.9, 3.10, 3.11, 3.12

Operating systems tested on:

Windows

Linked PRs

ZeroIntensity commented 5 days ago

cc @zooba as the Windows expert. The reproducer can be killed by just CTRL+C-ing twice on Linux.

zooba commented 4 days ago

I would prefer to make PyThread_join_thread aware of _PyOS_SigintEvent so that it can abort its wait.

zooba commented 4 days ago

That said, I am certain we have discussed this before, and presumably it's not so easy. I don't remember exactly why, but there'll be an issue somewhere with details.

GoodenoughPhysicsLab commented 4 days ago

That said, I am certain we have discussed this before, and presumably it's not so easy. I don't remember exactly why, but there'll be an issue somewhere with details.

No matter how difficult it is, I want to have a try.