File tree Expand file tree Collapse file tree 3 files changed +47
-0
lines changed
Expand file tree Collapse file tree 3 files changed +47
-0
lines changed Original file line number Diff line number Diff line change @@ -689,6 +689,36 @@ def test_releasing_unacquired_lock(self):
689689 lock = threading .Lock ()
690690 self .assertRaises (RuntimeError , lock .release )
691691
692+ def test_recursion_limit (self ):
693+ # Issue 9670
694+ # test that excessive recursion within a non-main thread causes
695+ # an exception rather than crashing the interpreter on platforms
696+ # like Mac OS X or FreeBSD which have small default stack sizes
697+ # for threads
698+ script = """if True:
699+ import threading
700+
701+ def recurse():
702+ return recurse()
703+
704+ def outer():
705+ try:
706+ recurse()
707+ except RuntimeError:
708+ pass
709+
710+ w = threading.Thread(target=outer)
711+ w.start()
712+ w.join()
713+ print('end of main thread')
714+ """
715+ expected_output = "end of main thread\n "
716+ p = subprocess .Popen ([sys .executable , "-c" , script ],
717+ stdout = subprocess .PIPE )
718+ stdout , stderr = p .communicate ()
719+ data = stdout .decode ().replace ('\r ' , '' )
720+ self .assertEqual (p .returncode , 0 , "Unexpected error" )
721+ self .assertEqual (data , expected_output )
692722
693723class LockTests (lock_tests .LockTests ):
694724 locktype = staticmethod (threading .Lock )
Original file line number Diff line number Diff line change @@ -10,6 +10,11 @@ What's New in Python 3.3 Alpha 1?
1010Core and Builtins
1111-----------------
1212
13+ - Issue #9670: Increase the default stack size for secondary threads on
14+ Mac OS X and FreeBSD to reduce the chances of a crash instead of a
15+ "maximum recursion depth" RuntimeError exception.
16+ (original patch by Ronald Oussoren)
17+
1318- Issue #11650: PyOS_StdioReadline() retries fgets() if it was interrupted
1419 (EINTR), for example if the program is stopped with CTRL+z on Mac OS X. Patch
1520 written by Charles-Francois Natali.
Original file line number Diff line number Diff line change 1818#ifndef THREAD_STACK_SIZE
1919#define THREAD_STACK_SIZE 0 /* use default stack size */
2020#endif
21+
22+ #if (defined(__APPLE__ ) || defined(__FreeBSD__ )) && defined(THREAD_STACK_SIZE ) && THREAD_STACK_SIZE == 0
23+ /* The default stack size for new threads on OSX is small enough that
24+ * we'll get hard crashes instead of 'maximum recursion depth exceeded'
25+ * exceptions.
26+ *
27+ * The default stack size below is the minimal stack size where a
28+ * simple recursive function doesn't cause a hard crash.
29+ */
30+ #undef THREAD_STACK_SIZE
31+ #define THREAD_STACK_SIZE 0x100000
32+ #endif
2133/* for safety, ensure a viable minimum stacksize */
2234#define THREAD_STACK_MIN 0x8000 /* 32kB */
2335#else /* !_POSIX_THREAD_ATTR_STACKSIZE */
You can’t perform that action at this time.
0 commit comments