Index: /Users/shanec/svn/workspace/trunk/build/vendor/trac/trac/db/pool.py
===================================================================
--- pool.py	(revision 8345)
+++ pool.py	(working copy)
@@ -132,8 +132,11 @@
     def _return_cnx(self, cnx, key, tid):
         self._available.acquire()
         try:
-            assert (tid, key) in self._active
-            cnx, num = self._active[(tid, key)]
+            #assert (tid, key) in self._active
+            # get the active connection and return it to the pool
+            cnx, num = self._active.get((tid, key), (None,0))
+            if not cnx: return
+
             if num == 1:
                 del self._active[(tid, key)]
                 self._available.notify() 
@@ -146,8 +149,20 @@
         finally:
             self._available.release()
 
-    def shutdown(self, tid=None):
+    def _return_active_cnx(self, tid, connector, kwargs):
+        if not connector or not kwargs:
+            return
+        key = unicode(kwargs)
+        # shutdown is called at the end of every request, return the
+        # socket connection to the pool if there is one
+        cnx, num = self._active.get((tid, key), (None,0))
+        if cnx:
+            self._active[(tid, key)] = (cnx, 1)
+            self._return_cnx(cnx, key, tid)
+
+    def shutdown(self, tid=None, connector=None, kwargs=None):
         """Close pooled connections not used in a while"""
+        self._return_active_cnx(tid, connector, kwargs)
         delay = 120
         if tid is None:
             delay = 0
@@ -176,5 +191,5 @@
         return _backend.get_cnx(self._connector, self._kwargs, timeout)
 
     def shutdown(self, tid=None):
-        _backend.shutdown(tid)
+        _backend.shutdown(tid, self._connector, self._kwargs)
 

