]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-116738: Make mmap.set_name thread-safe (#145555)
authorAN Long <aisk@users.noreply.github.com>
Sat, 7 Mar 2026 08:14:44 +0000 (17:14 +0900)
committerGitHub <noreply@github.com>
Sat, 7 Mar 2026 08:14:44 +0000 (17:14 +0900)
* Add critical section around mmap.set_name to make it thread-safe

* Add news entry

* Apply suggestion from @aisk

Misc/NEWS.d/next/Core_and_Builtins/2026-03-06-01-36-20.gh-issue-116738.OWVWRx.rst [new file with mode: 0644]
Modules/clinic/mmapmodule.c.h
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 (file)
index 0000000..212fd7b
--- /dev/null
@@ -0,0 +1,2 @@
+Make :meth:`!mmap.mmap.set_name` thread-safe on the :term:`free threaded <free
+threading>` build.
index db640800ad780fb417bf6476d23e5837366536f9..98c5bf6a2fb146205f5f5c18511bf2fa1dfee50a 100644 (file)
@@ -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 @@ exit:
 #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]*/
index 16e3c0ecefd05dd92a8dd012d2af9540d04e0f64..61d8a043a04ce22c6b87320a003d4b73c870b82a 100644 (file)
@@ -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:";