=== trac/versioncontrol/svn_fs.py
==================================================================
--- trac/versioncontrol/svn_fs.py  (revision 1347)
+++ trac/versioncontrol/svn_fs.py  (local)
@@ -61,43 +61,61 @@
     Instances of this type return their associated `pool when called.
     """
     
-    def __init__(self, parent, parent_pool=lambda: None):
+    def __init__(self, parent, parent_pool=None):
         """
         Create a new pool that is a sub-pool of `parent_pool`, and arrange for
-        `self.close` to be called up when the `parent` object is destroyed.
+        `self._close` to be called up when the `parent` object is destroyed.
         
         The `parent` object must be weak-referenceable.  The returned `Pool`
         instance will have the value of the newly created pool. 
         """
-        self.pool = core.svn_pool_create(parent_pool())
-        self.children = []
+        self._parent_pool = parent_pool
+        self._children = []
+        self._waiting_to_close = False
         
-        if parent_pool():
-            parent_pool.children.append(self)
+        if self._parent_pool:
+            self._pool = core.svn_pool_create(self._parent_pool())        
+            self._parent_pool._children.append(self)
+        else:
+            self._pool = core.svn_pool_create(None)
         
         try:
-            parent._pool_closer = weakref.ref(parent, self.close)
+            parent._pool_closer = weakref.ref(parent, self._close)
         except TypeError:
-            self.close(None)
+            self._close(None)
             raise
     
     def __call__(self):
-        return self.pool
+        return self._pool
     
-    def close(self, x):
+    def _child_closed(self, child):
+        self._children.remove(child)
+        if self._waiting_to_close:
+            self._close(x)
+    
+    def _close(self, x):
         """
         The parent object has been destroyed so it is time for us to go.
         
+        If we still have children that are alive, we do not clean up just
+        yet.  This would lead to their memory being freed from under them.
+        Instead, we wait for our child to notify us that they have been
+        closed and clean up then. 
+        
         -- So long, and thanks for all the fish!
         """
-        if not self.pool:
+        assert self._pool
+        
+        if self._children:
+            self._waiting_to_close = True
             return
         
-        while self.children:
-            self.children.pop().close(None)
+        core.svn_pool_destroy(self._pool)
+        self._pool = None
         
-        core.svn_pool_destroy(self.pool)
-        self.pool = None
+        if self._parent_pool:
+            self._parent_pool._child_closed(self)
+            self._parent_pool = None
 
 
 class SubversionRepository(Repository):

