--- a/Modules/posixmodule.c +++ a/Modules/posixmodule.c @@ -164,6 +164,15 @@ corresponding Unix manual entries for more information on calls."); #ifndef _MSC_VER +#ifdef WITH_THREAD +typedef void *PyThread_type_lock; + +extern PyThread_type_lock PyThread_allocate_lock(void); +extern int PyThread_acquire_lock(PyThread_type_lock, int); +extern void PyThread_release_lock(PyThread_type_lock); +PyThread_type_lock keymutex; +#endif + #if defined(__sgi)&&_COMPILER_VERSION>=700 /* declare ctermid_r if compiling with MIPSPro 7.x in ANSI C mode (default) */ @@ -350,7 +359,6 @@ extern int lstat(const char *, struct stat *); #endif #endif - #ifndef MS_WINDOWS PyObject * _PyInt_FromUid(uid_t uid) @@ -3796,7 +3804,15 @@ posix_fork1(PyObject *self, PyObject *noargs) pid_t pid; int result = 0; _PyImport_AcquireLock(); +#ifdef WITH_THREAD + if (keymutex == NULL) + keymutex = PyThread_allocate_lock(); + PyThread_acquire_lock(keymutex, 1); +#endif pid = fork1(); +#ifdef WITH_THREAD + PyThread_release_lock(keymutex); +#endif if (pid == 0) { /* child: this clobbers and resets the import lock. */ PyOS_AfterFork(); @@ -3829,7 +3845,15 @@ posix_fork(PyObject *self, PyObject *noargs) pid_t pid; int result = 0; _PyImport_AcquireLock(); +#ifdef WITH_THREAD + if (keymutex == NULL) + keymutex = PyThread_allocate_lock(); + PyThread_acquire_lock(keymutex, 1); +#endif pid = fork(); +#ifdef WITH_THREAD + PyThread_release_lock(keymutex); +#endif if (pid == 0) { /* child: this clobbers and resets the import lock. */ PyOS_AfterFork(); @@ -3955,7 +3979,15 @@ posix_forkpty(PyObject *self, PyObject *noargs) pid_t pid; _PyImport_AcquireLock(); +#ifdef WITH_THREAD + if (keymutex == NULL) + keymutex = PyThread_allocate_lock(); + PyThread_acquire_lock(keymutex, 1); +#endif pid = forkpty(&master_fd, NULL, NULL, NULL); +#ifdef WITH_THREAD + PyThread_release_lock(keymutex); +#endif if (pid == 0) { /* child: this clobbers and resets the import lock. */ PyOS_AfterFork(); --- a/Python/thread.c +++ a/Python/thread.c @@ -233,7 +233,7 @@ struct key { }; static struct key *keyhead = NULL; -static PyThread_type_lock keymutex = NULL; +PyThread_type_lock keymutex = NULL; static int nkeys = 0; /* PyThread_create_key() hands out nkeys+1 next */ /* Internal helper.