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

Overriding SIGINT doesn't work as expected in the new REPL #120221

Closed
pablogsal opened this issue Jun 7, 2024 · 8 comments
Closed

Overriding SIGINT doesn't work as expected in the new REPL #120221

pablogsal opened this issue Jun 7, 2024 · 8 comments
Labels
3.13 bugs and security fixes 3.14 new features, bugs and security fixes release-blocker topic-repl Related to the interactive shell type-bug An unexpected behavior, bug, or error

Comments

@pablogsal
Copy link
Member

pablogsal commented Jun 7, 2024

Bug report

In the old REPL, overriding SIGINT and pressing Ctrl-C works as expected:

❯ python
Python 3.11.6 (main, Nov  3 2023, 17:05:41) [Clang 15.0.0 (clang-1500.0.40.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import signal
>>> signal.signal(signal.SIGINT, lambda *x: print("NOOOO"))
<built-in function default_int_handler>
>>> NOOOO
NOOOO

but in the new REPL it doesn't trigger the signal handler:

❯ ./python.exe
Python 3.14.0a0 (heads/more_offsets-dirty:9403c2cf58, Jun  6 2024, 12:10:11) [Clang 15.0.0 (clang-1500.3.9.4)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import signal
>>> signal.signal(signal.SIGINT, lambda *x: print("NOOOO"))
<built-in function default_int_handler>
>>>
KeyboardInterrupt
>>>

Linked PRs

@pablogsal pablogsal added type-bug An unexpected behavior, bug, or error 3.13 bugs and security fixes 3.14 new features, bugs and security fixes labels Jun 7, 2024
@pablogsal
Copy link
Member Author

CC: @lysnikolaou @ambv

@Eclips4 Eclips4 added the topic-repl Related to the interactive shell label Jun 7, 2024
@pablogsal
Copy link
Member Author

This will be fixed by #120234

@vstinner
Copy link
Member

I noticed the problem when trying to debug Python in gdb. CTRL+C doesn't go into gdb anymore. For now, I'm using a bash alias:

alias gdb='PYTHON_BASIC_REPL=1 gdb'

@vstinner
Copy link
Member

Thank you. CTRL+C is gdb stops again the process as expected :)

@eryksun
Copy link
Contributor

eryksun commented Jun 19, 2024

This could have been addressed by changing default_keymap in "Lib/_pyepl/reader.py" to map (r"\x03", "interrupt") instead of mapping it to the ctrl_c command. The interrupt command calls os.kill(os.getpid(), signal.SIGINT), which works fine for overriding the SIGINT handler and supporting gdb. That said, the decision to let the terminal send the signal makes it equivalent to how it's currently implemented on Windows, and consistency is a plus.

@pablogsal
Copy link
Member Author

pablogsal commented Jun 19, 2024

This could have been addressed by changing default_keymap in "Lib/_pyepl/reader.py" to map (r"\x03", "interrupt") instead of mapping it to the ctrl_c command. The interrupt command calls os.kill(os.getpid(), signal.SIGINT), which works fine for overriding the SIGINT handler and supporting gdb. That said, the decision to let the terminal send the signal makes it equivalent to how it's currently implemented on Windows, and consistency is a plus.

The problem of that approach is that if you paste a huge buffer and then want to cancel because pasting it's too slow then you need to wait until you get the key press for Ctrl-C to send the signal, which will happen after all the buffer is processed, while using signals directly allows us to cancel almost immediately

mrahtz pushed a commit to mrahtz/cpython that referenced this issue Jun 30, 2024
noahbkim pushed a commit to hudson-trading/cpython that referenced this issue Jul 11, 2024
estyxx pushed a commit to estyxx/cpython that referenced this issue Jul 17, 2024
@ambv ambv reopened this Sep 6, 2024
@ambv
Copy link
Contributor

ambv commented Sep 6, 2024

This broke CTRL-C in the asyncio REPL. Now the interrupt and ctrl_c commands are never executed. I need some more time to investigate what happens on Windows to not break that when fixing Linux/Mac.

ambv added a commit to ambv/cpython that referenced this issue Sep 6, 2024
This switches the main pyrepl event loop to always be non-blocking so that it
can listen to incoming interruptions from other threads.

This also resolves invalid display of exceptions from other threads
(pythongh-123178).
ambv added a commit to ambv/cpython that referenced this issue Sep 6, 2024
This switches the main pyrepl event loop to always be non-blocking so that it
can listen to incoming interruptions from other threads.

This also resolves invalid display of exceptions from other threads
(pythongh-123178).
ambv added a commit that referenced this issue Sep 6, 2024
This switches the main pyrepl event loop to always be non-blocking so that it
can listen to incoming interruptions from other threads.

This also resolves invalid display of exceptions from other threads
(gh-123178).

This also fixes freezes with pasting and an active input hook.
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Sep 6, 2024
…123795)

This switches the main pyrepl event loop to always be non-blocking so that it
can listen to incoming interruptions from other threads.

This also resolves invalid display of exceptions from other threads
(pythongh-123178).

This also fixes freezes with pasting and an active input hook.
(cherry picked from commit 033510e)

Co-authored-by: Łukasz Langa <lukasz@langa.pl>
ambv added a commit that referenced this issue Sep 6, 2024
… (#123799)

This switches the main pyrepl event loop to always be non-blocking so that it
can listen to incoming interruptions from other threads.

This also resolves invalid display of exceptions from other threads
(gh-123178).

This also fixes freezes with pasting and an active input hook.
(cherry picked from commit 033510e)

Co-authored-by: Łukasz Langa <lukasz@langa.pl>
@ambv
Copy link
Contributor

ambv commented Sep 6, 2024

Fixed by #123799.

@ambv ambv closed this as completed Sep 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.13 bugs and security fixes 3.14 new features, bugs and security fixes release-blocker topic-repl Related to the interactive shell type-bug An unexpected behavior, bug, or error
Projects
Development

No branches or pull requests

6 participants