microsoft / microsoft/knack

knack.util.ensure_dir can cause race conditions

Open
#162 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
358
Forks
101
PR merge metrics
No merged PRs in 30d

Description

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)  

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the knack.util.ensure_dir entry point shown in the issue and inspect how it checks for and creates directories. Exercise parallel calls to identify the failure, then verify that concurrent creation is handled without masking other filesystem errors. Run the project's existing test suite if available and confirm the reported race no longer occurs.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
cli
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.