Kaydet (Commit) 674a3cd2 authored tarafından Charles-François Natali's avatar Charles-François Natali

Issue #24303: Fix random EEXIST upon multiprocessing semaphores creation with

Linux PID namespaces enabled.
üst c665ff61
...@@ -50,6 +50,9 @@ Core and Builtins ...@@ -50,6 +50,9 @@ Core and Builtins
Library Library
------- -------
- Issue #24303: Fix random EEXIST upon multiprocessing semaphores creation with
Linux PID namespaces enabled.
- Issue #25698: Importing module if the stack is too deep no longer replaces - Issue #25698: Importing module if the stack is too deep no longer replaces
imported module with the empty one. imported module with the empty one.
......
...@@ -429,7 +429,7 @@ semlock_new(PyTypeObject *type, PyObject *args, PyObject *kwds) ...@@ -429,7 +429,7 @@ semlock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
int kind, maxvalue, value; int kind, maxvalue, value;
PyObject *result; PyObject *result;
static char *kwlist[] = {"kind", "value", "maxvalue", NULL}; static char *kwlist[] = {"kind", "value", "maxvalue", NULL};
static int counter = 0; int try = 0;
if (!PyArg_ParseTupleAndKeywords(args, kwds, "iii", kwlist, if (!PyArg_ParseTupleAndKeywords(args, kwds, "iii", kwlist,
&kind, &value, &maxvalue)) &kind, &value, &maxvalue))
...@@ -440,10 +440,18 @@ semlock_new(PyTypeObject *type, PyObject *args, PyObject *kwds) ...@@ -440,10 +440,18 @@ semlock_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
return NULL; return NULL;
} }
PyOS_snprintf(buffer, sizeof(buffer), "/mp%ld-%d", (long)getpid(), counter++); /* Create a semaphore with a unique name. The bytes returned by
* _PyOS_URandom() are treated as unsigned long to ensure that the filename
* is valid (no special characters). */
do {
unsigned long suffix;
_PyOS_URandom((char *)&suffix, sizeof(suffix));
PyOS_snprintf(buffer, sizeof(buffer), "/mp%ld-%lu", (long)getpid(),
suffix);
SEM_CLEAR_ERROR();
handle = SEM_CREATE(buffer, value, maxvalue);
} while ((handle == SEM_FAILED) && (errno == EEXIST) && (++try < 100));
SEM_CLEAR_ERROR();
handle = SEM_CREATE(buffer, value, maxvalue);
/* On Windows we should fail if GetLastError()==ERROR_ALREADY_EXISTS */ /* On Windows we should fail if GetLastError()==ERROR_ALREADY_EXISTS */
if (handle == SEM_FAILED || SEM_GET_LAST_ERROR() != 0) if (handle == SEM_FAILED || SEM_GET_LAST_ERROR() != 0)
goto failure; goto failure;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment