Skip to content

Commit a803803

Browse files
committed
Lockfile now uses low-level commands to interact with the filesystem in order to make it impossible for threads to overwrite each other if we are talking about miliseconds. This renders plenty of 'special' handling redundant
1 parent 0bb2fc8 commit a803803

File tree

1 file changed

+5
-30
lines changed

1 file changed

+5
-30
lines changed

lib/git/utils.py

+5-30
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,6 @@ def _lock_file_path(self):
119119
"""
120120
return "%s.lock" % (self._file_path)
121121

122-
def _get_id(self):
123-
"""Returns string id to be written into the lock file"""
124-
return "%i|%i" % (os.getpid(), hash(self))
125-
126122
def _has_lock(self):
127123
"""
128124
Return
@@ -134,17 +130,6 @@ def _has_lock(self):
134130
if not self._owns_lock:
135131
return False
136132

137-
lock_file = self._lock_file_path()
138-
try:
139-
fp = open(lock_file, "rb")
140-
pid = fp.read()
141-
fp.close()
142-
except IOError:
143-
raise AssertionError("The lock file at %s could not be read" % lock_file)
144-
145-
if pid != self._get_id():
146-
raise AssertionError("We claim to own the lock at %s, but it was not owned by our process %r, but by %r" % (lock_file, self._get_id(), pid ))
147-
148133
return True
149134

150135
def _obtain_lock_or_raise(self):
@@ -160,21 +145,11 @@ def _obtain_lock_or_raise(self):
160145
if os.path.isfile(lock_file):
161146
raise IOError("Lock for file %r did already exist, delete %r in case the lock is illegal" % (self._file_path, lock_file))
162147

163-
my_id = self._get_id()
164-
fp = open(lock_file, "wb")
165-
fp.write(my_id)
166-
fp.close()
167-
168-
# verify its truly us who got the lock - if two threads are doing this within the
169-
# fraction of a millisecond, it is possible to actually trick the FS
170-
# and two threads write, but only one succeeds.
171-
fp = open(lock_file, 'rb')
172-
actual_id = fp.read()
173-
fp.close()
174-
if actual_id != my_id:
175-
msg = "Failed to obtain lock for file %r as the process identified by %r outraced this process or thread %r" % (self._file_path, actual_id, my_id)
176-
raise IOError(msg)
177-
# END verification
148+
try:
149+
fd = os.open(lock_file, os.O_WRONLY | os.O_CREAT | os.O_EXCL, 0)
150+
os.close(fd)
151+
except OSError,e:
152+
raise IOError(str(e))
178153

179154
self._owns_lock = True
180155

0 commit comments

Comments
 (0)