Bugs item #1223238, was opened at 2005-06-18 18:37
Message generated for change (Comment added) made by birkenfeld
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1223238&group_id=5470

Please note that this message will contain a full copy of the comment thread,
including the initial issue submission, for this request,
not just the latest update.
Category: Python Library
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Mattias EngdegÄrd (yorick)
Assigned to: Nobody/Anonymous (nobody)
Summary: race in os.makedirs()

Initial Comment:
os.makedirs() can fail if one of its components is
created while it is running (perhaps by another
process). This is because it checks for each directory
if it exists before creating it.

This is bad programming style. A correct implementation
would just call mkdir() on each directory (starting
with the rightmost, probably) and ignoring any EEXIST
error. This would not only fix the bug, it would also
be faster (fewer syscalls).

The patch is simple, but there is a wart in the design:
os.makedirs() throws an error if the (rightmost)
directory already exists, although by calling this
function the user has clearly indicated that she wants
the directories to be created if they don't exist and
have no complaints otherwise.

This leads to code like:

try:
    os.makedirs(path)
except OSError:
    pass

which is doubly bad because it hides the race condition!

So, before I submit a patch, should we:
a) just fix this bug but keep the old design
b) fix this bug, and don't throw an error if the dir exists

or maybe do a) for the next 2.4.x bugfix release and b)
in 2.5?


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

>Comment By: Reinhold Birkenfeld (birkenfeld)
Date: 2005-07-17 21:56

Message:
Logged In: YES 
user_id=1188172

See patch #1239890.

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

Comment By: Mattias EngdegÄrd (yorick)
Date: 2005-06-25 23:11

Message:
Logged In: YES 
user_id=432579

I'm fine with fixing the design for 2.5 and ignoring the bug for 2.4, since 
programs susceptible to the bug must use some kind of work-around in 
2.4.x (x < 2) anyway.
What I am using right now is:

def makedirs(name, mode=0777):
    try:
        os.mkdir(name, mode)
        return
    except OSError, err:
        if err.errno == errno.EEXIST:
            return
        if err.errno != errno.ENOENT:
            raise
    makedirs(os.path.dirname(name), mode)
    makedirs(name, mode)

This is compact and elegant, but relies on mkdir producing the correct 
errno values, which should be true for all platforms I'm aware of. It could 
also theoretically loop infinitely in bizarre cases but I don't see how that 
ever could happen.


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

Comment By: Neil Schemenauer (nascheme)
Date: 2005-06-18 19:43

Message:
Logged In: YES 
user_id=35752

I vote to fix the design for 2.5.  Backporting the minimal
fix to 2.4 would be optional, IMO.

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

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1223238&group_id=5470
_______________________________________________
Python-bugs-list mailing list 
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to