From 7d58fb9782cfa87de59927e94c1d0460c0af0087 Mon Sep 17 00:00:00 2001 From: manabeai Date: Sun, 22 Nov 2026 12:21:30 +0900 Subject: [PATCH 1/4] gh-162412: Fix a ctypes callback argument reference leak on Windows --- Lib/test/test_ctypes/test_callbacks.py | 52 +++++++++++++++++++ ...-11-22-12-20-43.gh-issue-162412.CtCbRf.rst | 1 + Modules/_ctypes/callbacks.c | 1 + 3 files changed, 54 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-11-22-12-20-43.gh-issue-162412.CtCbRf.rst diff --git a/Lib/test/test_ctypes/test_callbacks.py b/Lib/test/test_ctypes/test_callbacks.py index 7d746749130db25..c0c7745d919d621 100644 --- a/Lib/test/test_ctypes/test_callbacks.py +++ b/Lib/test/test_ctypes/test_callbacks.py @@ -141,6 +141,58 @@ def __del__(self): gc.collect() CFUNCTYPE(None)(lambda x=Nasty(): None) + @unittest.skipUnless(sys.platform == 'win32', + 'TryAddRef() is only used on Windows') + def test_argument_conversion_error_does_not_leak(self): + import weakref + + class PoisonedKey(str): + __hash__ = str.__hash__ + + def __eq__(self, other): + if str(other) == '_needs_com_addref_': + raise RuntimeError('injected lookup failure') + return str.__eq__(self, other) + + refs = [] + track = False + + def init(arg): + if track: + refs.append(weakref.ref(arg)) + + Arg = type('Arg', (ctypes.c_void_p,), { + '__init__': init, + PoisonedKey('_needs_com_addref_'): True, + }) + + # Verify that the dictionary lookup in TryAddRef() can fail. + with self.assertRaises(RuntimeError): + '_needs_com_addref_' in vars(Arg) + + calls = 0 + + def callback(arg): + nonlocal calls + calls += 1 + + c_callback = CFUNCTYPE(None, Arg)(callback) + source = Arg() + old_hook = sys.unraisablehook + try: + sys.unraisablehook = lambda unraisable: None + track = True + for _ in range(32): + c_callback(source) + finally: + track = False + sys.unraisablehook = old_hook + + gc.collect() + self.assertEqual(calls, 0) + self.assertEqual(len(refs), 32) + self.assertTrue(all(ref() is None for ref in refs)) + @unittest.skipUnless(hasattr(ctypes, 'WINFUNCTYPE'), 'ctypes.WINFUNCTYPE is required') def test_i38748_stackCorruption(self): diff --git a/Misc/NEWS.d/next/Library/2026-11-22-12-20-43.gh-issue-162412.CtCbRf.rst b/Misc/NEWS.d/next/Library/2026-11-22-12-20-43.gh-issue-162412.CtCbRf.rst new file mode 100644 index 000000000000000..2b51c2745177fb0 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-22-12-20-43.gh-issue-162412.CtCbRf.rst @@ -0,0 +1 @@ +Fix a reference leak in :mod:`ctypes` on Windows when converting a callback argument fails during the COM reference-count lookup. diff --git a/Modules/_ctypes/callbacks.c b/Modules/_ctypes/callbacks.c index e54111b8a4fab44..d95c5c5839d6ce6 100644 --- a/Modules/_ctypes/callbacks.c +++ b/Modules/_ctypes/callbacks.c @@ -181,6 +181,7 @@ static void _CallPythonObject(ctypes_state *st, args[i] = (PyObject *)obj; #ifdef MS_WIN32 if (TryAddRef(cnv, obj) < 0) { + Py_DECREF(obj); goto Error; } #endif From d82a6a43276502f9fa655d396e04bf855a6e1afc Mon Sep 17 00:00:00 2001 From: manabeai Date: Sun, 22 Nov 2026 12:33:06 +0900 Subject: [PATCH 2/4] Keep borrowed COM pointers out of wrappers on lookup failure --- Lib/test/test_ctypes/test_callbacks.py | 14 ++++++++++-- Modules/_ctypes/callbacks.c | 30 ++++++++++++++------------ 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/Lib/test/test_ctypes/test_callbacks.py b/Lib/test/test_ctypes/test_callbacks.py index c0c7745d919d621..3117b830a6aa7ec 100644 --- a/Lib/test/test_ctypes/test_callbacks.py +++ b/Lib/test/test_ctypes/test_callbacks.py @@ -142,7 +142,7 @@ def __del__(self): CFUNCTYPE(None)(lambda x=Nasty(): None) @unittest.skipUnless(sys.platform == 'win32', - 'TryAddRef() is only used on Windows') + 'COM callback argument handling is Windows-only') def test_argument_conversion_error_does_not_leak(self): import weakref @@ -155,18 +155,25 @@ def __eq__(self, other): return str.__eq__(self, other) refs = [] + finalized_values = [] track = False def init(arg): if track: refs.append(weakref.ref(arg)) + arg.temporary = True + + def finalize(arg): + if getattr(arg, 'temporary', False): + finalized_values.append(arg.value) Arg = type('Arg', (ctypes.c_void_p,), { '__init__': init, + '__del__': finalize, PoisonedKey('_needs_com_addref_'): True, }) - # Verify that the dictionary lookup in TryAddRef() can fail. + # Verify that the COM marker lookup can fail. with self.assertRaises(RuntimeError): '_needs_com_addref_' in vars(Arg) @@ -178,6 +185,8 @@ def callback(arg): c_callback = CFUNCTYPE(None, Arg)(callback) source = Arg() + # The finalizer records the pointer without dereferencing it. + source.value = 1 old_hook = sys.unraisablehook try: sys.unraisablehook = lambda unraisable: None @@ -192,6 +201,7 @@ def callback(arg): self.assertEqual(calls, 0) self.assertEqual(len(refs), 32) self.assertTrue(all(ref() is None for ref in refs)) + self.assertEqual(finalized_values, [None] * 32) @unittest.skipUnless(hasattr(ctypes, 'WINFUNCTYPE'), 'ctypes.WINFUNCTYPE is required') diff --git a/Modules/_ctypes/callbacks.c b/Modules/_ctypes/callbacks.c index d95c5c5839d6ce6..ea1fdd2c0be384c 100644 --- a/Modules/_ctypes/callbacks.c +++ b/Modules/_ctypes/callbacks.c @@ -83,22 +83,13 @@ PyType_Spec cthunk_spec = { * slower. */ static int -TryAddRef(PyObject *cnv, CDataObject *obj) +NeedsComAddRef(PyObject *cnv) { - IUnknown *punk; PyObject *attrdict = _PyType_GetDict((PyTypeObject *)cnv); if (!attrdict) { return 0; } - int r = PyDict_Contains(attrdict, &_Py_ID(_needs_com_addref_)); - if (r <= 0) { - return r; - } - - punk = *(IUnknown **)obj->b_ptr; - if (punk) - punk->lpVtbl->AddRef(punk); - return 0; + return PyDict_Contains(attrdict, &_Py_ID(_needs_com_addref_)); } #endif @@ -177,14 +168,25 @@ static void _CallPythonObject(ctypes_state *st, Py_DECREF(obj); goto Error; } - memcpy(obj->b_ptr, *pArgs, info->size); - args[i] = (PyObject *)obj; #ifdef MS_WIN32 - if (TryAddRef(cnv, obj) < 0) { + // Look up the marker before exposing the borrowed pointer to + // the wrapper's finalizer. + int needs_addref = NeedsComAddRef(cnv); + if (needs_addref < 0) { Py_DECREF(obj); goto Error; } #endif + memcpy(obj->b_ptr, *pArgs, info->size); +#ifdef MS_WIN32 + if (needs_addref) { + IUnknown *punk = *(IUnknown **)obj->b_ptr; + if (punk) { + punk->lpVtbl->AddRef(punk); + } + } +#endif + args[i] = (PyObject *)obj; } else { PyErr_Format(PyExc_TypeError, "cannot build parameter of type %R", cnv); From c4ca44f8a88b2f285c12fb95f19ced2fc6b11b00 Mon Sep 17 00:00:00 2001 From: manabeai Date: Sun, 22 Nov 2026 12:35:38 +0900 Subject: [PATCH 3/4] Remove redundant COM lookup comment --- Modules/_ctypes/callbacks.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/Modules/_ctypes/callbacks.c b/Modules/_ctypes/callbacks.c index ea1fdd2c0be384c..cebdea85e6a348e 100644 --- a/Modules/_ctypes/callbacks.c +++ b/Modules/_ctypes/callbacks.c @@ -169,8 +169,6 @@ static void _CallPythonObject(ctypes_state *st, goto Error; } #ifdef MS_WIN32 - // Look up the marker before exposing the borrowed pointer to - // the wrapper's finalizer. int needs_addref = NeedsComAddRef(cnv); if (needs_addref < 0) { Py_DECREF(obj); From ffeae3b55bfe57e43aecbfe76b2cc98bc302932e Mon Sep 17 00:00:00 2001 From: manabeai Date: Sun, 22 Nov 2026 12:55:20 +0900 Subject: [PATCH 4/4] Use a non-string key for the ctypes callback error test --- Lib/test/test_ctypes/test_callbacks.py | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/Lib/test/test_ctypes/test_callbacks.py b/Lib/test/test_ctypes/test_callbacks.py index 3117b830a6aa7ec..4e85eddabdfdc1f 100644 --- a/Lib/test/test_ctypes/test_callbacks.py +++ b/Lib/test/test_ctypes/test_callbacks.py @@ -146,13 +146,16 @@ def __del__(self): def test_argument_conversion_error_does_not_leak(self): import weakref - class PoisonedKey(str): - __hash__ = str.__hash__ + marker = '_needs_com_addref_' + + class PoisonedKey: + def __hash__(self): + return hash(marker) def __eq__(self, other): - if str(other) == '_needs_com_addref_': + if type(other) is str and other == marker: raise RuntimeError('injected lookup failure') - return str.__eq__(self, other) + return NotImplemented refs = [] finalized_values = [] @@ -167,15 +170,16 @@ def finalize(arg): if getattr(arg, 'temporary', False): finalized_values.append(arg.value) - Arg = type('Arg', (ctypes.c_void_p,), { - '__init__': init, - '__del__': finalize, - PoisonedKey('_needs_com_addref_'): True, - }) + with self.assertWarnsRegex(RuntimeWarning, 'non-string key'): + Arg = type('Arg', (ctypes.c_void_p,), { + '__init__': init, + '__del__': finalize, + PoisonedKey(): True, + }) # Verify that the COM marker lookup can fail. with self.assertRaises(RuntimeError): - '_needs_com_addref_' in vars(Arg) + marker in vars(Arg) calls = 0