wbolster/happybase — pull request #25 by rogerhu (2013-06-03 21:23:17+00:00)
Title: ClientProxy and connection have a circular reference, causing Python not to be able to garbage collect.

Found a circular reference that may have been causing memory leak issues. The offending code appears to be the _ClientProxy() class that takes in a connection object. Since the connection.client points to _ClientProxy() and _ClientProxy() instance is pointing back to connection, Python fails to call the __del__ function inside the Connection class.

![image](https://f.cloud.github.com/assets/326857/602028/e1958afe-cc8f-11e2-916f-cfeebb3e74ff.png)

```
def _create_connection(self):
"""Create a new connection with monkey-patched Thrift client."""
connection = Connection(**self._connection_kwargs)
connection.client = _ClientProxy(connection, connection.client)
return connection

```

See the problem? ClientProxy points to connection, and connection.client points to ClientProxy.

I put inside the __del__(self) of connection to verify the connection will die:
```
+ bl/happybase-0.5/connection.py
-167,6 +167,7 @@ class Connection(object)
self.transport.close()

def __del__(self):
print "dying"
try:
self._initialized
except AttributeError:
```

```
from happybase.pool import ConnectionPool

pool = ConnectionPool(3, host='myhost.com', port=9090)

with pool.connection() as conn:
print conn

import objgraph
objgraph.show_backrefs([conn, conn.client])
```

With this fix, the print "dying" appears and memory is reclaimed after the ConnectionPool() instead goes out of scope (http://eli.thegreenplace.net/2009/06/12/safely-using-destructors-in-python/).

Is there a way to avoid this issue by avoiding needing to pass the connection object into the ClientProxy?