An interpreted, interactive, object-oriented programming language
CentOS Sources
2017-08-01 71084d584ff953f5463757ec6536406320560b4d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
diff --git a/Include/pythread.h b/Include/pythread.h
index dfd6157..f3e6259 100644
--- a/Include/pythread.h
+++ b/Include/pythread.h
@@ -30,6 +30,8 @@ PyAPI_FUNC(void) PyThread_delete_key(int);
 PyAPI_FUNC(int) PyThread_set_key_value(int, void *);
 PyAPI_FUNC(void *) PyThread_get_key_value(int);
 PyAPI_FUNC(void) PyThread_delete_key_value(int key);
+PyAPI_FUNC(int) _PyThread_AcquireKeyLock(void);
+PyAPI_FUNC(void) _PyThread_ReleaseKeyLock(void);
 
 /* Cleanup after a fork */
 PyAPI_FUNC(void) PyThread_ReInitTLS(void);
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
index 022d7aa..8f6cbb2 100644
--- a/Modules/posixmodule.c
+++ b/Modules/posixmodule.c
@@ -65,6 +65,10 @@ corresponding Unix manual entries for more information on calls.");
 #include "osdefs.h"
 #endif
 
+#ifdef WITH_THREAD
+#include "pythread.h"
+#endif
+
 #ifdef HAVE_SYS_TYPES_H
 #include <sys/types.h>
 #endif /* HAVE_SYS_TYPES_H */
@@ -3796,7 +3800,18 @@ posix_fork1(PyObject *self, PyObject *noargs)
     pid_t pid;
     int result = 0;
     _PyImport_AcquireLock();
+#ifdef WITH_THREAD
+    if (_PyThread_AcquireKeyLock() == 0) {
+        _PyImport_ReleaseLock();
+        PyErr_SetString(PyExc_RuntimeError,
+                        "could not acquire thread key lock");
+        return NULL;
+    }
+#endif
     pid = fork1();
+#ifdef WITH_THREAD
+    _PyThread_ReleaseKeyLock();
+#endif
     if (pid == 0) {
         /* child: this clobbers and resets the import lock. */
         PyOS_AfterFork();
@@ -3829,7 +3844,18 @@ posix_fork(PyObject *self, PyObject *noargs)
     pid_t pid;
     int result = 0;
     _PyImport_AcquireLock();
+#ifdef WITH_THREAD
+    if (_PyThread_AcquireKeyLock() == 0) {
+        _PyImport_ReleaseLock();
+        PyErr_SetString(PyExc_RuntimeError,
+                        "could not acquire thread key lock");
+        return NULL;
+    }
+#endif
     pid = fork();
+#ifdef WITH_THREAD
+    _PyThread_ReleaseKeyLock();
+#endif
     if (pid == 0) {
         /* child: this clobbers and resets the import lock. */
         PyOS_AfterFork();
@@ -3955,7 +3981,18 @@ posix_forkpty(PyObject *self, PyObject *noargs)
     pid_t pid;
 
     _PyImport_AcquireLock();
+#ifdef WITH_THREAD
+    if (_PyThread_AcquireKeyLock() == 0) {
+        _PyImport_ReleaseLock();
+        PyErr_SetString(PyExc_RuntimeError,
+                        "could not acquire thread key lock");
+        return NULL;
+    }
+#endif
     pid = forkpty(&master_fd, NULL, NULL, NULL);
+#ifdef WITH_THREAD
+    _PyThread_ReleaseKeyLock();
+#endif
     if (pid == 0) {
         /* child: this clobbers and resets the import lock. */
         PyOS_AfterFork();
diff --git a/Python/thread.c b/Python/thread.c
index dd333e8..957739e 100644
--- a/Python/thread.c
+++ b/Python/thread.c
@@ -387,6 +387,24 @@ PyThread_delete_key_value(int key)
     PyThread_release_lock(keymutex);
 }
 
+int
+_PyThread_AcquireKeyLock(void)
+{
+    if (keymutex == NULL) {
+        keymutex = PyThread_allocate_lock();
+    }
+    if (keymutex == NULL) {
+        return 0;
+    }
+    return PyThread_acquire_lock(keymutex, 1);
+}
+
+void
+_PyThread_ReleaseKeyLock(void)
+{
+    PyThread_release_lock(keymutex);
+}
+
 /* Forget everything not associated with the current thread id.
  * This function is called from PyOS_AfterFork().  It is necessary
  * because other thread ids which were in use at the time of the fork