From: Victor Stinner Date: Fri, 9 Jan 2026 15:08:40 +0000 (+0100) Subject: gh-143191: Use _PyOS_MIN_STACK_SIZE in _thread.stack_size() (#143601) X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=ba10100c3931b18812b82d7124e2238f01927910;p=thirdparty%2FPython%2Fcpython.git gh-143191: Use _PyOS_MIN_STACK_SIZE in _thread.stack_size() (#143601) The stack size must be at least _PyOS_MIN_STACK_SIZE+SYSTEM_PAGE_SIZE bytes. --- diff --git a/Lib/test/test_thread.py b/Lib/test/test_thread.py index d94e04250c93..ac924728febc 100644 --- a/Lib/test/test_thread.py +++ b/Lib/test/test_thread.py @@ -76,6 +76,14 @@ class ThreadRunningTests(BasicThreadTest): thread.stack_size(0) self.assertEqual(thread.stack_size(), 0, "stack_size not reset to default") + with self.assertRaises(ValueError): + # 123 bytes is too small + thread.stack_size(123) + + with self.assertRaises(ValueError): + # size must be positive + thread.stack_size(-4096) + @unittest.skipIf(os.name not in ("nt", "posix"), 'test meant for nt and posix') def test_nt_and_posix_stack_size(self): try: diff --git a/Misc/NEWS.d/next/Library/2026-01-09-13-07-22.gh-issue-143191.PPR_vW.rst b/Misc/NEWS.d/next/Library/2026-01-09-13-07-22.gh-issue-143191.PPR_vW.rst new file mode 100644 index 000000000000..507b58362bec --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-01-09-13-07-22.gh-issue-143191.PPR_vW.rst @@ -0,0 +1,2 @@ +:func:`_thread.stack_size` now raises :exc:`ValueError` if the stack size is +too small. Patch by Victor Stinner. diff --git a/Modules/_threadmodule.c b/Modules/_threadmodule.c index cc8277c57838..73eff2734361 100644 --- a/Modules/_threadmodule.c +++ b/Modules/_threadmodule.c @@ -2200,9 +2200,10 @@ thread_stack_size(PyObject *self, PyObject *args) if (!PyArg_ParseTuple(args, "|n:stack_size", &new_size)) return NULL; - if (new_size < 0) { - PyErr_SetString(PyExc_ValueError, - "size must be 0 or a positive value"); + Py_ssize_t min_size = _PyOS_MIN_STACK_SIZE + SYSTEM_PAGE_SIZE; + if (new_size != 0 && new_size < min_size) { + PyErr_Format(PyExc_ValueError, + "size must be at least %zi bytes", min_size); return NULL; }