comparison mercurial/hg.py @ 28289:d493d64757eb stable 3.7.2

hg: obtain lock when creating share from pooled repo (issue5104) There are race conditions between clients performing a shared clone to pooled storage: 1) Clients race to create the new shared repo in the pool directory 2) 1 client is seeding the repo in the pool directory and another goes to share it before it is fully cloned We prevent these race conditions by obtaining a lock in the pool directory that is derived from the name of the repo we will be accessing. To test this, a simple generic "lockdelay" extension has been added. The extension inserts an optional, configurable delay before or after lock acquisition. In the test, we delay 2 seconds after lock acquisition in the first process and 1 second before lock acquisition in the 2nd process. This means the first process has 1s to obtain the lock. There is a race condition here. If we encounter it in the wild, we could change the dummy extension to wait on the lock file to appear instead of relying on timing. But that's more complicated. Let's see what happens first.
author Gregory Szorc <gregory.szorc@gmail.com>
date Sat, 27 Feb 2016 18:22:49 -0800
parents b502138f5faa
children 549ff28a345f a2c2dd399f3b
comparison
equal deleted inserted replaced
28288:e417e4512b0f 28289:d493d64757eb
333 raise error.Abort(_("src repository does not support " 333 raise error.Abort(_("src repository does not support "
334 "revision lookup and so doesn't " 334 "revision lookup and so doesn't "
335 "support clone by revision")) 335 "support clone by revision"))
336 revs = [srcpeer.lookup(r) for r in rev] 336 revs = [srcpeer.lookup(r) for r in rev]
337 337
338 # Obtain a lock before checking for or cloning the pooled repo otherwise
339 # 2 clients may race creating or populating it.
340 pooldir = os.path.dirname(sharepath)
341 # lock class requires the directory to exist.
342 try:
343 util.makedir(pooldir, False)
344 except OSError as e:
345 if e.errno != errno.EEXIST:
346 raise
347
348 poolvfs = scmutil.vfs(pooldir)
338 basename = os.path.basename(sharepath) 349 basename = os.path.basename(sharepath)
339 350
340 if os.path.exists(sharepath): 351 with lock.lock(poolvfs, '%s.lock' % basename):
341 ui.status(_('(sharing from existing pooled repository %s)\n') % 352 if os.path.exists(sharepath):
342 basename) 353 ui.status(_('(sharing from existing pooled repository %s)\n') %
343 else: 354 basename)
344 ui.status(_('(sharing from new pooled repository %s)\n') % basename) 355 else:
345 # Always use pull mode because hardlinks in share mode don't work well. 356 ui.status(_('(sharing from new pooled repository %s)\n') % basename)
346 # Never update because working copies aren't necessary in share mode. 357 # Always use pull mode because hardlinks in share mode don't work
347 clone(ui, peeropts, source, dest=sharepath, pull=True, 358 # well. Never update because working copies aren't necessary in
348 rev=rev, update=False, stream=stream) 359 # share mode.
360 clone(ui, peeropts, source, dest=sharepath, pull=True,
361 rev=rev, update=False, stream=stream)
349 362
350 sharerepo = repository(ui, path=sharepath) 363 sharerepo = repository(ui, path=sharepath)
351 share(ui, sharerepo, dest=dest, update=update, bookmarks=False) 364 share(ui, sharerepo, dest=dest, update=update, bookmarks=False)
352 365
353 # We need to perform a pull against the dest repo to fetch bookmarks 366 # We need to perform a pull against the dest repo to fetch bookmarks