]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-143191: Use _PyOS_MIN_STACK_SIZE in _thread.stack_size() (#143601)
authorVictor Stinner <vstinner@python.org>
Fri, 9 Jan 2026 15:08:40 +0000 (16:08 +0100)
committerGitHub <noreply@github.com>
Fri, 9 Jan 2026 15:08:40 +0000 (16:08 +0100)
The stack size must be at least _PyOS_MIN_STACK_SIZE+SYSTEM_PAGE_SIZE
bytes.

Lib/test/test_thread.py
Misc/NEWS.d/next/Library/2026-01-09-13-07-22.gh-issue-143191.PPR_vW.rst [new file with mode: 0644]
Modules/_threadmodule.c

index d94e04250c93076c36ecf187a7db2115af9e1562..ac924728febc9919cb0b5ab1ad9c8d30556c4e75 100644 (file)
@@ -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 (file)
index 0000000..507b583
--- /dev/null
@@ -0,0 +1,2 @@
+:func:`_thread.stack_size` now raises :exc:`ValueError` if the stack size is
+too small. Patch by Victor Stinner.
index cc8277c578385874a8e01147aa71a0a17c8808ef..73eff27343618cca0737d94c6abd79ac3b52fa78 100644 (file)
@@ -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;
     }