]> granicus.if.org Git - python/commit
Fix issue #1590864, multiple threads and fork() can cause deadlocks, by
authorThomas Wouters <thomas@python.org>
Wed, 16 Sep 2009 19:55:54 +0000 (19:55 +0000)
committerThomas Wouters <thomas@python.org>
Wed, 16 Sep 2009 19:55:54 +0000 (19:55 +0000)
commitc4dcb3801e6e64327e95821034a7578f5f31bf94
treefc650093479f7ca906df8c8c4da04580bf0ca9fe
parent69dfe8d80ed77215b4bfe84138ef070d6c0c7b16
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.
Doc/c-api/init.rst
Include/import.h
Lib/test/test_fork1.py
Modules/posixmodule.c
Python/import.c