comparison tests/test-lock-badness.t @ 23032:f484be02bd35

lock: while releasing, unlink lockfile even if the release function throws Consider a hypothetical bug in the release function that causes it to raise an exception. Also consider the bisect command, which saves its state in a finally clause. Saving the state requires acquiring the wlock. If we don't unlink the lockfile when the exception is thrown, we'll try to acquire the wlock again. We're going to try and acquire a lock again while our old lockfile is on disk. The PID on disk is our own, and of course we're still running, so we won't take over the lock. Hence we'll be stuck waiting for a lock that we left behind ourselves. To avoid this, always unlink the lockfile. This preserves the invariant that self.held > 0 is equivalent to the lockfile existing on disk.
author Siddharth Agarwal <sid0@fb.com>
date Thu, 16 Oct 2014 19:15:51 -0700
parents 8fb6844a4ff1
children 6fbe35588433
comparison
equal deleted inserted replaced
23031:3c0983cc279e 23032:f484be02bd35
8 adding a 8 adding a
9 9
10 $ hg clone a b 10 $ hg clone a b
11 updating to branch default 11 updating to branch default
12 1 files updated, 0 files merged, 0 files removed, 0 files unresolved 12 1 files updated, 0 files merged, 0 files removed, 0 files unresolved
13
14 Test that raising an exception in the release function doesn't cause the lock to choke
15
16 $ cat > testlock.py << EOF
17 > from mercurial import cmdutil, error, util
18 >
19 > cmdtable = {}
20 > command = cmdutil.command(cmdtable)
21 >
22 > def acquiretestlock(repo, releaseexc):
23 > def unlock():
24 > if releaseexc:
25 > raise util.Abort('expected release exception')
26 > l = repo._lock(repo.vfs, 'testlock', False, unlock, None, 'test lock')
27 > return l
28 >
29 > @command('testlockexc')
30 > def testlockexc(ui, repo):
31 > testlock = acquiretestlock(repo, True)
32 > try:
33 > testlock.release()
34 > finally:
35 > try:
36 > testlock = acquiretestlock(repo, False)
37 > except error.LockHeld:
38 > raise util.Abort('lockfile on disk even after releasing!')
39 > testlock.release()
40 > EOF
41 $ cat >> $HGRCPATH << EOF
42 > [extensions]
43 > testlock=$TESTTMP/testlock.py
44 > EOF
45
46 $ hg -R b testlockexc
47 abort: expected release exception
48 [255]
13 49
14 One process waiting for another 50 One process waiting for another
15 51
16 $ cat > hooks.py << EOF 52 $ cat > hooks.py << EOF
17 > import time 53 > import time