Skip to content

Commit 482f908

Browse files
committed
Issue python#9670: Increase the default stack size for secondary threads on
Mac OS X and FreeBSD to reduce the chances of a crash instead of a "maximum recursion depth" RuntimeError exception. (patch by Ronald Oussoren)
1 parent 1e94021 commit 482f908

3 files changed

Lines changed: 48 additions & 0 deletions

File tree

‎Lib/test/test_threading.py‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -689,6 +689,37 @@ class SemaphoreTests(lock_tests.SemaphoreTests):
689689
class BoundedSemaphoreTests(lock_tests.BoundedSemaphoreTests):
690690
semtype = staticmethod(threading.BoundedSemaphore)
691691

692+
@unittest.skipUnless(sys.platform == 'darwin', 'test macosx problem')
693+
def test_recursion_limit(self):
694+
# Issue 9670
695+
# test that excessive recursion within a non-main thread causes
696+
# an exception rather than crashing the interpreter on platforms
697+
# like Mac OS X or FreeBSD which have small default stack sizes
698+
# for threads
699+
script = """if True:
700+
import threading
701+
702+
def recurse():
703+
return recurse()
704+
705+
def outer():
706+
try:
707+
recurse()
708+
except RuntimeError:
709+
pass
710+
711+
w = threading.Thread(target=outer)
712+
w.start()
713+
w.join()
714+
print('end of main thread')
715+
"""
716+
expected_output = "end of main thread\n"
717+
p = subprocess.Popen([sys.executable, "-c", script],
718+
stdout=subprocess.PIPE)
719+
stdout, stderr = p.communicate()
720+
data = stdout.decode().replace('\r', '')
721+
self.assertEqual(p.returncode, 0, "Unexpected error")
722+
self.assertEqual(data, expected_output)
692723

693724
def test_main():
694725
test.test_support.run_unittest(LockTests, RLockTests, EventTests,

‎Misc/NEWS‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ What's New in Python 2.7.2?
99
Core and Builtins
1010
-----------------
1111

12+
- Issue #9670: Increase the default stack size for secondary threads on
13+
Mac OS X and FreeBSD to reduce the chances of a crash instead of a
14+
"maximum recursion depth" RuntimeError exception.
15+
(patch by Ronald Oussoren)
16+
1217
- Correct lookup of __dir__ on objects. Among other things, this causes errors
1318
besides AttributeError found on lookup to be propagated.
1419

‎Python/thread_pthread.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,18 @@
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 0x400000
32+
#endif
2133
/* for safety, ensure a viable minimum stacksize */
2234
#define THREAD_STACK_MIN 0x8000 /* 32kB */
2335
#else /* !_POSIX_THREAD_ATTR_STACKSIZE */

0 commit comments

Comments
 (0)