From 302922365982e01bede2ba1b58e5567362537963 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Fri, 9 Jan 2026 16:08:40 +0100 Subject: [PATCH] gh-143191: Use _PyOS_MIN_STACK_SIZE in _thread.stack_size() (GH-143601) The stack size must be at least _PyOS_MIN_STACK_SIZE+SYSTEM_PAGE_SIZE bytes. (cherry picked from commit ba10100c3931b18812b82d7124e2238f01927910) Co-authored-by: Victor Stinner --- Lib/test/test_thread.py | 8 ++++++++ .../2026-01-09-13-07-22.gh-issue-143191.PPR_vW.rst | 2 ++ Modules/_threadmodule.c | 7 ++++--- 3 files changed, 14 insertions(+), 3 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2026-01-09-13-07-22.gh-issue-143191.PPR_vW.rst diff --git a/Lib/test/test_thread.py b/Lib/test/test_thread.py index d94e04250c9307..ac924728febc99 100644 --- a/Lib/test/test_thread.py +++ b/Lib/test/test_thread.py @@ -76,6 +76,14 @@ def test_stack_size(self): 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 00000000000000..507b58362bec97 --- /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 1389a1ef2c1b23..32110090feb21e 100644 --- a/Modules/_threadmodule.c +++ b/Modules/_threadmodule.c @@ -2147,9 +2147,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; }