Mercurial > hg
comparison tests/test-lock.py @ 32088:0d892d820a51 stable
lock: avoid unintentional lock acquisition at failure of readlock
Acquiring lock by vfs.makelock() and getting lock holder (aka
"locker") information by vfs.readlock() aren't atomic action.
Therefore, failure of the former doesn't ensure success of the latter.
Before this patch, lock is unintentionally acquired, if ENOENT
causes failure of vfs.readlock() while 5 times retrying, because
lock._trylock() returns to caller silently after retrying, and
lock.lock() assumes that lock._trylock() returns successfully only if
lock is acquired.
In this case, lock symlink (or file) isn't created, even though lock
is treated as acquired in memory.
To avoid this issue, this patch makes lock._trylock() raise
LockHeld(EAGAIN) at the end of it, if lock isn't acquired while
retrying.
An empty "locker" meaning "busy for frequent lock/unlock by many
processes" might appear in an abortion message, if lock acquisition
fails. Therefore, this patch also does:
- use '%r' to increase visibility of "locker", even if it is empty
- show hint message to explain what empty "locker" means
author | FUJIWARA Katsunori <foozy@lares.dti.ne.jp> |
---|---|
date | Mon, 01 May 2017 19:59:13 +0900 |
parents | e067741d4607 |
children | 68c43a416585 |
comparison
equal
deleted
inserted
replaced
32087:e1938d6051da | 32088:0d892d820a51 |
---|---|
1 from __future__ import absolute_import | 1 from __future__ import absolute_import |
2 | 2 |
3 import copy | 3 import copy |
4 import errno | |
4 import os | 5 import os |
5 import silenttestrunner | 6 import silenttestrunner |
6 import tempfile | 7 import tempfile |
7 import types | 8 import types |
8 import unittest | 9 import unittest |
265 | 266 |
266 self.assertRaises(error.LockInheritanceContractViolation, tryinherit) | 267 self.assertRaises(error.LockInheritanceContractViolation, tryinherit) |
267 | 268 |
268 lock.release() | 269 lock.release() |
269 | 270 |
271 def testfrequentlockunlock(self): | |
272 """This tests whether lock acquisition fails as expected, even if | |
273 (1) lock can't be acquired (makelock fails by EEXIST), and | |
274 (2) locker info can't be read in (readlock fails by ENOENT) while | |
275 retrying 5 times. | |
276 """ | |
277 | |
278 d = tempfile.mkdtemp(dir=os.getcwd()) | |
279 state = teststate(self, d) | |
280 | |
281 def emulatefrequentlock(*args): | |
282 raise OSError(errno.EEXIST, "File exists") | |
283 def emulatefrequentunlock(*args): | |
284 raise OSError(errno.ENOENT, "No such file or directory") | |
285 | |
286 state.vfs.makelock = emulatefrequentlock | |
287 state.vfs.readlock = emulatefrequentunlock | |
288 | |
289 try: | |
290 state.makelock(timeout=0) | |
291 self.fail("unexpected lock acquisition") | |
292 except error.LockHeld as why: | |
293 self.assertTrue(why.errno == errno.ETIMEDOUT) | |
294 self.assertTrue(why.locker == "") | |
295 state.assertlockexists(False) | |
296 | |
270 if __name__ == '__main__': | 297 if __name__ == '__main__': |
271 silenttestrunner.main(__name__) | 298 silenttestrunner.main(__name__) |