[PATCH] blackbox: defer opening a log file until needed (issue3869)

Bryan O'Sullivan bos at serpentine.com
Tue Mar 26 18:28:24 CDT 2013


# HG changeset patch
# User Bryan O'Sullivan <bryano at fb.com>
# Date 1364340471 25200
#      Tue Mar 26 16:27:51 2013 -0700
# Node ID b399b3dfd25471dfb99856419e90d49961c85c58
# Parent  b2a36e9b9ccc9b6187efa93970eac7547aeb9632
blackbox: defer opening a log file until needed (issue3869)

Previously, we opened the log file when creating a repo object. This
was inefficient (not all repo creation is going to result in a need to
log something), but more importantly it broke subrepo updates when used
on NFS.

* perform an update in the master repo that triggers a subrepo clone

* empty subrepo already exists, and has an open, empty blackbox.log file
  due to it being opened eagerly/prematurely

* hg decides to blow away the skeletal subrepo (see use of shutil.rmtree
  in subrepo._get)

* we crash, due to NFS treating a delete of an open file as really a
  rename to a hidden ".nfs" file

Now that we open the blackbox log file on demand, no file exists at the
time the empty subrepo is deleted, so the above problem does not occur.

diff --git a/hgext/blackbox.py b/hgext/blackbox.py
--- a/hgext/blackbox.py
+++ b/hgext/blackbox.py
@@ -47,6 +47,15 @@ def wrapui(ui):
 
             if util.safehasattr(self, '_blackbox'):
                 blackbox = self._blackbox
+            elif util.safehasattr(self, '_bbopener'):
+                try:
+                    self._blackbox = self._bbopener('blackbox.log', 'a')
+                except (IOError, OSError), err:
+                    self.debug('warning: cannot write to blackbox.log: %s\n' %
+                               err.strerror)
+                    del self._bbopener
+                    self._blackbox = None
+                blackbox = self._blackbox
             else:
                 # certain ui instances exist outside the context of
                 # a repo, so just default to the last blackbox that
@@ -65,12 +74,7 @@ def wrapui(ui):
                 lastblackbox = blackbox
 
         def setrepo(self, repo):
-            try:
-                self._blackbox = repo.opener('blackbox.log', 'a')
-            except (IOError, OSError), err:
-                self.debug('warning: cannot write to blackbox.log: %s\n' %
-                           err.strerror)
-                self._blackbox = None
+            self._bbopener = repo.opener
 
     ui.__class__ = blackboxui
 


More information about the Mercurial-devel mailing list