Index: trac/db/pool.py
===================================================================
--- trac/db/pool.py	(revision 3791)
+++ trac/db/pool.py	(working copy)
@@ -46,8 +46,9 @@
 
     def __del__(self):
         self.close()
-
-
+        self._pool._debug_decr(self._tid)
+                                        
+        
 class ConnectionPool(object):
     """A very simple connection pool implementation."""
 
@@ -59,7 +60,19 @@
         self._cursize = 0 # current pool size, includes active connections
         self._connector = connector
         self._kwargs = kwargs
+        self._debug_pcpt = {}
+        self._debug_tid = {}
 
+    def _debug_decr(self, tid):
+        self._available.acquire()
+        try:
+            pooled_connections_per_thread = self._debug_pcpt.get(tid)
+            assert pooled_connections_per_thread # check the debugging code
+            assert pooled_connections_per_thread > 0
+            self._debug_pcpt[tid] = pooled_connections_per_thread - 1
+        finally:
+            self._available.release()
+
     def get_cnx(self, timeout=None):
         start = time.time()
         self._available.acquire()
@@ -70,6 +83,7 @@
                 if num == 0: # was pushed back (see _cleanup)
                     cnx.rollback()
                 self._active[tid][0] = num + 1
+                self._debug_pcpt[tid] = self._debug_pcpt.get(tid, 0) + 1
                 return PooledConnection(self, cnx, tid)
             while True:
                 if self._dormant:
@@ -92,6 +106,13 @@
                                                 % timeout
                     else:
                         self._available.wait()
+            if tid in self._debug_pcpt:
+                if tid in self._debug_tid:
+                    print '[%d] THREAD REUSED (%d)' % \
+                          (tid, self._debug_tid.get(tid))
+                pooled_connections_per_thread = self._debug_pcpt.get(tid)
+                assert pooled_connections_per_thread == 0
+            self._debug_pcpt[tid] = 1
             self._active[tid] = [1, cnx]
             return PooledConnection(self, cnx, tid)
         finally:
@@ -102,6 +123,15 @@
         try:
             if tid in self._active:
                 num, cnx_ = self._active.get(tid)
+                if cnx is not cnx_:
+                    print 'ConnectionPool is ', repr(self)
+                    print 'In [%d], _active -> %s\n _dormant -> %s' % \
+                          (threading._get_ident(),
+                           repr(self._active), repr(self._dormant))
+                    print 'Available connections: %d/%d' % \
+                          (self._cursize, self._maxsize)
+                    print 'PooledConnection created from [%d] %s' % \
+                          (tid, repr(cnx))
                 assert cnx is cnx_
                 if num > 1:
                     self._active[tid][0] = num - 1
@@ -131,6 +161,7 @@
         try:
             if tid:
                 cleanup_list = [tid]
+                self._debug_tid[tid] = self._debug_tid.get(tid, 0) + 1
             else:
                 cleanup_list = self._active.keys()
             for tid in cleanup_list:

