wulczer/txpostgres — Issue #10: Failures gitting reactor when trying to connect to nonexisting server/with wrong credentials
Author: kowalski
Created: 2011-12-05 11:38:38+00:00
State: open

Hey there,
I was trying to use txpostgres to implement the writer smart enough to handle connection problems. The following code is my setup of the connection:

```python
self._db = self._txpostgres.ConnectionPool(
"postgres", min=1, **self._credentials)

d = self._db.start()
d.addCallbacks(self._connection_established, self._connection_failed)
return d
```

When the credentials are bad I get to _connection_failed as expected (hurray!) with defer.FirstError wrapping OperationError (this is correct by all means). The problem is that the same error is hitting reactor even though I handle it. I can see two types of tracebacks in the logs:

```
Traceback (most recent call last):
File "/usr/local/lib/python2.6/dist-packages/Twisted-10.1.0-py2.6-linux-i686.egg/twisted/internet/selectreactor.py", line 146, in _doReadOrWrite
why = getattr(selectable, method)()
File "/home/kowal/projects/txpostgres/txpostgres/txpostgres.py", line 564, in doRead
_PollingMixin.doRead(self)
File "/home/kowal/projects/txpostgres/txpostgres/txpostgres.py", line 141, in doRead
self.continuePolling()
File "/home/kowal/projects/txpostgres/txpostgres/txpostgres.py", line 113, in continuePolling
state = self.pollable().poll()
psycopg2.OperationalError: asynchronous connection failed
```

and

```
Traceback (most recent call last):
File "/usr/local/lib/python2.6/dist-packages/Twisted-10.1.0-py2.6-linux-i686.egg/twisted/python/log.py", line 84, in callWithLogger
return callWithContext({"system": lp}, func, *args, **kw)
File "/usr/local/lib/python2.6/dist-packages/Twisted-10.1.0-py2.6-linux-i686.egg/twisted/python/log.py", line 69, in callWithContext
return context.call({ILogContext: newCtx}, func, *args, **kw)
File "/usr/local/lib/python2.6/dist-packages/Twisted-10.1.0-py2.6-linux-i686.egg/twisted/python/context.py", line 59, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "/usr/local/lib/python2.6/dist-packages/Twisted-10.1.0-py2.6-linux-i686.egg/twisted/python/context.py", line 37, in callWithContext
return func(*args,**kw)
File "/usr/local/lib/python2.6/dist-packages/Twisted-10.1.0-py2.6-linux-i686.egg/twisted/internet/selectreactor.py", line 156, in _doReadOrWrite
self._disconnectSelectable(selectable, why, method=="doRead")
File "/usr/local/lib/python2.6/dist-packages/Twisted-10.1.0-py2.6-linux-i686.egg/twisted/internet/posixbase.py", line 256, in _disconnectSelectable
selectable.connectionLost(failure.Failure(why))
File "/home/kowal/projects/txpostgres/txpostgres/txpostgres.py", line 175, in connectionLost
self.continuePolling()
File "/home/kowal/projects/txpostgres/txpostgres/txpostgres.py", line 113, in continuePolling
state = self.pollable().poll()
psycopg2.OperationalError: asynchronous connection failed
```

So far my best idea to get around this problem is to use psycopg2 to connect synchronously and after, when I know the server is tehre, use txpostgres. But it's sooo dirty, has timing uses, etc... imo there should be a way to do this cleanly. Any advice?