From 9c8eade20c6c6cc6f31dffb5e42472391d63bbf4 Mon Sep 17 00:00:00 2001 From: "Miss Islington (bot)" <31488909+miss-islington@users.noreply.github.com> Date: Mon, 27 Oct 2025 21:05:10 +0100 Subject: [PATCH] [3.13] gh-140634: Fix a reference counting bug in os.sched_param.__reduce__() (GH-140667) (GH-140686) (cherry picked from commit 364ae607d8035db8ba92486ebebd8225446c1a90) Co-authored-by: Serhiy Storchaka --- Lib/test/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_posix.py b/Lib/test/test_posix.py index c327d2add2f1..ffda92ae53e9 100644 --- a/Lib/test/test_posix.py +++ b/Lib/test/test_posix.py @@ -1366,6 +1366,14 @@ class PosixTester(unittest.TestCase): 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 000000000000..b1ba9b26ad54 --- /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:`!os.sched_param.__reduce__`. diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index 9c1564093b96..13c2c1fd6787 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -8175,7 +8175,7 @@ os_sched_param_impl(PyTypeObject *type, PyObject *sched_priority) static PyObject * os_sched_param_reduce(PyObject *self, PyObject *Py_UNUSED(ignored)) { - 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 = { -- 2.47.3