[Python-checkins] GH-94736: Fix _multiprocessing.SemLock subclassing (#94738)

pablogsal webhook-mailer at python.org
Mon Jul 11 08:12:54 EDT 2022


https://github.com/python/cpython/commit/f5b76330cfb93e1ad1a77c71dafe719f6a808cec
commit: f5b76330cfb93e1ad1a77c71dafe719f6a808cec
branch: main
author: Kumar Aditya <59607654+kumaraditya303 at users.noreply.github.com>
committer: pablogsal <Pablogsal at gmail.com>
date: 2022-07-11T13:12:36+01:00
summary:

GH-94736: Fix _multiprocessing.SemLock subclassing (#94738)

* fix allocator and deallocator

* 📜🤖 Added by blurb_it.

* code review

Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>

files:
A Misc/NEWS.d/next/Library/2022-07-11-10-41-48.gh-issue-94736.EbsgeK.rst
M Lib/test/_test_multiprocessing.py
M Modules/_multiprocessing/semaphore.c

diff --git a/Lib/test/_test_multiprocessing.py b/Lib/test/_test_multiprocessing.py
index 75969975af35b..4c4b9ac532396 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
+
+
+ at 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/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 0000000000000..3080672ecdfbb
--- /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.
diff --git a/Modules/_multiprocessing/semaphore.c b/Modules/_multiprocessing/semaphore.c
index 8607476aff10f..f5fd3257f066a 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]



More information about the Python-checkins mailing list