Skip to content

Commit 61be422

Browse files
committed
Issue #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)
2 parents aeabd5f + 056f5b9 commit 61be422

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

Lib/test/test_threading.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import weakref
1313
import os
1414
from test.script_helper import assert_python_ok, assert_python_failure
15+
import subprocess
1516

1617
from test import lock_tests
1718

@@ -703,6 +704,37 @@ def test_releasing_unacquired_lock(self):
703704
lock = threading.Lock()
704705
self.assertRaises(RuntimeError, lock.release)
705706

707+
@unittest.skipUnless(sys.platform == 'darwin', 'test macosx problem')
708+
def test_recursion_limit(self):
709+
# Issue 9670
710+
# test that excessive recursion within a non-main thread causes
711+
# an exception rather than crashing the interpreter on platforms
712+
# like Mac OS X or FreeBSD which have small default stack sizes
713+
# for threads
714+
script = """if True:
715+
import threading
716+
717+
def recurse():
718+
return recurse()
719+
720+
def outer():
721+
try:
722+
recurse()
723+
except RuntimeError:
724+
pass
725+
726+
w = threading.Thread(target=outer)
727+
w.start()
728+
w.join()
729+
print('end of main thread')
730+
"""
731+
expected_output = "end of main thread\n"
732+
p = subprocess.Popen([sys.executable, "-c", script],
733+
stdout=subprocess.PIPE)
734+
stdout, stderr = p.communicate()
735+
data = stdout.decode().replace('\r', '')
736+
self.assertEqual(p.returncode, 0, "Unexpected error")
737+
self.assertEqual(data, expected_output)
706738

707739
class LockTests(lock_tests.LockTests):
708740
locktype = staticmethod(threading.Lock)

Misc/NEWS

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,11 @@ What's New in Python 3.3 Alpha 1?
1010
Core 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+
(patch by Ronald Oussoren)
17+
1318
- Issue #12106: The use of the multiple-with shorthand syntax is now reflected
1419
in the AST.
1520

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)