71084d
diff --git a/Include/pythread.h b/Include/pythread.h
71084d
index dfd6157..f3e6259 100644
71084d
--- a/Include/pythread.h
71084d
+++ b/Include/pythread.h
71084d
@@ -30,6 +30,8 @@ PyAPI_FUNC(void) PyThread_delete_key(int);
71084d
 PyAPI_FUNC(int) PyThread_set_key_value(int, void *);
71084d
 PyAPI_FUNC(void *) PyThread_get_key_value(int);
71084d
 PyAPI_FUNC(void) PyThread_delete_key_value(int key);
71084d
+PyAPI_FUNC(int) _PyThread_AcquireKeyLock(void);
71084d
+PyAPI_FUNC(void) _PyThread_ReleaseKeyLock(void);
71084d
 
71084d
 /* Cleanup after a fork */
71084d
 PyAPI_FUNC(void) PyThread_ReInitTLS(void);
71084d
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c
71084d
index 022d7aa..8f6cbb2 100644
71084d
--- a/Modules/posixmodule.c
71084d
+++ b/Modules/posixmodule.c
71084d
@@ -65,6 +65,10 @@ corresponding Unix manual entries for more information on calls.");
71084d
 #include "osdefs.h"
71084d
 #endif
71084d
71084d
+#ifdef WITH_THREAD
71084d
+#include "pythread.h"
71084d
+#endif
71084d
+
71084d
 #ifdef HAVE_SYS_TYPES_H
71084d
 #include <sys/types.h>
71084d
 #endif /* HAVE_SYS_TYPES_H */
71084d
@@ -3796,7 +3800,18 @@ posix_fork1(PyObject *self, PyObject *noargs)
71084d
     pid_t pid;
71084d
     int result = 0;
71084d
     _PyImport_AcquireLock();
71084d
+#ifdef WITH_THREAD
71084d
+    if (_PyThread_AcquireKeyLock() == 0) {
71084d
+        _PyImport_ReleaseLock();
71084d
+        PyErr_SetString(PyExc_RuntimeError,
71084d
+                        "could not acquire thread key lock");
71084d
+        return NULL;
71084d
+    }
71084d
+#endif
71084d
     pid = fork1();
71084d
+#ifdef WITH_THREAD
71084d
+    _PyThread_ReleaseKeyLock();
71084d
+#endif
71084d
     if (pid == 0) {
71084d
         /* child: this clobbers and resets the import lock. */
71084d
         PyOS_AfterFork();
71084d
@@ -3829,7 +3844,18 @@ posix_fork(PyObject *self, PyObject *noargs)
71084d
     pid_t pid;
71084d
     int result = 0;
71084d
     _PyImport_AcquireLock();
71084d
+#ifdef WITH_THREAD
71084d
+    if (_PyThread_AcquireKeyLock() == 0) {
71084d
+        _PyImport_ReleaseLock();
71084d
+        PyErr_SetString(PyExc_RuntimeError,
71084d
+                        "could not acquire thread key lock");
71084d
+        return NULL;
71084d
+    }
71084d
+#endif
71084d
     pid = fork();
71084d
+#ifdef WITH_THREAD
71084d
+    _PyThread_ReleaseKeyLock();
71084d
+#endif
71084d
     if (pid == 0) {
71084d
         /* child: this clobbers and resets the import lock. */
71084d
         PyOS_AfterFork();
71084d
@@ -3955,7 +3981,18 @@ posix_forkpty(PyObject *self, PyObject *noargs)
71084d
     pid_t pid;
71084d
 
71084d
     _PyImport_AcquireLock();
71084d
+#ifdef WITH_THREAD
71084d
+    if (_PyThread_AcquireKeyLock() == 0) {
71084d
+        _PyImport_ReleaseLock();
71084d
+        PyErr_SetString(PyExc_RuntimeError,
71084d
+                        "could not acquire thread key lock");
71084d
+        return NULL;
71084d
+    }
71084d
+#endif
71084d
     pid = forkpty(&master_fd, NULL, NULL, NULL);
71084d
+#ifdef WITH_THREAD
71084d
+    _PyThread_ReleaseKeyLock();
71084d
+#endif
71084d
     if (pid == 0) {
71084d
         /* child: this clobbers and resets the import lock. */
71084d
         PyOS_AfterFork();
71084d
diff --git a/Python/thread.c b/Python/thread.c
71084d
index dd333e8..957739e 100644
71084d
--- a/Python/thread.c
71084d
+++ b/Python/thread.c
71084d
@@ -387,6 +387,24 @@ PyThread_delete_key_value(int key)
71084d
     PyThread_release_lock(keymutex);
71084d
 }
71084d
 
71084d
+int
71084d
+_PyThread_AcquireKeyLock(void)
71084d
+{
71084d
+    if (keymutex == NULL) {
71084d
+        keymutex = PyThread_allocate_lock();
71084d
+    }
71084d
+    if (keymutex == NULL) {
71084d
+        return 0;
71084d
+    }
71084d
+    return PyThread_acquire_lock(keymutex, 1);
71084d
+}
71084d
+
71084d
+void
71084d
+_PyThread_ReleaseKeyLock(void)
71084d
+{
71084d
+    PyThread_release_lock(keymutex);
71084d
+}
71084d
+
71084d
 /* Forget everything not associated with the current thread id.
71084d
  * This function is called from PyOS_AfterFork().  It is necessary
71084d
  * because other thread ids which were in use at the time of the fork