diff options
author | Benjamin Peterson <benjamin@python.org> | 2009-10-04 20:32:25 (GMT) |
---|---|---|
committer | Benjamin Peterson <benjamin@python.org> | 2009-10-04 20:32:25 (GMT) |
commit | 0df35a93a2c53debf6d3ce00f022b79ea7892429 (patch) | |
tree | 4f560be43ff3796362d4c556bf3900880b6da35c /Modules/posixmodule.c | |
parent | 60e4cae06a8dd83f9689aaec32094bf199d481ad (diff) | |
download | cpython-0df35a93a2c53debf6d3ce00f022b79ea7892429.zip cpython-0df35a93a2c53debf6d3ce00f022b79ea7892429.tar.gz cpython-0df35a93a2c53debf6d3ce00f022b79ea7892429.tar.bz2 |
Merged revisions 74841 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r74841 | thomas.wouters | 2009-09-16 14:55:54 -0500 (Wed, 16 Sep 2009) | 23 lines
Fix issue #1590864, multiple threads and fork() can cause deadlocks, by
acquiring the import lock around fork() calls. This prevents other threads
from having that lock while the fork happens, and is the recommended way of
dealing with such issues. There are two other locks we care about, the GIL
and the Thread Local Storage lock. The GIL is obviously held when calling
Python functions like os.fork(), and the TLS lock is explicitly reallocated
instead, while also deleting now-orphaned TLS data.
This only fixes calls to os.fork(), not extension modules or embedding
programs calling C's fork() directly. Solving that requires a new set of API
functions, and possibly a rewrite of the Python/thread_*.c mess. Add a
warning explaining the problem to the documentation in the mean time.
This also changes behaviour a little on AIX. Before, AIX (but only AIX) was
getting the import lock reallocated, seemingly to avoid this very same
problem. This is not the right approach, because the import lock is a
re-entrant one, and reallocating would do the wrong thing when forking while
holding the import lock.
Will backport to 2.6, minus the tiny AIX behaviour change.
........
Diffstat (limited to 'Modules/posixmodule.c')
-rw-r--r-- | Modules/posixmodule.c | 34 |
1 files changed, 31 insertions, 3 deletions
diff --git a/Modules/posixmodule.c b/Modules/posixmodule.c index f767798..22b637e 100644 --- a/Modules/posixmodule.c +++ b/Modules/posixmodule.c @@ -3721,11 +3721,21 @@ Return 0 to child process and PID of child to parent process."); static PyObject * posix_fork1(PyObject *self, PyObject *noargs) { - pid_t pid = fork1(); + pid_t pid; + int result; + _PyImport_AcquireLock(); + pid = fork1(); + result = _PyImport_ReleaseLock(); if (pid == -1) return posix_error(); if (pid == 0) PyOS_AfterFork(); + if (result < 0) { + /* Don't clobber the OSError if the fork failed. */ + PyErr_SetString(PyExc_RuntimeError, + "not holding the import lock"); + return NULL; + } return PyLong_FromPid(pid); } #endif @@ -3740,11 +3750,21 @@ Return 0 to child process and PID of child to parent process."); static PyObject * posix_fork(PyObject *self, PyObject *noargs) { - pid_t pid = fork(); + pid_t pid; + int result; + _PyImport_AcquireLock(); + pid = fork(); + result = _PyImport_ReleaseLock(); if (pid == -1) return posix_error(); if (pid == 0) PyOS_AfterFork(); + if (result < 0) { + /* Don't clobber the OSError if the fork failed. */ + PyErr_SetString(PyExc_RuntimeError, + "not holding the import lock"); + return NULL; + } return PyLong_FromPid(pid); } #endif @@ -3847,14 +3867,22 @@ To both, return fd of newly opened pseudo-terminal.\n"); static PyObject * posix_forkpty(PyObject *self, PyObject *noargs) { - int master_fd = -1; + int master_fd = -1, result; pid_t pid; + _PyImport_AcquireLock(); pid = forkpty(&master_fd, NULL, NULL, NULL); + result = _PyImport_ReleaseLock(); if (pid == -1) return posix_error(); if (pid == 0) PyOS_AfterFork(); + if (result < 0) { + /* Don't clobber the OSError if the fork failed. */ + PyErr_SetString(PyExc_RuntimeError, + "not holding the import lock"); + return NULL; + } return Py_BuildValue("(Ni)", PyLong_FromPid(pid), master_fd); } #endif |