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 will exit out of Python interpreter in Windows #46018
Comments
When running Python 2.5.1 stable in Windows, you can press Ctrl-C as Steps to reproduce: |
I don't think it's a critical bug but it may be worth to debug it. |
I get the same behaviour with python 2.5.1, and even 2.4.4 (on Windows So this is not 3.0 specific. What is new in python3.0 is that file |
I wanted to add that this issue also affects python 2.5.1 on the Mac. |
Removing the Windows part: on my machine, repeated Ctrl-C's don't exit the |
I cannot reproduce this on my machine (running OSX) using 2.5, 2.6 and 3.1 |
Unassigning this issue from myself as I cannot reproduce the issue on OSX. |
I can't reproduce this on windows (in a KVM) with 2.6 or 2.7. |
I tested this on a real Windows 7 machine (64 bit, Ultimate) I have seen this issue fixed in Python 2.5.2 on Windows. It would never exit the interpreter no matter how long I pressed Ctrl-C. I don't see this issue in Linux. I don't have a Mac to test, but I'd like Mac users to test the signal handling in their terminals. |
I'm not able to reproduce this. Do you have anything installed like pyreadline? |
Windows 7 64-bit (on the metal, not in a VM), can confirm. Holding down Ctrl+C will (eventually) halt Python on all the versions I have installed: 2.3, 2.7, 3.0, 3.1, 3.2. (All of these are 32-bit Pythons). Haven't done anything silly like try to install readline on Windows. I also tried it on cygwin Python (2.6), held down for maybe 20 seconds, didn't crash. |
For extra clarification, this issue can crop up with even a single press of ctrl-c. It's not really related to multiple presses, except that pressing it more increases the odds of it happening. |
Could add a printf() to PC/launcher.c:ctrl_c_handler() to test if the handler is called in the error case? |
Just to confirm: I can reproduce this more or less consistently on all versions of Python 2.2 -> 3.2 on Windows 7. Those are distribution builds -- ie not debug builds I've made myself. But it certainly does occur on a debug build of 2.7. I'm trying to narrow it down through some instrumentation (read: printf) on the 2.7 branch. The MSDN page for signal warns that a separate thread will be created for the signal handler which will muddy the waters. |
OK, I've run out of time to look, but the culprit looks like it's an odd interaction between my_fgets in myreadline.c and the interrupt handler in signal. There's a section of code which is conditional on ERROR_OPERATION_ABORTED being returned from fgets in the CRT. That then tries to play ball with a the interrupt handler having fired already (in a separate thread) by sleeping for one second and checking that the handler was tripped. If it has then the function returns one and stuff happens elsewhere; it it hasn't then the function behaves as if EOF (ie Ctrl-Z) was pressed and Python exits. That's as far as I've got; it looks like a race condition of some sort but I can't see where. I'm not 100% sure that the SIGINT handler is tripping. |
Great analysis!
|
That'll be my next move when I get some more time. |
OK, it is a race condition between the code in myreadline.c and the |
Attached is a patch against Python 2.7. It checks in a loop for SIGINT and, if it still hasn't fired, assumed Ctrl-Z was pressed which generates the same error. |
Shouldn't we be using a proper synchronization primitive? What about waiting for an event? |
In fact, there is _PyOS_SigintEvent at the end of signalmodule.c |
Is the ERROR_OPERATION_ABORTED set when Ctrl-Z is hit? |
Yep. |
To be clear: ERROR_OPERATION_ABORTED is set when Ctrl-Z is hit as the |
This patch is for 2.7 and does enough to solve the issue without a major |
Hm, I wonder if the test for the SIGINT event is required at all. |
Nope. Ctrl-C also sets EOF |
And here's a patch for the default branch, using the new sigint event as Kristan suggested. |
And here's the patch against 3.2 (essentially the 2.7 patch but allowing for the removal of RISCOS support) |
Tim, you've got tabs in your 3.3 patch. |
Thanks, Antoine. I'll sort that out. (Goodness know how;
Ummm. Because they were written separately and I didn't |
New changeset bb4b184e5b33 by Tim Golden in branch '2.7': New changeset 52af10209976 by Tim Golden in branch '3.2': New changeset 9523c122d6fc by Tim Golden in branch 'default': |
New changeset 2de5c9ced464 by Jesus Cea in branch '2.7': |
New changeset 4de541fbdd58 by Jesus Cea in branch '3.2': New changeset 7937aa6b7e92 by Jesus Cea in branch 'default': |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: