Skip to content

Commit 916686f

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 b87d03d commit 916686f

3 files changed

Lines changed: 14 additions & 4 deletions

File tree

β€ŽLib/test/_test_multiprocessing.pyβ€Ž

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5964,3 +5964,14 @@ def tearDownModule():
59645964

59655965
remote_globs['setUpModule'] = setUpModule
59665966
remote_globs['tearDownModule'] = tearDownModule
5967+
5968+
5969+
@unittest.skipIf(not hasattr(_multiprocessing, 'SemLock'), 'SemLock not available')
5970+
class SemLockTests(unittest.TestCase):
5971+
5972+
def test_semlock_subclass(self):
5973+
class SemLock(_multiprocessing.SemLock):
5974+
pass
5975+
name = f'test_semlock_subclass-{os.getpid()}'
5976+
s = SemLock(1, 0, 10, name, 0)
5977+
_multiprocessing.sem_unlink(name)
Lines changed: 1 addition & 0 deletions
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β€Ž

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -452,9 +452,7 @@ static PyObject *
452452
newsemlockobject(PyTypeObject *type, SEM_HANDLE handle, int kind, int maxvalue,
453453
char *name)
454454
{
455-
SemLockObject *self;
456-
457-
self = PyObject_New(SemLockObject, type);
455+
SemLockObject *self = (SemLockObject *)type->tp_alloc(type, 0);
458456
if (!self)
459457
return NULL;
460458
self->handle = handle;
@@ -571,7 +569,7 @@ semlock_dealloc(SemLockObject* self)
571569
if (self->handle != SEM_FAILED)
572570
SEM_CLOSE(self->handle);
573571
PyMem_Free(self->name);
574-
PyObject_Free(self);
572+
Py_TYPE(self)->tp_free((PyObject*)self);
575573
}
576574

577575
/*[clinic input]

0 commit comments

Comments
Β (0)