diff options
author | Tim Peters <tim.peters@gmail.com> | 2002-01-28 23:11:23 (GMT) |
---|---|---|
committer | Tim Peters <tim.peters@gmail.com> | 2002-01-28 23:11:23 (GMT) |
commit | 4fd5a06580c2b03f0252553e4024f168ea054353 (patch) | |
tree | b52b5b960c7a0b384c7fa6e22cb9c65fbe72222e /Lib | |
parent | fea155370e00e58c694392dd79d47e9481897b4f (diff) | |
download | cpython-4fd5a06580c2b03f0252553e4024f168ea054353.zip cpython-4fd5a06580c2b03f0252553e4024f168ea054353.tar.gz cpython-4fd5a06580c2b03f0252553e4024f168ea054353.tar.bz2 |
SF bug #509805 tempfile.gettempdir not threadsafe
This is an ancient race when multiple threads call gettempdir() (or
anything relying on it) for the first time.
Fixed x-platform via the Big Hammer of rearranging the code to serialize
the first calls. Subsequent calls are as fast as before.
Note that the Python test suite can't provoke this bug: it requires
setting up multiple threads making the very first calls into tempfile,
but the test suite uses tempfile several times before getting to
test_threadedtempfile.
Bugfix candidate.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/tempfile.py | 30 |
1 files changed, 28 insertions, 2 deletions
diff --git a/Lib/tempfile.py b/Lib/tempfile.py index 417b749..51e43b0 100644 --- a/Lib/tempfile.py +++ b/Lib/tempfile.py @@ -17,6 +17,30 @@ def gettempdir(): global tempdir if tempdir is not None: return tempdir + + # _gettempdir_inner deduces whether a candidate temp dir is usable by + # trying to create a file in it, and write to it. If that succeeds, + # great, it closes the file and unlinks it. There's a race, though: + # the *name* of the test file it tries is the same across all threads + # under most OSes (Linux is an exception), and letting multiple threads + # all try to open, write to, close, and unlink a single file can cause + # a variety of bogus errors (e.g., you cannot unlink a file under + # Windows if anyone has it open, and two threads cannot create the + # same file in O_EXCL mode under Unix). The simplest cure is to serialize + # calls to _gettempdir_inner. This isn't a real expense, because the + # first thread to succeed sets the global tempdir, and all subsequent + # calls to gettempdir() reuse that without trying _gettempdir_inner. + _tempdir_lock.acquire() + try: + return _gettempdir_inner() + finally: + _tempdir_lock.release() + +def _gettempdir_inner(): + """Function to calculate the directory to use.""" + global tempdir + if tempdir is not None: + return tempdir try: pwd = os.getcwd() except (AttributeError, os.error): @@ -179,8 +203,8 @@ def TemporaryFile(mode='w+b', bufsize=-1, suffix=""): # multiple threads will never see the same integer). The integer will # usually be a Python int, but if _counter.get_next() is called often # enough, it will become a Python long. -# Note that the only name that survives this next block of code -# is "_counter". +# Note that the only names that survive this next block of code +# are "_counter" and "_tempdir_lock". class _ThreadSafeCounter: def __init__(self, mutex, initialvalue=0): @@ -209,10 +233,12 @@ except ImportError: release = acquire _counter = _ThreadSafeCounter(_DummyMutex()) + _tempdir_lock = _DummyMutes() del _DummyMutex else: _counter = _ThreadSafeCounter(thread.allocate_lock()) + _tempdir_lock = thread.allocate_lock() del thread del _ThreadSafeCounter |