wulczer/txpostgres — Issue #6: disconnecting while a query is underway hangs when using epoll
Author: wulczer
Created: 2011-11-05 20:16:37+00:00
State: open

This test case hangs when using the epoll reactor:

```python
def test_disconnectWhileRunning(self):
"""
Disconnecting from the server when there is a query underway causes the
query to fail with ConnectionDone.
"""
d = self.conn.runQuery("select pg_sleep(5)")
reactor.callLater(0, self.conn.close)
def restoreConnection(res):
self.conn = txpostgres.Connection()
d = self.conn.connect(user=DB_USER, password=DB_PASS,
host=DB_HOST, database=DB_NAME)
return d.addCallback(lambda _: res)

d = self.assertFailure(d, error.ConnectionDone)
# restore the connection, otherwise all the other tests will fail
return d.addBoth(restoreConnection)

The cause is almost certainly a quirk in Linux epoll, where if a file descriptor is closed, it gets removed from the watched set automatically (see Q6 in man epoll(2)). The result is that when psycopg2 closes the connection FD, Twisted never see that and the cursor's connectionLost is never called.

What probably will have to happen is that the connection will have to track all its cursors and call connectionLost() manually on all of them when its close() method is called.