Skip to content

Commit

Permalink
Fixing untrustable os.path.exists
Browse files Browse the repository at this point in the history
As per https://stackoverflow.com/a/3112717/1067833,
os.path.exists sometimes delivers a wrong value. These are corner
cases, but mine is exactly one:

I run xapian-haystack in a multiprocess environment (indexing and
django server), on an LXC container, which has its mount probably
mounted from an NFS server (I have no control/information over it).

This corner case results the os.path.exists give `True` for one
process and `False` for another, resulting in the exception I
handle with this patch.
  • Loading branch information
karolyi authored and jorgecarleitao committed Oct 28, 2017
1 parent 253c4c2 commit 3c322a9
Showing 1 changed file with 5 additions and 2 deletions.
7 changes: 5 additions & 2 deletions xapian_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,11 @@ def __init__(self, connection_alias, **connection_options):

self.path = connection_options.get('PATH')

if self.path != MEMORY_DB_NAME and not os.path.exists(self.path):
os.makedirs(self.path)
if self.path != MEMORY_DB_NAME:
try:
os.makedirs(self.path)
except FileExistsError:
pass

self.flags = connection_options.get('FLAGS', DEFAULT_XAPIAN_FLAGS)
self.language = getattr(settings, 'HAYSTACK_XAPIAN_LANGUAGE', 'english')
Expand Down

2 comments on commit 3c322a9

@xacce
Copy link

@xacce xacce commented on 3c322a9 Nov 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

python 2.7 doesnt has FileExistsError

@karolyi
Copy link
Contributor Author

@karolyi karolyi commented on 3c322a9 Apr 7, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.