Blame SOURCES/00306-fix-oserror-17-upon-semaphores-creation.patch

925e6b
925e6b
# HG changeset patch
925e6b
# User Charles-François Natali <cf.natali@gmail.com>
925e6b
# Date 1455316761 0
925e6b
# Node ID d3662c088db8fb2c89f754031f18b1543419fed9
925e6b
# Parent  5715a6d9ff12053e81f7ad75268ac059b079b351
925e6b
Issue #24303: Fix random EEXIST upon multiprocessing semaphores creation with
925e6b
Linux PID namespaces enabled.
925e6b
925e6b
diff --git a/Modules/_multiprocessing/semaphore.c b/Modules/_multiprocessing/semaphore.c
925e6b
--- a/Modules/_multiprocessing/semaphore.c
925e6b
+++ b/Modules/_multiprocessing/semaphore.c
925e6b
@@ -429,7 +429,7 @@ semlock_new(PyTypeObject *type, PyObject
925e6b
     int kind, maxvalue, value;
925e6b
     PyObject *result;
925e6b
     static char *kwlist[] = {"kind", "value", "maxvalue", NULL};
925e6b
-    static int counter = 0;
925e6b
+    int try = 0;
925e6b
 
925e6b
     if (!PyArg_ParseTupleAndKeywords(args, kwds, "iii", kwlist,
925e6b
                                      &kind, &value, &maxvalue))
925e6b
@@ -440,10 +440,18 @@ semlock_new(PyTypeObject *type, PyObject
925e6b
         return NULL;
925e6b
     }
925e6b
 
925e6b
-    PyOS_snprintf(buffer, sizeof(buffer), "/mp%ld-%d", (long)getpid(), counter++);
925e6b
+    /* Create a semaphore with a unique name. The bytes returned by
925e6b
+     * _PyOS_URandom() are treated as unsigned long to ensure that the filename
925e6b
+     * is valid (no special characters). */
925e6b
+    do {
925e6b
+        unsigned long suffix;
925e6b
+        _PyOS_URandom((char *)&suffix, sizeof(suffix));
925e6b
+        PyOS_snprintf(buffer, sizeof(buffer), "/mp%ld-%lu", (long)getpid(),
925e6b
+                      suffix);
925e6b
+        SEM_CLEAR_ERROR();
925e6b
+        handle = SEM_CREATE(buffer, value, maxvalue);
925e6b
+    } while ((handle == SEM_FAILED) && (errno == EEXIST) && (++try < 100));
925e6b
 
925e6b
-    SEM_CLEAR_ERROR();
925e6b
-    handle = SEM_CREATE(buffer, value, maxvalue);
925e6b
     /* On Windows we should fail if GetLastError()==ERROR_ALREADY_EXISTS */
925e6b
     if (handle == SEM_FAILED || SEM_GET_LAST_ERROR() != 0)
925e6b
         goto failure;
925e6b