Ticket #3504: debug_thread_reuse.diff
| File debug_thread_reuse.diff, 3.1 KB (added by cboos, 2 years ago) |
|---|
-
trac/db/pool.py
46 46 47 47 def __del__(self): 48 48 self.close() 49 50 49 self._pool._debug_decr(self._tid) 50 51 51 52 class ConnectionPool(object): 52 53 """A very simple connection pool implementation.""" 53 54 … … 59 60 self._cursize = 0 # current pool size, includes active connections 60 61 self._connector = connector 61 62 self._kwargs = kwargs 63 self._debug_pcpt = {} 64 self._debug_tid = {} 62 65 66 def _debug_decr(self, tid): 67 self._available.acquire() 68 try: 69 pooled_connections_per_thread = self._debug_pcpt.get(tid) 70 assert pooled_connections_per_thread # check the debugging code 71 assert pooled_connections_per_thread > 0 72 self._debug_pcpt[tid] = pooled_connections_per_thread - 1 73 finally: 74 self._available.release() 75 63 76 def get_cnx(self, timeout=None): 64 77 start = time.time() 65 78 self._available.acquire() … … 70 83 if num == 0: # was pushed back (see _cleanup) 71 84 cnx.rollback() 72 85 self._active[tid][0] = num + 1 86 self._debug_pcpt[tid] = self._debug_pcpt.get(tid, 0) + 1 73 87 return PooledConnection(self, cnx, tid) 74 88 while True: 75 89 if self._dormant: … … 92 106 % timeout 93 107 else: 94 108 self._available.wait() 109 if tid in self._debug_pcpt: 110 if tid in self._debug_tid: 111 print '[%d] THREAD REUSED (%d)' % \ 112 (tid, self._debug_tid.get(tid)) 113 pooled_connections_per_thread = self._debug_pcpt.get(tid) 114 assert pooled_connections_per_thread == 0 115 self._debug_pcpt[tid] = 1 95 116 self._active[tid] = [1, cnx] 96 117 return PooledConnection(self, cnx, tid) 97 118 finally: … … 102 123 try: 103 124 if tid in self._active: 104 125 num, cnx_ = self._active.get(tid) 126 if cnx is not cnx_: 127 print 'ConnectionPool is ', repr(self) 128 print 'In [%d], _active -> %s\n _dormant -> %s' % \ 129 (threading._get_ident(), 130 repr(self._active), repr(self._dormant)) 131 print 'Available connections: %d/%d' % \ 132 (self._cursize, self._maxsize) 133 print 'PooledConnection created from [%d] %s' % \ 134 (tid, repr(cnx)) 105 135 assert cnx is cnx_ 106 136 if num > 1: 107 137 self._active[tid][0] = num - 1 … … 131 161 try: 132 162 if tid: 133 163 cleanup_list = [tid] 164 self._debug_tid[tid] = self._debug_tid.get(tid, 0) + 1 134 165 else: 135 166 cleanup_list = self._active.keys() 136 167 for tid in cleanup_list:
