smacintyre/asyncmongo — pull request #1 by philwhln (2011-12-06 05:45:20+00:00)
Title: fixed bug with "Adding support for Replica Sets"

Hi smacintyre,

I used your code from Replica Sets, but I came across this bug.

Connections are returned to the idle pool prematurely. For instance, when the message is sent to check if it ismaster. After the ismaster check _parse_response returns the connection to the pool, even though __handle_ismaster goes on to use the connection with send_message for the original message.

So, if this connection retrieved from the pool by someone else before the 2nd send_message has finished, it sees connection.__callback is set and raises ProgrammingError('connection already in use').

This is what I was seeing.

Unfortunately this nice ProgrammingError is lost along the way. Due the error, the connection is closed and an exception "connection closed" is what we see. This is misleading, but I thought I'd focus on just the above issue for now.

btw, my setup was 2 replicas + 1 arbiter. I had stopped the first replica, so this consistently failed when I ran my batch script.

Thanks for implementing this Replica Sets code!

Cheers,
Phil