Skip to content

Commit a702bd4

Browse files
authored
bpo-41686: Always create the SIGINT event on Windows (GH-23344) (GH-23347) (GH-23349)
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)). (cherry picked from commit 05a5d69)
1 parent 545dcb1 commit a702bd4

File tree

4 files changed

+55
-30
lines changed

4 files changed

+55
-30
lines changed

Include/internal/pycore_pylifecycle.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ extern void PyList_Fini(void);
6969
extern void PySet_Fini(void);
7070
extern void PyBytes_Fini(void);
7171
extern void PyFloat_Fini(void);
72+
73+
extern int _PySignal_Init(int install_signal_handlers);
7274
extern void PyOS_FiniInterrupts(void);
7375
extern void PySlice_Fini(void);
7476
extern void PyAsyncGen_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
@@ -1598,11 +1598,6 @@ PyInit__signal(void)
15981598
goto finally;
15991599
#endif
16001600

1601-
#ifdef MS_WINDOWS
1602-
/* Create manual-reset event, initially unset */
1603-
sigint_event = CreateEvent(NULL, TRUE, FALSE, FALSE);
1604-
#endif
1605-
16061601
if (PyErr_Occurred()) {
16071602
Py_DECREF(m);
16081603
m = NULL;
@@ -1726,6 +1721,53 @@ PyOS_InitInterrupts(void)
17261721
}
17271722
}
17281723

1724+
1725+
static int
1726+
signal_install_handlers(void)
1727+
{
1728+
#ifdef SIGPIPE
1729+
PyOS_setsig(SIGPIPE, SIG_IGN);
1730+
#endif
1731+
#ifdef SIGXFZ
1732+
PyOS_setsig(SIGXFZ, SIG_IGN);
1733+
#endif
1734+
#ifdef SIGXFSZ
1735+
PyOS_setsig(SIGXFSZ, SIG_IGN);
1736+
#endif
1737+
1738+
// Import _signal to install the Python SIGINT handler
1739+
PyObject *module = PyImport_ImportModule("_signal");
1740+
if (!module) {
1741+
return -1;
1742+
}
1743+
Py_DECREF(module);
1744+
1745+
return 0;
1746+
}
1747+
1748+
1749+
int
1750+
_PySignal_Init(int install_signal_handlers)
1751+
{
1752+
#ifdef MS_WINDOWS
1753+
/* Create manual-reset event, initially unset */
1754+
sigint_event = CreateEvent(NULL, TRUE, FALSE, FALSE);
1755+
if (sigint_event == NULL) {
1756+
PyErr_SetFromWindowsErr(0);
1757+
return -1;
1758+
}
1759+
#endif
1760+
1761+
if (install_signal_handlers) {
1762+
if (signal_install_handlers() < 0) {
1763+
return -1;
1764+
}
1765+
}
1766+
1767+
return 0;
1768+
}
1769+
1770+
17291771
void
17301772
PyOS_FiniInterrupts(void)
17311773
{

Python/pylifecycle.c

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ extern grammar _PyParser_Grammar; /* From graminit.c */
6363
static PyStatus add_main_module(PyInterpreterState *interp);
6464
static PyStatus init_import_size(void);
6565
static PyStatus init_sys_streams(PyInterpreterState *interp);
66-
static PyStatus init_signals(void);
6766
static void call_py_exitfuncs(PyInterpreterState *);
6867
static void wait_for_thread_shutdown(void);
6968
static void call_ll_exitfuncs(_PyRuntimeState *runtime);
@@ -952,11 +951,8 @@ pyinit_main(_PyRuntimeState *runtime, PyInterpreterState *interp)
952951
return status;
953952
}
954953

955-
if (config->install_signal_handlers) {
956-
status = init_signals();
957-
if (_PyStatus_EXCEPTION(status)) {
958-
return status;
959-
}
954+
if (_PySignal_Init(config->install_signal_handlers) < 0) {
955+
return _PyStatus_ERR("can't initialize signals");
960956
}
961957

962958
if (_PyTraceMalloc_Init(config->tracemalloc) < 0) {
@@ -2299,25 +2295,6 @@ Py_Exit(int sts)
22992295
exit(sts);
23002296
}
23012297

2302-
static PyStatus
2303-
init_signals(void)
2304-
{
2305-
#ifdef SIGPIPE
2306-
PyOS_setsig(SIGPIPE, SIG_IGN);
2307-
#endif
2308-
#ifdef SIGXFZ
2309-
PyOS_setsig(SIGXFZ, SIG_IGN);
2310-
#endif
2311-
#ifdef SIGXFSZ
2312-
PyOS_setsig(SIGXFSZ, SIG_IGN);
2313-
#endif
2314-
PyOS_InitInterrupts(); /* May imply init_signals() */
2315-
if (PyErr_Occurred()) {
2316-
return _PyStatus_ERR("can't import signal");
2317-
}
2318-
return _PyStatus_OK();
2319-
}
2320-
23212298

23222299
/* Restore signals that the interpreter has called SIG_IGN on to SIG_DFL.
23232300
*

0 commit comments

Comments
 (0)