Skip to content

Commit 05a5d69

Browse files
authored
bpo-41686: Always create the SIGINT event on Windows (GH-23344) (GH-23347)
bpo-41686, bpo-41713: On Windows, the SIGINT event, _PyOS_SigintEvent(), is now created even if Python is configured to not install signal handlers (PyConfig.install_signal_handlers=0 or Py_InitializeEx(0)).
1 parent ac472b3 commit 05a5d69

File tree

4 files changed

+54
-30
lines changed

4 files changed

+54
-30
lines changed

Include/internal/pycore_pylifecycle.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ extern void _PyFloat_Fini(void);
6868
extern void _PySlice_Fini(void);
6969
extern void _PyAsyncGen_Fini(void);
7070

71+
extern int _PySignal_Init(int install_signal_handlers);
7172
extern void PyOS_FiniInterrupts(void);
7273

7374
extern void _PyExc_Fini(void);
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
On Windows, the ``SIGINT`` event, ``_PyOS_SigintEvent()``, is now created
2+
even if Python is configured to not install signal handlers (if
3+
:c:member:`PyConfig.install_signal_handlers` equals to 0, or
4+
``Py_InitializeEx(0)``).

Modules/signalmodule.c

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1632,11 +1632,6 @@ PyInit__signal(void)
16321632
goto finally;
16331633
#endif
16341634

1635-
#ifdef MS_WINDOWS
1636-
/* Create manual-reset event, initially unset */
1637-
sigint_event = CreateEvent(NULL, TRUE, FALSE, FALSE);
1638-
#endif
1639-
16401635
if (PyErr_Occurred()) {
16411636
Py_DECREF(m);
16421637
m = NULL;
@@ -1773,6 +1768,53 @@ PyOS_InitInterrupts(void)
17731768
}
17741769
}
17751770

1771+
1772+
static int
1773+
signal_install_handlers(void)
1774+
{
1775+
#ifdef SIGPIPE
1776+
PyOS_setsig(SIGPIPE, SIG_IGN);
1777+
#endif
1778+
#ifdef SIGXFZ
1779+
PyOS_setsig(SIGXFZ, SIG_IGN);
1780+
#endif
1781+
#ifdef SIGXFSZ
1782+
PyOS_setsig(SIGXFSZ, SIG_IGN);
1783+
#endif
1784+
1785+
// Import _signal to install the Python SIGINT handler
1786+
PyObject *module = PyImport_ImportModule("_signal");
1787+
if (!module) {
1788+
return -1;
1789+
}
1790+
Py_DECREF(module);
1791+
1792+
return 0;
1793+
}
1794+
1795+
1796+
int
1797+
_PySignal_Init(int install_signal_handlers)
1798+
{
1799+
#ifdef MS_WINDOWS
1800+
/* Create manual-reset event, initially unset */
1801+
sigint_event = CreateEvent(NULL, TRUE, FALSE, FALSE);
1802+
if (sigint_event == NULL) {
1803+
PyErr_SetFromWindowsErr(0);
1804+
return -1;
1805+
}
1806+
#endif
1807+
1808+
if (install_signal_handlers) {
1809+
if (signal_install_handlers() < 0) {
1810+
return -1;
1811+
}
1812+
}
1813+
1814+
return 0;
1815+
}
1816+
1817+
17761818
void
17771819
PyOS_FiniInterrupts(void)
17781820
{

Python/pylifecycle.c

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,6 @@ static PyStatus add_main_module(PyInterpreterState *interp);
5757
static PyStatus init_import_site(void);
5858
static PyStatus init_set_builtins_open(void);
5959
static PyStatus init_sys_streams(PyThreadState *tstate);
60-
static PyStatus init_signals(PyThreadState *tstate);
6160
static void call_py_exitfuncs(PyThreadState *tstate);
6261
static void wait_for_thread_shutdown(PyThreadState *tstate);
6362
static void call_ll_exitfuncs(_PyRuntimeState *runtime);
@@ -1013,11 +1012,8 @@ init_interp_main(PyThreadState *tstate)
10131012
}
10141013

10151014
if (is_main_interp) {
1016-
if (config->install_signal_handlers) {
1017-
status = init_signals(tstate);
1018-
if (_PyStatus_EXCEPTION(status)) {
1019-
return status;
1020-
}
1015+
if (_PySignal_Init(config->install_signal_handlers) < 0) {
1016+
return _PyStatus_ERR("can't initialize signals");
10211017
}
10221018

10231019
if (_PyTraceMalloc_Init(config->tracemalloc) < 0) {
@@ -2442,25 +2438,6 @@ Py_Exit(int sts)
24422438
exit(sts);
24432439
}
24442440

2445-
static PyStatus
2446-
init_signals(PyThreadState *tstate)
2447-
{
2448-
#ifdef SIGPIPE
2449-
PyOS_setsig(SIGPIPE, SIG_IGN);
2450-
#endif
2451-
#ifdef SIGXFZ
2452-
PyOS_setsig(SIGXFZ, SIG_IGN);
2453-
#endif
2454-
#ifdef SIGXFSZ
2455-
PyOS_setsig(SIGXFSZ, SIG_IGN);
2456-
#endif
2457-
PyOS_InitInterrupts(); /* May imply init_signals() */
2458-
if (_PyErr_Occurred(tstate)) {
2459-
return _PyStatus_ERR("can't import signal");
2460-
}
2461-
return _PyStatus_OK();
2462-
}
2463-
24642441

24652442
/* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL.
24662443
*

0 commit comments

Comments
 (0)