Skip to content

Commit 08c1661

Browse files
committed
Add finialization routines; fixed some memory leaks related to this.
Reset the SIGINT handler when the finalization is invoked.
1 parent 05f7c50 commit 08c1661

File tree

1 file changed

+35
-4
lines changed

1 file changed

+35
-4
lines changed

Modules/signalmodule.c

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ static PyObject *DefaultHandler;
104104
static PyObject *IgnoreHandler;
105105
static PyObject *IntHandler;
106106

107+
static RETSIGTYPE (*old_siginthandler)() = SIG_DFL;
108+
107109

108110

109111
static PyObject *
@@ -286,7 +288,6 @@ initsignal()
286288
x = DefaultHandler = PyInt_FromLong((long)SIG_DFL);
287289
if (!x || PyDict_SetItemString(d, "SIG_DFL", x) < 0)
288290
goto finally;
289-
Py_DECREF(x);
290291

291292
x = IgnoreHandler = PyInt_FromLong((long)SIG_IGN);
292293
if (!x || PyDict_SetItemString(d, "SIG_IGN", x) < 0)
@@ -295,10 +296,12 @@ initsignal()
295296
x = PyInt_FromLong((long)NSIG);
296297
if (!x || PyDict_SetItemString(d, "NSIG", x) < 0)
297298
goto finally;
299+
Py_DECREF(x);
298300

299301
x = IntHandler = PyDict_GetItemString(d, "default_int_handler");
300302
if (!x)
301303
goto finally;
304+
Py_INCREF(IntHandler);
302305

303306
Handlers[0].tripped = 0;
304307
for (i = 1; i < NSIG; i++) {
@@ -322,10 +325,10 @@ initsignal()
322325
}
323326
if (Handlers[SIGINT].func == DefaultHandler) {
324327
/* Install default int handler */
328+
Py_INCREF(IntHandler);
325329
Py_DECREF(Handlers[SIGINT].func);
326330
Handlers[SIGINT].func = IntHandler;
327-
Py_INCREF(IntHandler);
328-
signal(SIGINT, &signal_handler);
331+
old_siginthandler = signal(SIGINT, &signal_handler);
329332
}
330333

331334
#ifdef SIGHUP
@@ -503,7 +506,28 @@ initsignal()
503506

504507
/* Check for errors */
505508
finally:
506-
Py_FatalError("can't initialize module signal");
509+
return;
510+
}
511+
512+
static void
513+
finisignal()
514+
{
515+
int i;
516+
517+
signal(SIGINT, old_siginthandler);
518+
519+
for (i = 1; i < NSIG; i++) {
520+
Handlers[i].tripped = 0;
521+
Py_XDECREF(Handlers[i].func);
522+
Handlers[i].func = NULL;
523+
}
524+
525+
Py_XDECREF(IntHandler);
526+
IntHandler = NULL;
527+
Py_XDECREF(DefaultHandler);
528+
DefaultHandler = NULL;
529+
Py_XDECREF(IgnoreHandler);
530+
IgnoreHandler = NULL;
507531
}
508532

509533

@@ -561,6 +585,13 @@ void
561585
PyOS_InitInterrupts()
562586
{
563587
initsignal();
588+
_PyImport_FixupExtension("signal", "signal");
589+
}
590+
591+
void
592+
PyOS_FiniInterrupts()
593+
{
594+
finisignal();
564595
}
565596

566597
int

0 commit comments

Comments
 (0)