Skip to content

Commit 9aece75

Browse files
committed
Merged revisions 75570,75574,75624 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r75570 | antoine.pitrou | 2009-10-20 23:29:37 +0200 (mar., 20 oct. 2009) | 6 lines 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. ........ r75574 | antoine.pitrou | 2009-10-20 23:59:25 +0200 (mar., 20 oct. 2009) | 4 lines Test wouldn't work in debug mode. We probably need a function in test_support to handle this. ........ r75624 | antoine.pitrou | 2009-10-23 14:01:13 +0200 (ven., 23 oct. 2009) | 3 lines Fix Windows buildbot failure ........
1 parent f75774b commit 9aece75

File tree

5 files changed

+71
-29
lines changed

5 files changed

+71
-29
lines changed

Lib/test/test_threading.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,30 @@ def func(frame, event, arg):
284284
self.failIf(rc == 2, "interpreted was blocked")
285285
self.failUnless(rc == 0, "Unexpected error")
286286

287+
def test_join_nondaemon_on_shutdown(self):
288+
# Issue 1722344
289+
# Raising SystemExit skipped threading._shutdown
290+
import subprocess
291+
p = subprocess.Popen([sys.executable, "-c", """if 1:
292+
import threading
293+
from time import sleep
294+
295+
def child():
296+
sleep(1)
297+
# As a non-daemon thread we SHOULD wake up and nothing
298+
# should be torn down yet
299+
print "Woke up, sleep function is:", sleep
300+
301+
threading.Thread(target=child).start()
302+
raise SystemExit
303+
"""],
304+
stdout=subprocess.PIPE,
305+
stderr=subprocess.PIPE)
306+
stdout, stderr = p.communicate()
307+
self.assertEqual(stdout.strip(),
308+
"Woke up, sleep function is: <built-in function sleep>")
309+
stderr = re.sub(r"^\[\d+ refs\]", "", stderr, re.MULTILINE).strip()
310+
self.assertEqual(stderr, "")
287311

288312
def test_enumerate_after_join(self):
289313
# 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
@@ -517,6 +517,7 @@ Kevin O'Connor
517517
Tim O'Malley
518518
Pascal Oberndoerfer
519519
Jeffrey Ollie
520+
Adam Olsen
520521
Grant Olson
521522
Piet van Oostrum
522523
Jason Orendorff

Misc/NEWS

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,22 @@ Python News
44

55
(editors: check NEWS.help for information about editing NEWS using ReST.)
66

7+
What's New in Python 2.6.5
8+
==========================
9+
10+
*Release date: XX-XXX-20XX*
11+
12+
Core and Builtins
13+
-----------------
14+
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+
19+
Library
20+
-------
21+
22+
723
What's New in Python 2.6.4 final?
824
=================================
925

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
@@ -620,8 +593,6 @@ Py_Main(int argc, char **argv)
620593
sts = PyRun_AnyFileFlags(stdin, "<stdin>", &cf) != 0;
621594
}
622595

623-
WaitForThreadShutdown();
624-
625596
Py_Finalize();
626597
#ifdef RISCOS
627598
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);
@@ -387,6 +389,8 @@ Py_Finalize(void)
387389
if (!initialized)
388390
return;
389391

392+
wait_for_thread_shutdown();
393+
390394
/* The interpreter is still entirely intact at this point, and the
391395
* exit funcs may be relying on that. In particular, if some thread
392396
* or exit func is still waiting to do an import, the import machinery
@@ -1663,6 +1667,32 @@ Py_FatalError(const char *msg)
16631667
#include "pythread.h"
16641668
#endif
16651669

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

0 commit comments

Comments
 (0)