Re: Error during garbage collection after calling isready() on a closed connection

Brian Sutherland <[email protected]>
Newsgroups gmane.comp.python.db.psycopg.devel
Message-ID <[email protected]>
On Sat, Jan 10, 2009 at 07:08:24PM +0100, Federico Di Gregorio wrote:
> Il giorno ven, 09/01/2009 alle 17.13 +0200, Brian Sutherland ha scritto:
> > Hi,
> > 
> > Attached is an example script of a nasty little bug I found. It looks
> > like calling .isready() on a server closed connection causes an error
> > during the next garbage collection. That can cause all sorts of havoc on
> > a python process.
> > 
> > I found it in version 2.5.1 and verified that it's still there in 2.0.8.
> 
> Hi,
> 

I've backported your patch to 2.0.5.1 and it seems to work perfectly.
Thanks!

> cursor.isready() now raise an exception if the internal call to libpq
> reports problems. This is probably now _exactly_ what you want because
> the first call to isready() after the disconnection results in a notice
> (correctly appended to connections notices) and not in an error. An
> error will be raised on any successive call that try to use the now
> dropped connection.

Yes, raising an error on the isready() is _exactly_ what I want :)

That way I can dis-connect and try re-connect.

> This fix is available from bzr repository.
> 
> This seems correct to me, because the cursor is ready and is reading
> some "cached" data (the disconnection message) that was sent by the
> server just before dropping the connection.

In my application whether the exception is raised on the first or the
second isready() calls isn't important. In fact, I can't think of a
situation where it's important that the exception be raised exactly on
the first isready() call after the connection is dropped.

> Technical note: even after consuming the input and checking the
> connection (PQconsumeInput and PQisBusy calls), the connection is still
> CONNECTION_OK so on the first call to isready() after the server restart
> we don't have any way to understand what's happening except for parsing
> the notice (unrealiable IMHO).
> 
> federico
> 
> -- 
> Federico Di Gregorio                         http://people.initd.org/fog
> Debian GNU/Linux Developer                                [email protected]
> INIT.D Developer                                           fog-NGVKUo/i/[email protected]
>  And anyone who yells "fork" deserves to get one stuck in them.
>                                                           -- Dan Winship



-- 
Brian Sutherland
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.