Skip to content

Commit efb60c0

Browse files
committed
Issue #1722344: threading._shutdown() is now called in Py_Finalize(), which
fixes the problem of some exceptions being thrown at shutdown when the interpreter is killed. Patch by Adam Olsen.
1 parent a7a52ab commit efb60c0

File tree

5 files changed

+59
-29
lines changed

5 files changed

+59
-29
lines changed

Lib/test/test_threading.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,30 @@ def func(frame, event, arg):
306306
self.assertFalse(rc == 2, "interpreted was blocked")
307307
self.assertTrue(rc == 0, "Unexpected error")
308308

309+
def test_join_nondaemon_on_shutdown(self):
310+
# Issue 1722344
311+
# Raising SystemExit skipped threading._shutdown
312+
import subprocess
313+
p = subprocess.Popen([sys.executable, "-c", """if 1:
314+
import threading
315+
from time import sleep
316+
317+
def child():
318+
sleep(1)
319+
# As a non-daemon thread we SHOULD wake up and nothing
320+
# should be torn down yet
321+
print "Woke up, sleep function is:", sleep
322+
323+
threading.Thread(target=child).start()
324+
raise SystemExit
325+
"""],
326+
stdout=subprocess.PIPE,
327+
stderr=subprocess.PIPE)
328+
stdout, stderr = p.communicate()
329+
self.assertEqual(stdout, "Woke up, sleep function is: <built-in function sleep>\n")
330+
self.assertEqual(stderr, "")
331+
332+
309333

310334
def test_enumerate_after_join(self):
311335
# Try hard to trigger #1703448: a thread is still returned in

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -540,6 +540,7 @@ Kevin O'Connor
540540
Tim O'Malley
541541
Pascal Oberndoerfer
542542
Jeffrey Ollie
543+
Adam Olsen
543544
Grant Olson
544545
Piet van Oostrum
545546
Jason Orendorff

Misc/NEWS

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ What's New in Python 2.7 alpha 1
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue #1722344: threading._shutdown() is now called in Py_Finalize(), which
16+
fixes the problem of some exceptions being thrown at shutdown when the
17+
interpreter is killed. Patch by Adam Olsen.
18+
1519
- Issue #7168: Document PyFloat_AsString and PyFloat_AsReprString, and
1620
note that they are unsafe and deprecated.
1721

Modules/main.c

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -222,33 +222,6 @@ static int RunMainFromImporter(char *filename)
222222
}
223223

224224

225-
/* Wait until threading._shutdown completes, provided
226-
the threading module was imported in the first place.
227-
The shutdown routine will wait until all non-daemon
228-
"threading" threads have completed. */
229-
#include "abstract.h"
230-
static void
231-
WaitForThreadShutdown(void)
232-
{
233-
#ifdef WITH_THREAD
234-
PyObject *result;
235-
PyThreadState *tstate = PyThreadState_GET();
236-
PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,
237-
"threading");
238-
if (threading == NULL) {
239-
/* threading not imported */
240-
PyErr_Clear();
241-
return;
242-
}
243-
result = PyObject_CallMethod(threading, "_shutdown", "");
244-
if (result == NULL)
245-
PyErr_WriteUnraisable(threading);
246-
else
247-
Py_DECREF(result);
248-
Py_DECREF(threading);
249-
#endif
250-
}
251-
252225
/* Main program */
253226

254227
int
@@ -624,8 +597,6 @@ Py_Main(int argc, char **argv)
624597
sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
625598
}
626599

627-
WaitForThreadShutdown();
628-
629600
Py_Finalize();
630601
#ifdef RISCOS
631602
if (Py_RISCOSWimpFlag)

Python/pythonrun.c

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "ast.h"
1818
#include "eval.h"
1919
#include "marshal.h"
20+
#include "abstract.h"
2021

2122
#ifdef HAVE_SIGNAL_H
2223
#include <signal.h>
@@ -61,6 +62,7 @@ static PyObject *run_pyc_file(FILE *, const char *, PyObject *, PyObject *,
6162
PyCompilerFlags *);
6263
static void err_input(perrdetail *);
6364
static void initsigs(void);
65+
static void wait_for_thread_shutdown(void);
6466
static void call_sys_exitfunc(void);
6567
static void call_ll_exitfuncs(void);
6668
extern void _PyUnicode_Init(void);
@@ -390,6 +392,8 @@ Py_Finalize(void)
390392
if (!initialized)
391393
return;
392394

395+
wait_for_thread_shutdown();
396+
393397
/* The interpreter is still entirely intact at this point, and the
394398
* exit funcs may be relying on that. In particular, if some thread
395399
* or exit func is still waiting to do an import, the import machinery
@@ -1666,6 +1670,32 @@ Py_FatalError(const char *msg)
16661670
#include "pythread.h"
16671671
#endif
16681672

1673+
/* Wait until threading._shutdown completes, provided
1674+
the threading module was imported in the first place.
1675+
The shutdown routine will wait until all non-daemon
1676+
"threading" threads have completed. */
1677+
static void
1678+
wait_for_thread_shutdown(void)
1679+
{
1680+
#ifdef WITH_THREAD
1681+
PyObject *result;
1682+
PyThreadState *tstate = PyThreadState_GET();
1683+
PyObject *threading = PyMapping_GetItemString(tstate->interp->modules,
1684+
"threading");
1685+
if (threading == NULL) {
1686+
/* threading not imported */
1687+
PyErr_Clear();
1688+
return;
1689+
}
1690+
result = PyObject_CallMethod(threading, "_shutdown", "");
1691+
if (result == NULL)
1692+
PyErr_WriteUnraisable(threading);
1693+
else
1694+
Py_DECREF(result);
1695+
Py_DECREF(threading);
1696+
#endif
1697+
}
1698+
16691699
#define NEXITFUNCS 32
16701700
static void (*exitfuncs[NEXITFUNCS])(void);
16711701
static int nexitfuncs = 0;

0 commit comments

Comments
 (0)