comparison mercurial/branchmap.py @ 29744:0d588332ad2c

branchmap: acquires lock before writting the rev branch cache We now attempt to acquire a lock and write the branch cache within that lock. This would prevent cache corruption when multiple processes try to write the cache at the same time.
author Pierre-Yves David <pierre-yves.david@ens-lyon.org>
date Fri, 05 Aug 2016 14:57:16 +0200
parents 9f3c49ee4486
children 3b184adfb5be
comparison
equal deleted inserted replaced
29743:9f3c49ee4486 29744:0d588332ad2c
468 tr.addfinalize('write-revbranchcache', self.write) 468 tr.addfinalize('write-revbranchcache', self.write)
469 469
470 def write(self, tr=None): 470 def write(self, tr=None):
471 """Save branch cache if it is dirty.""" 471 """Save branch cache if it is dirty."""
472 repo = self._repo 472 repo = self._repo
473 if True: 473 wlock = None
474 try:
474 if self._rbcnamescount < len(self._names): 475 if self._rbcnamescount < len(self._names):
476 wlock = repo.wlock(wait=False)
475 try: 477 try:
476 if self._rbcnamescount != 0: 478 if self._rbcnamescount != 0:
477 f = repo.vfs.open(_rbcnames, 'ab') 479 f = repo.vfs.open(_rbcnames, 'ab')
478 if f.tell() == self._rbcsnameslen: 480 if f.tell() == self._rbcsnameslen:
479 f.write('\0') 481 f.write('\0')
499 return 501 return
500 self._rbcnamescount = len(self._names) 502 self._rbcnamescount = len(self._names)
501 503
502 start = self._rbcrevslen * _rbcrecsize 504 start = self._rbcrevslen * _rbcrecsize
503 if start != len(self._rbcrevs): 505 if start != len(self._rbcrevs):
506 if wlock is None:
507 wlock = repo.wlock(wait=False)
504 revs = min(len(repo.changelog), 508 revs = min(len(repo.changelog),
505 len(self._rbcrevs) // _rbcrecsize) 509 len(self._rbcrevs) // _rbcrecsize)
506 try: 510 try:
507 f = repo.vfs.open(_rbcrevs, 'ab') 511 f = repo.vfs.open(_rbcrevs, 'ab')
508 if f.tell() != start: 512 if f.tell() != start:
519 except (IOError, OSError, error.Abort) as inst: 523 except (IOError, OSError, error.Abort) as inst:
520 repo.ui.debug("couldn't write revision branch cache: %s\n" % 524 repo.ui.debug("couldn't write revision branch cache: %s\n" %
521 inst) 525 inst)
522 return 526 return
523 self._rbcrevslen = revs 527 self._rbcrevslen = revs
528 except error.LockError as inst:
529 repo.ui.debug("couldn't write revision branch cache: %s\n" % inst)
530 finally:
531 if wlock is not None:
532 wlock.release()