Patchwork [3,of,3] reposvfs: add a ward to check if locks are properly taken

login
register
mail settings
Submitter Pierre-Yves David
Date Oct. 14, 2016, 1:56 a.m.
Message ID <e4af8c0de378cd1c7edd.1476410175@nodosa.octopoid.net>
Download mbox | patch
Permalink /patch/17079/
State Changes Requested
Headers show

Comments

Pierre-Yves David - Oct. 14, 2016, 1:56 a.m.
# HG changeset patch
# User Pierre-Yves David <pierre-yves.david@ens-lyon.org>
# Date 1470672882 -7200
#      Mon Aug 08 18:14:42 2016 +0200
# Node ID e4af8c0de378cd1c7eddc83e086d9416f83755f5
# Parent  8d3ae769555bd7134bb70f31b16e67d17726b627
# EXP-Topic vfs.ward
reposvfs: add a ward to check if locks are properly taken

We use the 'ward' to check for the store lock when accessing file in '.hg/store'
for writing. This caught a couple of instance where the transaction was released
after the lock, we should probably have a dedicated checker for that case.

Patch

diff --git a/mercurial/localrepo.py b/mercurial/localrepo.py
--- a/mercurial/localrepo.py
+++ b/mercurial/localrepo.py
@@ -344,8 +344,14 @@  class localrepository(object):
             if inst.errno != errno.ENOENT:
                 raise
 
+        getsvfs = scmutil.vfs
+        if (self.ui.configbool('devel', 'all-warnings') or
+            self.ui.configbool('devel', 'check-locks')):
+                def getsvfs(*args, **kwargs):
+                    kwargs['ward'] = self._getsvfsward()
+                    return scmutil.vfs(*args, **kwargs)
         self.store = store.store(
-                self.requirements, self.sharedpath, scmutil.vfs)
+                self.requirements, self.sharedpath, getsvfs)
         self.spath = self.store.path
         self.svfs = self.store.vfs
         self.sjoin = self.store.join
@@ -410,6 +416,23 @@  class localrepository(object):
                                   stacklevel=2)
         return checkvfs
 
+    def _getsvfsward(self):
+        """build a ward for self.svfs"""
+        rref = weakref.ref(self)
+        def checksvfs(f, mode, atomictemp):
+            repo = rref()
+            if repo is None or not util.safehasattr(repo, '_lockref'):
+                return
+            if mode in ('r', 'rb'):
+                return
+            assert f.startswith(repo.sharedpath), (repo.path, f)
+            # truncate name relative to the repository (.hg)
+            relname = f[len(repo.sharedpath) + 1:]
+            if repo._currentlock(repo._lockref) is None:
+                repo.ui.develwarn('write with no lock: "%s"' % relname,
+                                  stacklevel=2)
+        return checksvfs
+
     def close(self):
         self._writecaches()