Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 38f11cc

Browse files
authored
bpo-1054041: Exit properly after an uncaught ^C. (#11862)
* bpo-1054041: Exit properly by a signal after a ^C. An uncaught KeyboardInterrupt exception means the user pressed ^C and our code did not handle it. Programs that install SIGINT handlers are supposed to reraise the SIGINT signal to the SIG_DFL handler in order to exit in a manner that their calling process can detect that they died due to a Ctrl-C. https://www.cons.org/cracauer/sigint.html After this change on POSIX systems while true; do python -c 'import time; time.sleep(23)'; done can be stopped via a simple Ctrl-C instead of the shell infinitely restarting a new python process. What to do on Windows, or if anything needs to be done there has not yet been determined. That belongs in its own PR. TODO(gpshead): A unittest for this behavior is still needed. * Do the unhandled ^C check after pymain_free. * Return STATUS_CONTROL_C_EXIT on Windows. * Fix ifdef around unistd.h include. * 📜🤖 Added by blurb_it. * Add STATUS_CTRL_C_EXIT to the os module on Windows * Add unittests. * Don't send CTRL_C_EVENT in the Windows test. It was causing CI systems to bail out of the entire test suite. See https://dev.azure.com/Python/cpython/_build/results?buildId=37980 for example. * Correct posix test (fail on macOS?) check. * STATUS_CONTROL_C_EXIT must be unsigned. * Improve the error message. * test typo :) * Skip if the bash version is too old. ...and rename the windows test to reflect what it does. * min bash version is 4.4, detect no bash. * restore a blank line i didn't mean to delete. * PyErr_Occurred() before the Py_DECREF(co); * Don't add os.STATUS_CONTROL_C_EXIT as a constant. * Update the Windows test comment. * Refactor common logic into a run_eval_code_obj fn.
1 parent 43766f8 commit 38f11cc

6 files changed

Lines changed: 107 additions & 6 deletions

File tree

Include/internal/pycore_pylifecycle.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ extern "C" {
88
# error "this header requires Py_BUILD_CORE or Py_BUILD_CORE_BUILTIN define"
99
#endif
1010

11+
/* True if the main interpreter thread exited due to an unhandled
12+
* KeyboardInterrupt exception, suggesting the user pressed ^C. */
13+
PyAPI_DATA(int) _Py_UnhandledKeyboardInterrupt;
14+
1115
PyAPI_FUNC(int) _Py_UnixMain(int argc, char **argv);
1216

1317
PyAPI_FUNC(int) _Py_SetFileSystemEncoding(

Lib/test/test_signal.py

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,48 @@ def test_valid_signals(self):
7878
self.assertNotIn(signal.NSIG, s)
7979
self.assertLess(len(s), signal.NSIG)
8080

81+
@unittest.skipUnless(sys.executable, "sys.executable required.")
82+
def test_keyboard_interrupt_exit_code(self):
83+
"""KeyboardInterrupt triggers exit via SIGINT."""
84+
process = subprocess.run(
85+
[sys.executable, "-c",
86+
"import os,signal; os.kill(os.getpid(), signal.SIGINT)"],
87+
stderr=subprocess.PIPE)
88+
self.assertIn(b"KeyboardInterrupt", process.stderr)
89+
self.assertEqual(process.returncode, -signal.SIGINT)
90+
91+
@unittest.skipUnless(sys.executable, "sys.executable required.")
92+
def test_keyboard_interrupt_communicated_to_shell(self):
93+
"""KeyboardInterrupt exits such that shells detect a ^C."""
94+
try:
95+
bash_proc = subprocess.run(
96+
["bash", "-c", 'echo "${BASH_VERSION}"'],
97+
stdout=subprocess.PIPE, stderr=subprocess.DEVNULL)
98+
except OSError:
99+
raise unittest.SkipTest("bash required.")
100+
if bash_proc.returncode:
101+
raise unittest.SkipTest("could not determine bash version.")
102+
bash_ver = bash_proc.stdout.decode("ascii").strip()
103+
bash_major_minor = [int(n) for n in bash_ver.split(".", 2)[:2]]
104+
if bash_major_minor < [4, 4]:
105+
# In older versions of bash, -i does not work as needed
106+
# _for this automated test_. Older shells do behave as
107+
# expected in manual interactive use.
108+
raise unittest.SkipTest(f"bash version {bash_ver} is too old.")
109+
# The motivation for https://bugs.python.org/issue1054041.
110+
# An _interactive_ shell (bash -i simulates that here) detects
111+
# when a command exits via ^C and stops executing further
112+
# commands.
113+
process = subprocess.run(
114+
["bash", "-ic",
115+
f"{sys.executable} -c 'import os,signal; os.kill(os.getpid(), signal.SIGINT)'; "
116+
"echo TESTFAIL using bash \"${BASH_VERSION}\""],
117+
stderr=subprocess.PIPE, stdout=subprocess.PIPE)
118+
self.assertIn(b"KeyboardInterrupt", process.stderr)
119+
# An interactive shell will abort if python exits properly to
120+
# indicate that a KeyboardInterrupt occurred.
121+
self.assertNotIn(b"TESTFAIL", process.stdout)
122+
81123

82124
@unittest.skipUnless(sys.platform == "win32", "Windows specific")
83125
class WindowsSignalTests(unittest.TestCase):
@@ -112,6 +154,20 @@ def test_issue9324(self):
112154
with self.assertRaises(ValueError):
113155
signal.signal(7, handler)
114156

157+
@unittest.skipUnless(sys.executable, "sys.executable required.")
158+
def test_keyboard_interrupt_exit_code(self):
159+
"""KeyboardInterrupt triggers an exit using STATUS_CONTROL_C_EXIT."""
160+
# We don't test via os.kill(os.getpid(), signal.CTRL_C_EVENT) here
161+
# as that requires setting up a console control handler in a child
162+
# in its own process group. Doable, but quite complicated. (see
163+
# @eryksun on https://github.com/python/cpython/pull/11862)
164+
process = subprocess.run(
165+
[sys.executable, "-c", "raise KeyboardInterrupt"],
166+
stderr=subprocess.PIPE)
167+
self.assertIn(b"KeyboardInterrupt", process.stderr)
168+
STATUS_CONTROL_C_EXIT = 0xC000013A
169+
self.assertEqual(process.returncode, STATUS_CONTROL_C_EXIT)
170+
115171

116172
class WakeupFDTests(unittest.TestCase):
117173

@@ -1217,11 +1273,8 @@ def handler(signum, frame):
12171273
class RaiseSignalTest(unittest.TestCase):
12181274

12191275
def test_sigint(self):
1220-
try:
1276+
with self.assertRaises(KeyboardInterrupt):
12211277
signal.raise_signal(signal.SIGINT)
1222-
self.fail("Expected KeyInterrupt")
1223-
except KeyboardInterrupt:
1224-
pass
12251278

12261279
@unittest.skipIf(sys.platform != "win32", "Windows specific test")
12271280
def test_invalid_argument(self):
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
When the main interpreter exits due to an uncaught KeyboardInterrupt, the process now exits in the appropriate manner for its parent process to detect that a SIGINT or ^C terminated the process. This allows shells and batch scripts to understand that the user has asked them to stop.

Modules/main.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99
#include "pycore_pystate.h"
1010

1111
#include <locale.h>
12+
#ifdef HAVE_SIGNAL_H
13+
#include <signal.h>
14+
#endif
15+
#include <stdio.h>
16+
#if defined(HAVE_GETPID) && defined(HAVE_UNISTD_H)
17+
#include <unistd.h>
18+
#endif
1219

1320
#if defined(MS_WINDOWS) || defined(__CYGWIN__)
1421
# include <windows.h>
@@ -1830,6 +1837,29 @@ pymain_main(_PyMain *pymain)
18301837

18311838
pymain_free(pymain);
18321839

1840+
if (_Py_UnhandledKeyboardInterrupt) {
1841+
/* https://bugs.python.org/issue1054041 - We need to exit via the
1842+
* SIG_DFL handler for SIGINT if KeyboardInterrupt went unhandled.
1843+
* If we don't, a calling process such as a shell may not know
1844+
* about the user's ^C. https://www.cons.org/cracauer/sigint.html */
1845+
#if defined(HAVE_GETPID) && !defined(MS_WINDOWS)
1846+
if (PyOS_setsig(SIGINT, SIG_DFL) == SIG_ERR) {
1847+
perror("signal"); /* Impossible in normal environments. */
1848+
} else {
1849+
kill(getpid(), SIGINT);
1850+
}
1851+
/* If setting SIG_DFL failed, or kill failed to terminate us,
1852+
* there isn't much else we can do aside from an error code. */
1853+
#endif /* HAVE_GETPID && !MS_WINDOWS */
1854+
#ifdef MS_WINDOWS
1855+
/* cmd.exe detects this, prints ^C, and offers to terminate. */
1856+
/* https://msdn.microsoft.com/en-us/library/cc704588.aspx */
1857+
pymain->status = STATUS_CONTROL_C_EXIT;
1858+
#else
1859+
pymain->status = SIGINT + 128;
1860+
#endif /* !MS_WINDOWS */
1861+
}
1862+
18331863
return pymain->status;
18341864
}
18351865

Python/pylifecycle.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ static void call_py_exitfuncs(PyInterpreterState *);
6666
static void wait_for_thread_shutdown(void);
6767
static void call_ll_exitfuncs(void);
6868

69+
int _Py_UnhandledKeyboardInterrupt = 0;
6970
_PyRuntimeState _PyRuntime = _PyRuntimeState_INIT;
7071

7172
_PyInitError

Python/pythonrun.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include "Python-ast.h"
1414
#undef Yield /* undefine macro conflicting with <winbase.h> */
15+
#include "pycore_pylifecycle.h"
1516
#include "pycore_pystate.h"
1617
#include "grammar.h"
1718
#include "node.h"
@@ -1027,6 +1028,17 @@ flush_io(void)
10271028
PyErr_Restore(type, value, traceback);
10281029
}
10291030

1031+
static PyObject *
1032+
run_eval_code_obj(PyCodeObject *co, PyObject *globals, PyObject *locals)
1033+
{
1034+
PyObject *v;
1035+
v = PyEval_EvalCode((PyObject*)co, globals, locals);
1036+
if (!v && PyErr_Occurred() == PyExc_KeyboardInterrupt) {
1037+
_Py_UnhandledKeyboardInterrupt = 1;
1038+
}
1039+
return v;
1040+
}
1041+
10301042
static PyObject *
10311043
run_mod(mod_ty mod, PyObject *filename, PyObject *globals, PyObject *locals,
10321044
PyCompilerFlags *flags, PyArena *arena)
@@ -1036,7 +1048,7 @@ run_mod(mod_ty mod, PyObject *filename, PyObject *globals, PyObject *locals,
10361048
co = PyAST_CompileObject(mod, filename, flags, -1, arena);
10371049
if (co == NULL)
10381050
return NULL;
1039-
v = PyEval_EvalCode((PyObject*)co, globals, locals);
1051+
v = run_eval_code_obj(co, globals, locals);
10401052
Py_DECREF(co);
10411053
return v;
10421054
}
@@ -1073,7 +1085,7 @@ run_pyc_file(FILE *fp, const char *filename, PyObject *globals,
10731085
}
10741086
fclose(fp);
10751087
co = (PyCodeObject *)v;
1076-
v = PyEval_EvalCode((PyObject*)co, globals, locals);
1088+
v = run_eval_code_obj(co, globals, locals);
10771089
if (v && flags)
10781090
flags->cf_flags |= (co->co_flags & PyCF_MASK);
10791091
Py_DECREF(co);

0 commit comments

Comments
 (0)