From 517ac72b00f34ce6fc00ea79997d5bf9464401b1 Mon Sep 17 00:00:00 2001 From: Ned Deily Date: Sat, 9 Apr 2011 12:32:12 -0700 Subject: [PATCH] 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) --- Lib/test/test_threading.py | 30 ++++++++++++++++++++++++++++++ Misc/NEWS | 5 +++++ Python/thread_pthread.h | 12 ++++++++++++ 3 files changed, 47 insertions(+) diff --git a/Lib/test/test_threading.py b/Lib/test/test_threading.py index f977a7fbbee..75235ad25dc 100644 --- a/Lib/test/test_threading.py +++ b/Lib/test/test_threading.py @@ -650,6 +650,36 @@ class ThreadingExceptionTests(unittest.TestCase): thread.start() self.assertRaises(RuntimeError, setattr, thread, "daemon", True) + def test_recursion_limit(self): + # Issue 9670 + # test that excessive recursion within a non-main thread causes + # an exception rather than crashing the interpreter on platforms + # like Mac OS X or FreeBSD which have small default stack sizes + # for threads + script = """if True: + import threading + + def recurse(): + return recurse() + + def outer(): + try: + recurse() + except RuntimeError: + pass + + w = threading.Thread(target=outer) + w.start() + w.join() + print('end of main thread') + """ + expected_output = "end of main thread\n" + p = subprocess.Popen([sys.executable, "-c", script], + stdout=subprocess.PIPE) + stdout, stderr = p.communicate() + data = stdout.decode().replace('\r', '') + self.assertEqual(p.returncode, 0, "Unexpected error") + self.assertEqual(data, expected_output) class LockTests(lock_tests.LockTests): locktype = staticmethod(threading.Lock) diff --git a/Misc/NEWS b/Misc/NEWS index 5307fe86621..81586f071a8 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -10,6 +10,11 @@ What's New in Python 3.1.4? Core and Builtins ----------------- +- 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. + (original patch by Ronald Oussoren) + - Issue #11650: PyOS_StdioReadline() retries fgets() if it was interrupted (EINTR), for example if the program is stopped with CTRL+z on Mac OS X. Patch written by Charles-Francois Natali. diff --git a/Python/thread_pthread.h b/Python/thread_pthread.h index 32fd2d0ef74..ffe2c7766e3 100644 --- a/Python/thread_pthread.h +++ b/Python/thread_pthread.h @@ -18,6 +18,18 @@ #ifndef THREAD_STACK_SIZE #define THREAD_STACK_SIZE 0 /* use default stack size */ #endif + +#if (defined(__APPLE__) || defined(__FreeBSD__)) && defined(THREAD_STACK_SIZE) && THREAD_STACK_SIZE == 0 + /* The default stack size for new threads on OSX is small enough that + * we'll get hard crashes instead of 'maximum recursion depth exceeded' + * exceptions. + * + * The default stack size below is the minimal stack size where a + * simple recursive function doesn't cause a hard crash. + */ +#undef THREAD_STACK_SIZE +#define THREAD_STACK_SIZE 0x100000 +#endif /* for safety, ensure a viable minimum stacksize */ #define THREAD_STACK_MIN 0x8000 /* 32kB */ #else /* !_POSIX_THREAD_ATTR_STACKSIZE */