]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-94736: Fix _multiprocessing.SemLock subclassing (GH-94738)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Mon, 11 Jul 2022 12:39:09 +0000 (05:39 -0700)
committerGitHub <noreply@github.com>
Mon, 11 Jul 2022 12:39:09 +0000 (05:39 -0700)
* fix allocator and deallocator

* ðŸ“œðŸ¤– Added by blurb_it.

* code review

Co-authored-by: blurb-it[bot] <43283697+blurb-it[bot]@users.noreply.github.com>
(cherry picked from commit f5b76330cfb93e1ad1a77c71dafe719f6a808cec)

Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
Lib/test/_test_multiprocessing.py
Misc/NEWS.d/next/Library/2022-07-11-10-41-48.gh-issue-94736.EbsgeK.rst [new file with mode: 0644]
Modules/_multiprocessing/semaphore.c

index ba488b17920ef7e4012c40860fb1666357d10f46..08eea47441675474f45c6e2abd4f0c2644d1baeb 100644 (file)
@@ -5964,3 +5964,14 @@ def install_tests_in_module_dict(remote_globs, start_method):
 
     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/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 (file)
index 0000000..3080672
--- /dev/null
@@ -0,0 +1 @@
+Fix crash when deallocating an instance of a subclass of ``_multiprocessing.SemLock``. Patch by Kumar Aditya.
index 9a2d1f85c92fa27c4bb7357d7923275a1c03116c..3356658fef189cf5939c58e2cd5cb9a58715685b 100644 (file)
@@ -452,9 +452,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;
@@ -571,7 +569,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]