https://github.com/python/cpython/commit/46761c976060c3e9ddbd33ea64601b54a97ddbe0
commit: 46761c976060c3e9ddbd33ea64601b54a97ddbe0
branch: main
author: AN Long <[email protected]>
committer: corona10 <[email protected]>
date: 2026-03-07T17:14:44+09:00
summary:

gh-116738: Make mmap.set_name thread-safe (#145555)

* Add critical section around mmap.set_name to make it thread-safe

* Add news entry

* Apply suggestion from @aisk

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2026-03-06-01-36-20.gh-issue-116738.OWVWRx.rst
M Modules/clinic/mmapmodule.c.h
M Modules/mmapmodule.c

diff --git 
a/Misc/NEWS.d/next/Core_and_Builtins/2026-03-06-01-36-20.gh-issue-116738.OWVWRx.rst
 
b/Misc/NEWS.d/next/Core_and_Builtins/2026-03-06-01-36-20.gh-issue-116738.OWVWRx.rst
new file mode 100644
index 00000000000000..212fd7b02902e9
--- /dev/null
+++ 
b/Misc/NEWS.d/next/Core_and_Builtins/2026-03-06-01-36-20.gh-issue-116738.OWVWRx.rst
@@ -0,0 +1,2 @@
+Make :meth:`!mmap.mmap.set_name` thread-safe on the :term:`free threaded <free
+threading>` build.
diff --git a/Modules/clinic/mmapmodule.c.h b/Modules/clinic/mmapmodule.c.h
index db640800ad780f..98c5bf6a2fb146 100644
--- a/Modules/clinic/mmapmodule.c.h
+++ b/Modules/clinic/mmapmodule.c.h
@@ -556,7 +556,9 @@ mmap_mmap_set_name(PyObject *self, PyObject *arg)
         PyErr_SetString(PyExc_ValueError, "embedded null character");
         goto exit;
     }
+    Py_BEGIN_CRITICAL_SECTION(self);
     return_value = mmap_mmap_set_name_impl((mmap_object *)self, name);
+    Py_END_CRITICAL_SECTION();
 
 exit:
     return return_value;
@@ -879,4 +881,4 @@ mmap_mmap_madvise(PyObject *self, PyObject *const *args, 
Py_ssize_t nargs)
 #ifndef MMAP_MMAP_MADVISE_METHODDEF
     #define MMAP_MMAP_MADVISE_METHODDEF
 #endif /* !defined(MMAP_MMAP_MADVISE_METHODDEF) */
-/*[clinic end generated code: output=8389e3c8e3db3a78 input=a9049054013a1b77]*/
+/*[clinic end generated code: output=1122b93314aebc5c input=a9049054013a1b77]*/
diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c
index 16e3c0ecefd05d..61d8a043a04ce2 100644
--- a/Modules/mmapmodule.c
+++ b/Modules/mmapmodule.c
@@ -1122,6 +1122,7 @@ mmap_mmap_seek_impl(mmap_object *self, Py_ssize_t dist, 
int how)
 }
 
 /*[clinic input]
+@critical_section
 mmap.mmap.set_name
 
     name: str
@@ -1131,7 +1132,7 @@ mmap.mmap.set_name
 
 static PyObject *
 mmap_mmap_set_name_impl(mmap_object *self, const char *name)
-/*[clinic end generated code: output=1edaf4fd51277760 input=6c7dd91cad205f07]*/
+/*[clinic end generated code: output=1edaf4fd51277760 input=7c0e2a17ca6d1adc]*/
 {
 #if defined(MAP_ANONYMOUS) && defined(__linux__)
     const char *prefix = "cpython:mmap:";

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to