comparison mercurial/localrepo.py @ 42511:044045dce23a stable

bookmarks: actual fix for race condition deleting bookmark This is a simple but efficient fix to prevent the issue tested in `test-bookmarks-corner-case.t`. It might be worth pursuing a more generic approach where filecache learn to depend on each other, but that would not be suitable for stable. The issue is complicated enough that I documented the race and its current solution as inline comment. See this comment for details on the fix.
author Pierre-Yves David <pierre-yves.david@octobus.net>
date Fri, 21 Jun 2019 03:50:40 +0200
parents 3472a3f9d785
children 84aff7e20c55 2c27b7fadcd3
comparison
equal deleted inserted replaced
42510:3472a3f9d785 42511:044045dce23a
1220 return cls(self, name, visibilityexceptions) 1220 return cls(self, name, visibilityexceptions)
1221 1221
1222 @mixedrepostorecache(('bookmarks', 'plain'), ('bookmarks.current', 'plain'), 1222 @mixedrepostorecache(('bookmarks', 'plain'), ('bookmarks.current', 'plain'),
1223 ('00changelog.i', '')) 1223 ('00changelog.i', ''))
1224 def _bookmarks(self): 1224 def _bookmarks(self):
1225 # Since the multiple files involved in the transaction cannot be
1226 # written atomically (with current repository format), there is a race
1227 # condition here.
1228 #
1229 # 1) changelog content A is read
1230 # 2) outside transaction update changelog to content B
1231 # 3) outside transaction update bookmark file referring to content B
1232 # 4) bookmarks file content is read and filtered against changelog-A
1233 #
1234 # When this happens, bookmarks against nodes missing from A are dropped.
1235 #
1236 # Having this happening during read is not great, but it become worse
1237 # when this happen during write because the bookmarks to the "unknown"
1238 # nodes will be dropped for good. However, writes happen within locks.
1239 # This locking makes it possible to have a race free consistent read.
1240 # For this purpose data read from disc before locking are
1241 # "invalidated" right after the locks are taken. This invalidations are
1242 # "light", the `filecache` mechanism keep the data in memory and will
1243 # reuse them if the underlying files did not changed. Not parsing the
1244 # same data multiple times helps performances.
1245 #
1246 # Unfortunately in the case describe above, the files tracked by the
1247 # bookmarks file cache might not have changed, but the in-memory
1248 # content is still "wrong" because we used an older changelog content
1249 # to process the on-disk data. So after locking, the changelog would be
1250 # refreshed but `_bookmarks` would be preserved.
1251 # Adding `00changelog.i` to the list of tracked file is not
1252 # enough, because at the time we build the content for `_bookmarks` in
1253 # (4), the changelog file has already diverged from the content used
1254 # for loading `changelog` in (1)
1255 #
1256 # To prevent the issue, we force the changelog to be explicitly
1257 # reloaded while computing `_bookmarks`. The data race can still happen
1258 # without the lock (with a narrower window), but it would no longer go
1259 # undetected during the lock time refresh.
1260 #
1261 # The new schedule is as follow
1262 #
1263 # 1) filecache logic detect that `_bookmarks` needs to be computed
1264 # 2) cachestat for `bookmarks` and `changelog` are captured (for book)
1265 # 3) We force `changelog` filecache to be tested
1266 # 4) cachestat for `changelog` are captured (for changelog)
1267 # 5) `_bookmarks` is computed and cached
1268 #
1269 # The step in (3) ensure we have a changelog at least as recent as the
1270 # cache stat computed in (1). As a result at locking time:
1271 # * if the changelog did not changed since (1) -> we can reuse the data
1272 # * otherwise -> the bookmarks get refreshed.
1273 self._refreshchangelog()
1225 return bookmarks.bmstore(self) 1274 return bookmarks.bmstore(self)
1226 1275
1227 def _refreshchangelog(self): 1276 def _refreshchangelog(self):
1228 """make sure the in memory changelog match the on-disk one""" 1277 """make sure the in memory changelog match the on-disk one"""
1229 if ('changelog' in vars(self) and self.currenttransaction() is None): 1278 if ('changelog' in vars(self) and self.currenttransaction() is None):