X Tutup
The Wayback Machine - https://web.archive.org/web/20240110155241/https://github.com/python/cpython/issues/113130
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

Ctrl-C Does Not Do Anything While A Subinterpreter Is Running in the Main Thread #113130

Open
ericsnowcurrently opened this issue Dec 14, 2023 · 0 comments
Labels
3.12 bugs and security fixes 3.13 new features, bugs and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-subinterpreters type-bug An unexpected behavior, bug, or error

Comments

@ericsnowcurrently
Copy link
Member

ericsnowcurrently commented Dec 14, 2023

Bug report

$ cat > /tmp/bug-interp-not-interrupted.py << EOF
import threading
import time
from test.support import interpreters

done = False

# A subthread running the main interpreter does not get interrupted.
def task():
    while not done:
        time.sleep(0.1)
t = threading.Thread(target=task)
t.start()

interp = interpreters.create()

start = time.time()
try:
    interp.exec_sync("""if True:
        import time
        print('Hit Ctrl-C now!')
        for i in range(1, 6):
            time.sleep(1)
            print(i)
    """)
    # Ctrl-C isn't handled until the subinterpreter finishes.
finally:
    done = True

EOF
$ ./python /tmp/bug-interp-not-interrupted.py
1
^C^C2
3
4
^C5
Traceback (most recent call last):
  File "/tmp/bug-interp-not-interruptedpy", line 17, in <module>
    interp.exec_sync("""if True:
    ...
  File "./cpython/Lib/test/support/interpreters/__init__.py", line 178, in exec_sync
    excinfo = _interpreters.exec(self._id, code)
              ~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^
KeyboardInterrupt

This sort of makes sense since signals are handled by the main thread of the main interpreter, and only via the eval breaker (triggered exclusively in the eval loop). Thus if a subinterpreter is running in the main thread, signals (including from Ctrl-C) won't be handled until the next time the main interpreter is running in the main thread.

Possible solutions:

  • handle signals in a dedicated thread, separate from the main thread (guaranteed to only run the main interpreter)
  • handle signals in whichever interpreter happens to be running in the main thread
@ericsnowcurrently ericsnowcurrently added type-bug An unexpected behavior, bug, or error interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-subinterpreters 3.12 bugs and security fixes 3.13 new features, bugs and security fixes labels Dec 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.12 bugs and security fixes 3.13 new features, bugs and security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-subinterpreters type-bug An unexpected behavior, bug, or error
Projects
Status: Todo
Development

No branches or pull requests

1 participant
X Tutup