Skip to content

Commit 8464e4a

Browse files
GH-94736: Fix _multiprocessing.SemLock subclassing (GH-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> (cherry picked from commit f5b7633) Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
1 parent 45896f2 commit 8464e4a

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

β€ŽLib/test/_test_multiprocessing.py

+11
Original file line numberDiff line numberDiff line change
@@ -6026,3 +6026,14 @@ def tearDownModule():
60266026

60276027
remote_globs['setUpModule'] = setUpModule
60286028
remote_globs['tearDownModule'] = tearDownModule
6029+
6030+
6031+
@unittest.skipIf(not hasattr(_multiprocessing, 'SemLock'), 'SemLock not available')
6032+
class SemLockTests(unittest.TestCase):
6033+
6034+
def test_semlock_subclass(self):
6035+
class SemLock(_multiprocessing.SemLock):
6036+
pass
6037+
name = f'test_semlock_subclass-{os.getpid()}'
6038+
s = SemLock(1, 0, 10, name, 0)
6039+
_multiprocessing.sem_unlink(name)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix crash when deallocating an instance of a subclass of ``_multiprocessing.SemLock``. Patch by Kumar Aditya.

β€ŽModules/_multiprocessing/semaphore.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -454,9 +454,7 @@ static PyObject *
454454
newsemlockobject(PyTypeObject *type, SEM_HANDLE handle, int kind, int maxvalue,
455455
char *name)
456456
{
457-
SemLockObject *self;
458-
459-
self = PyObject_New(SemLockObject, type);
457+
SemLockObject *self = (SemLockObject *)type->tp_alloc(type, 0);
460458
if (!self)
461459
return NULL;
462460
self->handle = handle;
@@ -573,7 +571,7 @@ semlock_dealloc(SemLockObject* self)
573571
if (self->handle != SEM_FAILED)
574572
SEM_CLOSE(self->handle);
575573
PyMem_Free(self->name);
576-
PyObject_Free(self);
574+
Py_TYPE(self)->tp_free((PyObject*)self);
577575
}
578576

579577
/*[clinic input]

0 commit comments

Comments
Β (0)