[Python-bugs-list] [ python-Bugs-509805 ] tempfile.gettempdir not threadsafe

noreply@sourceforge.net noreply@sourceforge.net
Mon, 28 Jan 2002 12:16:39 -0800


Bugs item #509805, was opened at 2002-01-28 12:13
You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=509805&group_id=5470

Category: Python Library
Group: Platform-specific
Status: Open
Resolution: None
Priority: 7
Submitted By: Tim Peters (tim_one)
Assigned to: Tim Peters (tim_one)
Summary: tempfile.gettempdir not threadsafe

Initial Comment:
tempfile.gettempdir() tests whether a candidate temp 
dir is usable by creating a file in it and writing to 
it.  If that succeeds, it unlinks the file and 
says "great".

Alas, the logic appears to be flawed in different ways 
for different platforms.  Examples:

+ Windows.  The name of the temp file is just the
  pid with "test" tacked on to the end.  *All* threads
  calling this see the same name.  So threads A and
  B can both open the test file at the same time,
  thread A closes it and tries to unlink it, and
  blows up then because thread B still has the file
  open (you can't unlink an open file on Windows).
  I've seen this happen <wink>.

+ Non-Linux Unix flavors.  Again all threads see
  the same file name.  The test file is opened with
      os.O_RDWR | os.O_CREAT | os.O_EXCL
  so if thread A gets in first, I expect all other
  threads will suffer a bogus error until thread A
  finishes unlinking it.  The consequece is that the
  other threads may be fooled into (erroneously)
  believing that there are no usable temp directories.

+ Linux.  Someone else needs to think about this.
  I expect it's safe on Linux because different
  threads have different pids (so don't collide
  on the test file name) -- although I expect it
  remains vulnerable to junk files sitting around
  that just happen to have the same name.

+ 'mac' and 'riscos':  no idea.

----------------------------------------------------------------------

You can respond by visiting: 
http://sourceforge.net/tracker/?func=detail&atid=105470&aid=509805&group_id=5470