Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

knack.util.ensure_dir can cause race conditions #162

Open
swells opened this issue Sep 3, 2019 · 2 comments
Open

knack.util.ensure_dir can cause race conditions #162

swells opened this issue Sep 3, 2019 · 2 comments

Comments

@swells
Copy link
Member

swells commented Sep 3, 2019

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
Copy link
Member Author

swells commented Sep 6, 2019

This looks to be a duplicate of 159

@atbagga
Copy link
Contributor

atbagga commented Oct 17, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants