From c1d89d188341208e7cefc7530a86ac007dfeff53 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 27 Oct 2025 13:49:34 +0200 Subject: [PATCH 1/2] gh-140634: Fix a reference counting bug in posix.sched_param.__reduce__() --- Lib/test/test_os/test_posix.py | 8 ++++++++ .../2025-10-27-13-49-31.gh-issue-140634.ULng9G.rst | 1 + Modules/posixmodule.c | 2 +- 3 files changed, 10 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2025-10-27-13-49-31.gh-issue-140634.ULng9G.rst diff --git a/Lib/test/test_os/test_posix.py b/Lib/test/test_os/test_posix.py index de24719a1ca455..37da293a441e46 100644 --- a/Lib/test/test_os/test_posix.py +++ b/Lib/test/test_os/test_posix.py @@ -1427,6 +1427,14 @@ def test_sched_param(self): self.assertNotEqual(newparam, param) self.assertEqual(newparam.sched_priority, 0) + @requires_sched + def test_bug_140634(self): + sched_priority = float('inf') # any new reference + param = posix.sched_param(sched_priority) + param.__reduce__() + del sched_priority, param # should not crash + support.gc_collect() # just to be sure + @unittest.skipUnless(hasattr(posix, "sched_rr_get_interval"), "no function") def test_sched_rr_get_interval(self): try: diff --git a/Misc/NEWS.d/next/Library/2025-10-27-13-49-31.gh-issue-140634.ULng9G.rst b/Misc/NEWS.d/next/Library/2025-10-27-13-49-31.gh-issue-140634.ULng9G.rst new file mode 100644 index 00000000000000..fa1f17563fc909 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-10-27-13-49-31.gh-issue-140634.ULng9G.rst @@ -0,0 +1 @@ +Fix a reference counting bug in :meth:`posix.sched_param.__reduce__`. diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index a30712f75d5d06..50464b01efba31 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -8724,7 +8724,7 @@ os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority) static PyObject * os_sched_param_reduce(PyObject *self, PyObject *Py_UNUSED(dummy)) { - return Py_BuildValue("(O(N))", Py_TYPE(self), PyStructSequence_GetItem(self, 0)); + return Py_BuildValue("(O(O))", Py_TYPE(self), PyStructSequence_GetItem(self, 0)); } static PyMethodDef os_sched_param_reduce_method = { From 0de9adef2d60bf9ce9b0cfb7d1b10aea50c70fc9 Mon Sep 17 00:00:00 2001 From: Serhiy Storchaka Date: Mon, 27 Oct 2025 15:20:38 +0200 Subject: [PATCH 2/2] Update 2025-10-27-13-49-31.gh-issue-140634.ULng9G.rst --- .../next/Library/2025-10-27-13-49-31.gh-issue-140634.ULng9G.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2025-10-27-13-49-31.gh-issue-140634.ULng9G.rst b/Misc/NEWS.d/next/Library/2025-10-27-13-49-31.gh-issue-140634.ULng9G.rst index fa1f17563fc909..b1ba9b26ad5431 100644 --- a/Misc/NEWS.d/next/Library/2025-10-27-13-49-31.gh-issue-140634.ULng9G.rst +++ b/Misc/NEWS.d/next/Library/2025-10-27-13-49-31.gh-issue-140634.ULng9G.rst @@ -1 +1 @@ -Fix a reference counting bug in :meth:`posix.sched_param.__reduce__`. +Fix a reference counting bug in :meth:`!os.sched_param.__reduce__`.