facebook/wangle

Checking if file exists before reading can cause problem in case of race conditions.

mohitanand001 opened this issue · 1 comments

if os.path.exists(installed_name):
with open(installed_name, "rb") as f:

The above snippet of code can cause problem in case of race condition, where file might be present at the time of if os.path.exists(path) but might not exist when we try to read the file and result in exception. The python EAFP(Easy to Ask for Forgiveness than Permission) philosophy suggests that we use a try catch block to avoid this problem. Suggested changes.
You can read this stackoverflow answer to understand the problem more
Can this issue be assigned to me?
The above snippet can be replaced by the following.

try:
    f = open(config_file)
except IOError as e:
    print("File can't be accessed")
else:
      with f:
             remaining code
              ...
              ...

@wez 
@yfeldblum 

This build system should not be used concurrently with other programs which can modify the build system's filesystem state.

But if the user insists on using it that way and the build system fails with a raised exception, the user may simply retry.