-
Notifications
You must be signed in to change notification settings - Fork 146
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 doesn't send SIGINT to python program launched with debugger #500
Comments
Just as a note, it does seem to work properly for me on Windows (so, it's probably a Mac-only issue). |
Thank you - definitely useful and interesting info. To be clear, just generic ctrl-c copies and pastes outside of a terminal, SIGINTs inside of a terminal, and also SIGINTs in the debugger, yeah? I've chased down a million different key remaps or other key combinations or shortcuts or commands for trying to send a SIGINT to the running process, but none achieve the expected KeyboardInterrupt in my running program, so I can't say whether I'm doing it wrong or whether the debugger catches and kills them all. |
One thing to check: if instead of the |
Mmm Interesting test: No. Ctrl-C does not work in the external terminal. And symmetrically, if I "Run python file in terminal" instead of using the debugger, which does launch python in an integrated terminal, ctrl-c does work as expected. So this sounds like specifically a debugger problem (good call @karrtikr ) |
It might have something to do with the fact that we're using a separate process group to spawn the debuggee in, so that it can be cleanly terminated with its entire subprocess tree: debugpy/src/debugpy/launcher/debuggee.py Lines 63 to 87 in 6116bc1
That said, it should still be receiving signals, since it's the foreground process in the session. This might be something Mac (or BSD) specific. (On Windows, we use job objects instead, which don't affect terminal interaction.) |
@int19h Is there a different shortcut/signal I could try sending? like, is there a shortcut to send SIGUSR1 or something? It looks like python has a special case for SIGINT=KeyboardInterrupt, so I'm not sure how valid a test it would be anyway. |
There's Ctrl+D for |
I'll try to put together a test |
I did some digging and noted that the default SIGINT handler is not set when running python programs inside vscode
which is printing "Handlers.SIG_IGN" whereas it should print "<built-in function default_int_handler>" It's not a proper solution but setting the default handler inside the python script fixes this problem:
|
Ok think I've found the real reason: debugpy/src/debugpy/launcher/__main__.py Lines 29 to 32 in 1de5526
Removing this line fixes the problem |
Hey cool! That does look VERY intentional though. I don't fully understand the logic behind the decision as it's written there. What it DOES look like is that it assumes Ctrl-C "always means exit" rather than allowing for the possibility that a process under debug might want to do its own handling. |
It's actually the other way around. The problem is that, because the launcher is the process that spawns the actual debuggee, it receives SIGINT first on Ctrl+C. Which causes it to exit, which in turns kills the debuggee - even if the debuggee actually wanted to handle that signal. So this was an attempt to get the launcher out of the picture, such that the debuggee can do its own thing. I'm not sure why it's not working properly. Note that the launcher is a separate process, and so whatever signal handling tweaks it makes, they do not affect the debuggee - that was the whole point. But I don't think the suggested fix is correct on all platforms. This requires further investigation. |
I checked the suggested fix, and it does indeed cause an issue on Windows - the same one that we originally added it for: microsoft/ptvsd#1896 However, the change does work on Linux. On the other hand, the unchanged code works on Windows (i.e. debuggee does receive KeyboardInterrupt). So, I think this needs to be handled in a platform-specific way. @osown, can you please update the PR to add a check for |
Extensive searching leads me to believe this probably isn't a bug, but I still can't figure out how to get the expected behavior in Code. In a nutshell, "Ctrl-C" should raise a
KeyboardInterupt
in a running python program. When run in a vscode-python terminal withpython3 program.py
, it works as expected. When launched via debugger, it does not and the program just keeps executing.Environment data
python.languageServer
setting: Jedi[NOTE: If you suspect that your issue is related to the Microsoft Python Language Server (
python.languageServer: 'Microsoft'
), please download our new language server Pylance from the VS Code marketplace to see if that fixes your issue]^ Tried this, no change.
Expected behaviour
When I "Start debugging" with "Python: Current file", "ctrl-c" at terminal, SIGINT is passed to python process and raises a KeyboardException wherever it is currently executing.
Actual behaviour
The above works as expected when a program is run from the terminal with
python3 program.py,
but when launched with the debugger as above, SIGINT isn't passed to Python.Steps to reproduce:
KeyboardException
The text was updated successfully, but these errors were encountered: