[mod_python] socket.py ssl() method problem in mod_python

Huzaifa Tapal huzaifa at hostway.com
Wed Jul 6 11:11:11 EDT 2005


Hello All,

I recently came across a problem in making secure socket connections 
only introduced in the mod_python (persistent) version of my 
application.  It seems that after a number of successful socket 
connections, somehow and in someway the interpreter gets corrupted and 
start receiving an error stating "socket object is not of type _sock" 
coming from the socket.py module in python.

To further explain, in httplib.py the connect method in HTTPSConnection, 
creates a socket instance and then passes it the ssl() method in 
socket.py to make it a secure socket connection.  That is where the 
problem occurs and somehow the ssl() method starts claiming that its 
receiving a bad type of socket object.

After a whole lot of poking around I think the problem is in socket.py's 
implementation of the modular level ssl() method and that it is not 
thread safe.  Here is what the ssl() method looks like:

    def ssl(sock, keyfile=None, certfile=None):
         if hasattr(sock, "_sock"):
             sock = sock._sock
         return _realssl(sock, keyfile, certfile)


I thought that the hasattr() call and then the referencing of the passed 
socket object was somehow corrupting the mod_python interpreter and 
thereafter any ssl() socket connections were failing with that error.  
So I made a patch to httplib.py's connect() method to pass in a 
socket._sock to the ssl() method instead so that the ssl() method 
doesn't have to do the switch and that seems to work fine.

After that successful attempt, we tried to determine, whether hasattr 
check was need at all and that we can simply just modify the ssl() 
method to be as following:

    def ssl(sock, keyfile=None, certfile=None):
         return _realssl(sock._sock, keyfile, certfile)


That reintroduced the original error.  At this point I am stumped as to 
what could be causing this type of interpreter corruption to happen for 
the ssl() method.  I know the fact that the ssl() method is global in 
the socket.py module has something to do with it but I can't figure it 
how does the passed sock object be corrupted everytime.

Does anybody have any insight into this problem or has run into this 
problem at all?

Hozi




More information about the Mod_python mailing list