Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 056f5b9

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 a290bac + 9a7c524 commit 056f5b9

3 files changed

Lines changed: 49 additions & 0 deletions

File tree

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

@@ -691,6 +692,37 @@ def test_daemonize_active_thread(self):
691692
thread.start()
692693
self.assertRaises(RuntimeError, setattr, thread, "daemon", True)
693694

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

695727
class LockTests(lock_tests.LockTests):
696728
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.2.1 release candidate 2?
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
Library
1419
-------
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)