From 27f0a32d3db963efde7e3995036f3b4b72583cdf Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Mon, 11 Jul 2022 10:38:25 +0000 Subject: [PATCH 1/3] fix allocator and deallocator --- Lib/test/_test_multiprocessing.py | 11 +++++++++++ Modules/_multiprocessing/semaphore.c | 7 +++---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/Lib/test/_test_multiprocessing.py b/Lib/test/_test_multiprocessing.py index 75969975af35ba..4c4b9ac5323961 100644 --- a/Lib/test/_test_multiprocessing.py +++ b/Lib/test/_test_multiprocessing.py @@ -6020,3 +6020,14 @@ def tearDownModule(): remote_globs['setUpModule'] = setUpModule remote_globs['tearDownModule'] = tearDownModule + + +@unittest.skipIf(not hasattr(_multiprocessing, 'SemLock'), 'SemLock not available') +class SemLockTests(unittest.TestCase): + + def test_semlock_subclass(self): + class SemLock(_multiprocessing.SemLock): + pass + name = f'test_semlock_subclass-{os.getpid()}' + s = SemLock(1, 0, 10, name, 0) + _multiprocessing.sem_unlink(name) diff --git a/Modules/_multiprocessing/semaphore.c b/Modules/_multiprocessing/semaphore.c index 8607476aff10ff..af74ec47cfeaf2 100644 --- a/Modules/_multiprocessing/semaphore.c +++ b/Modules/_multiprocessing/semaphore.c @@ -454,9 +454,7 @@ static PyObject * newsemlockobject(PyTypeObject *type, SEM_HANDLE handle, int kind, int maxvalue, char *name) { - SemLockObject *self; - - self = PyObject_New(SemLockObject, type); + SemLockObject *self = (SemLockObject *)type->tp_alloc(type, 0); if (!self) return NULL; self->handle = handle; @@ -573,7 +571,7 @@ semlock_dealloc(SemLockObject* self) if (self->handle != SEM_FAILED) SEM_CLOSE(self->handle); PyMem_Free(self->name); - PyObject_Free(self); + Py_TYPE(self)->tp_free((PyObject*)self); } /*[clinic input] @@ -780,6 +778,7 @@ PyTypeObject _PyMp_SemLockType = { /* tp_init */ 0, /* tp_alloc */ 0, /* tp_new */ _multiprocessing_SemLock, + .tp_free = PyObject_Free, }; /* From 699d81d637a81ad3a7f79897f75e395d8399e410 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Mon, 11 Jul 2022 10:41:49 +0000 Subject: [PATCH 2/3] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../next/Library/2022-07-11-10-41-48.gh-issue-94736.EbsgeK.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Library/2022-07-11-10-41-48.gh-issue-94736.EbsgeK.rst diff --git a/Misc/NEWS.d/next/Library/2022-07-11-10-41-48.gh-issue-94736.EbsgeK.rst b/Misc/NEWS.d/next/Library/2022-07-11-10-41-48.gh-issue-94736.EbsgeK.rst new file mode 100644 index 00000000000000..3080672ecdfbb8 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2022-07-11-10-41-48.gh-issue-94736.EbsgeK.rst @@ -0,0 +1 @@ +Fix crash when deallocating an instance of a subclass of ``_multiprocessing.SemLock``. Patch by Kumar Aditya. From 4e2f2a06b1def284e8fd644aab2e20bfb6b9a474 Mon Sep 17 00:00:00 2001 From: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com> Date: Mon, 11 Jul 2022 11:16:58 +0000 Subject: [PATCH 3/3] code review --- Modules/_multiprocessing/semaphore.c | 1 - 1 file changed, 1 deletion(-) diff --git a/Modules/_multiprocessing/semaphore.c b/Modules/_multiprocessing/semaphore.c index af74ec47cfeaf2..f5fd3257f066a3 100644 --- a/Modules/_multiprocessing/semaphore.c +++ b/Modules/_multiprocessing/semaphore.c @@ -778,7 +778,6 @@ PyTypeObject _PyMp_SemLockType = { /* tp_init */ 0, /* tp_alloc */ 0, /* tp_new */ _multiprocessing_SemLock, - .tp_free = PyObject_Free, }; /*