Merge branch 'stable'

* stable:
  Fixed race condition in 'repo sync -jN' that would open multiple masters.
This commit is contained in:
Shawn O. Pearce 2010-12-22 14:46:15 -08:00
commit 9275fd4329
2 changed files with 89 additions and 60 deletions

View File

@ -18,6 +18,10 @@ import os
import re import re
import subprocess import subprocess
import sys import sys
try:
import threading as _threading
except ImportError:
import dummy_threading as _threading
import time import time
import urllib2 import urllib2
@ -371,10 +375,27 @@ class RefSpec(object):
_master_processes = [] _master_processes = []
_master_keys = set() _master_keys = set()
_ssh_master = True _ssh_master = True
_master_keys_lock = None
def init_ssh():
"""Should be called once at the start of repo to init ssh master handling.
At the moment, all we do is to create our lock.
"""
global _master_keys_lock
assert _master_keys_lock is None, "Should only call init_ssh once"
_master_keys_lock = _threading.Lock()
def _open_ssh(host, port=None): def _open_ssh(host, port=None):
global _ssh_master global _ssh_master
# Acquire the lock. This is needed to prevent opening multiple masters for
# the same host when we're running "repo sync -jN" (for N > 1) _and_ the
# manifest <remote fetch="ssh://xyz"> specifies a different host from the
# one that was passed to repo init.
_master_keys_lock.acquire()
try:
# Check to see whether we already think that the master is running; if we # Check to see whether we already think that the master is running; if we
# think it's already running, return right away. # think it's already running, return right away.
if port is not None: if port is not None:
@ -439,8 +460,12 @@ def _open_ssh(host, port=None):
_master_keys.add(key) _master_keys.add(key)
time.sleep(1) time.sleep(1)
return True return True
finally:
_master_keys_lock.release()
def close_ssh(): def close_ssh():
global _master_keys_lock
terminate_ssh_clients() terminate_ssh_clients()
for p in _master_processes: for p in _master_processes:
@ -459,6 +484,9 @@ def close_ssh():
except OSError: except OSError:
pass pass
# We're done with the lock, so we can delete it.
_master_keys_lock = None
URI_SCP = re.compile(r'^([^@:]*@?[^:/]{1,}):') URI_SCP = re.compile(r'^([^@:]*@?[^:/]{1,}):')
URI_ALL = re.compile(r'^([a-z][a-z+]*)://([^@/]*@?[^/]*)/') URI_ALL = re.compile(r'^([a-z][a-z+]*)://([^@/]*@?[^/]*)/')

View File

@ -28,7 +28,7 @@ import re
import sys import sys
from trace import SetTrace from trace import SetTrace
from git_config import close_ssh from git_config import init_ssh, close_ssh
from command import InteractiveCommand from command import InteractiveCommand
from command import MirrorSafeCommand from command import MirrorSafeCommand
from command import PagedCommand from command import PagedCommand
@ -212,6 +212,7 @@ def _Main(argv):
repo = _Repo(opt.repodir) repo = _Repo(opt.repodir)
try: try:
try: try:
init_ssh()
repo._Run(argv) repo._Run(argv)
finally: finally:
close_ssh() close_ssh()