microsoft / knack

Knack - A Python command line interface framework
https://pypi.python.org/pypi/knack
MIT License
346 stars 95 forks source link

knack.util.ensure_dir can cause race conditions #162

Open swells opened 5 years ago

swells commented 5 years ago

This checks if a directory exists and then calls mkdir to create it. During automation or scripting where parallelism can come into play this can cause a race condition easily:

knack.util.ensure_dir

def ensure_dir(d):
    """ Create a directory if it doesn't exist """
    if not os.path.isdir(d):
        os.makedirs(d)

To prevent this race condition, use try/expect: to create the directory, then ignore "File exists" errors, which mean the directory was there.

 try:
        os.makedirs(path)
except OSError as e:
        if e.errno != errno.EEXIST:
            raise

Or for 3.2+

os.makedirs('/path/to/dir', exist_ok=True)  
swells commented 5 years ago

This looks to be a duplicate of 159

atbagga commented 4 years ago

@Swells Isn't this already fixed in #159 using same approach as you suggested?