diff --git a/Lib/test/test_free_threading/test_winreg.py b/Lib/test/test_free_threading/test_winreg.py new file mode 100644 index 00000000000000..b7382fd029d050 --- /dev/null +++ b/Lib/test/test_free_threading/test_winreg.py @@ -0,0 +1,50 @@ +import unittest + +from test.support import import_helper, threading_helper +from test.support.threading_helper import run_concurrently + +winreg = import_helper.import_module("winreg") + +NTHREADS = 4 +NKEYS = 1000 + + +def open_keys(): + return [winreg.OpenKey(winreg.HKEY_CURRENT_USER, "Software") + for _ in range(NKEYS)] + + +@threading_helper.requires_working_threading() +class WinregTests(unittest.TestCase): + def test_close(self): + keys = open_keys() + + def close(close_func): + for key in keys: + close_func(key) + + run_concurrently(close, nthreads=NTHREADS, + args=(winreg.HKEYType.Close,)) + keys = open_keys() + run_concurrently(close, nthreads=NTHREADS, args=(winreg.CloseKey,)) + + def test_detach_and_close(self): + keys = open_keys() + detached = [] + + def detach(): + for key in keys: + detached.append(key.Detach()) + + def close(): + for key in keys: + key.Close() + + run_concurrently([detach, close]) + for handle in detached: + if handle: + winreg.CloseKey(handle) + + +if __name__ == "__main__": + unittest.main() diff --git a/Misc/NEWS.d/next/Windows/2026-11-26-00-36-30.gh-issue-116738.OY94SQ.rst b/Misc/NEWS.d/next/Windows/2026-11-26-00-36-30.gh-issue-116738.OY94SQ.rst new file mode 100644 index 00000000000000..10670e897ddea3 --- /dev/null +++ b/Misc/NEWS.d/next/Windows/2026-11-26-00-36-30.gh-issue-116738.OY94SQ.rst @@ -0,0 +1,2 @@ +Fix a race condition in :meth:`winreg.PyHKEY.Close` and +:meth:`winreg.PyHKEY.Detach` on the :term:`free-threaded build`. diff --git a/PC/winreg.c b/PC/winreg.c index 417e9a9843375c..4bfba8ea195028 100644 --- a/PC/winreg.c +++ b/PC/winreg.c @@ -15,6 +15,7 @@ #include "Python.h" #include "pycore_object.h" // _PyObject_Init() #include "pycore_moduleobject.h" +#include "pycore_pyatomic_ft_wrappers.h" // FT_ATOMIC_LOAD_PTR_RELAXED() #include @@ -167,21 +168,22 @@ PyHKEY_deallocFunc(PyObject *ob) static int PyHKEY_boolFunc(PyObject *ob) { - return ((PyHKEYObject *)ob)->hkey != 0; + return FT_ATOMIC_LOAD_PTR_RELAXED(((PyHKEYObject *)ob)->hkey) != 0; } static PyObject * PyHKEY_intFunc(PyObject *ob) { PyHKEYObject *pyhkey = (PyHKEYObject *)ob; - return PyLong_FromVoidPtr(pyhkey->hkey); + return PyLong_FromVoidPtr(FT_ATOMIC_LOAD_PTR_RELAXED(pyhkey->hkey)); } static PyObject * PyHKEY_strFunc(PyObject *ob) { PyHKEYObject *pyhkey = (PyHKEYObject *)ob; - return PyUnicode_FromFormat("", pyhkey->hkey); + return PyUnicode_FromFormat("", + FT_ATOMIC_LOAD_PTR_RELAXED(pyhkey->hkey)); } static PyObject * @@ -194,8 +196,8 @@ PyHKEY_richcompare(PyObject *ob1, PyObject *ob2, int op) PyHKEYObject *pyhkey1 = (PyHKEYObject *)ob1; PyHKEYObject *pyhkey2 = (PyHKEYObject *)ob2; - HKEY hkey1 = pyhkey1->hkey; - HKEY hkey2 = pyhkey2->hkey; + HKEY hkey1 = FT_ATOMIC_LOAD_PTR_RELAXED(pyhkey1->hkey); + HKEY hkey2 = FT_ATOMIC_LOAD_PTR_RELAXED(pyhkey2->hkey); int result; switch (op) { @@ -310,11 +312,11 @@ winreg_HKEYType_Detach_impl(PyHKEYObject *self) /*[clinic end generated code: output=dda5a9e1a01ae78f input=dd2cc09e6c6ba833]*/ { void* ret; - if (PySys_Audit("winreg.PyHKEY.Detach", "n", (Py_ssize_t)self->hkey) < 0) { + if (PySys_Audit("winreg.PyHKEY.Detach", "n", + (Py_ssize_t)FT_ATOMIC_LOAD_PTR_RELAXED(self->hkey)) < 0) { return NULL; } - ret = (void*)self->hkey; - self->hkey = 0; + ret = _Py_atomic_exchange_ptr(&self->hkey, NULL); return PyLong_FromVoidPtr(ret); } @@ -420,11 +422,12 @@ PyHKEY_Close(winreg_state *st, PyObject *ob_handle) LONG rc; HKEY key; - if (!PyHKEY_AsHKEY(st, ob_handle, &key, TRUE)) { - return FALSE; - } if (PyHKEY_Check(st, ob_handle)) { - ((PyHKEYObject*)ob_handle)->hkey = 0; + key = _Py_atomic_exchange_ptr(&((PyHKEYObject *)ob_handle)->hkey, + NULL); + } + else if (!PyHKEY_AsHKEY(st, ob_handle, &key, TRUE)) { + return FALSE; } Py_BEGIN_ALLOW_THREADS rc = key ? RegCloseKey(key) : ERROR_SUCCESS; @@ -448,7 +451,7 @@ PyHKEY_AsHKEY(winreg_state *st, PyObject *ob, HKEY *pHANDLE, BOOL bNoneOK) } else if (PyHKEY_Check(st ,ob)) { PyHKEYObject *pH = (PyHKEYObject *)ob; - *pHANDLE = pH->hkey; + *pHANDLE = FT_ATOMIC_LOAD_PTR_RELAXED(pH->hkey); } else if (PyLong_Check(ob)) { /* We also support integers */