Search code examples
pythonsslpython-2.7openssl

Python Cannot rebuild SSL socket after putting in Multiprocessing.Queue


I have a server that has one process waiting for connections and then puts the sockets into a queue so worker threads can pick up a connection and communicate with the clients and do whatever.

Receiving process

    while True:
        # Wait for a connection
        print >>sys.stdout, 'waiting for a connection'

        readySocks = select.select(socksToCheck, [], [], d_consts.SELECT_SEC)
        for s in readySocks[0]:
            if s is serverClient:
                sock, client_address = s.accept()
                print >>sys.stdout, 'Client connection from ', client_address

                sockfd = multiprocessing.reduction.reduce_handle(sock.fileno())
                self.cmpQueue.put(inter_proc_msgs.ConMsg(sockfd, client_address))
                print >>sys.stdout, 'Client connection sent for processing: ', client_address

Worker process

        try:
            print '[%s] Checking Queue...' % self.name
            clientConMsg = self.cmpQueue.get(block=False) #throws exception if empty
            print 'something in queue'
            fileDescriptor = multiprocessing.reduction.rebuild_handle(clientConMsg.sockfd )
            newCon = socket.fromfd(fileDescriptor, socket.AF_INET, socket.SOCK_STREAM)
            print self.name, newCon, time.strftime("%a, %d %b %Y %H:%M:%S", time.gmtime())

            #wrap in ssl
            sslsock = ssl.wrap_socket(newCon,
                                 server_side=True,
                                 certfile="newcert.pem",
                                 keyfile="privkey.pem",
                                 ssl_version=ssl.PROTOCOL_SSLv23)
            ##non blocking
            sslsock.setblocking(0)
            #add to the list of client sockets to check with TTL
            self.clientSocketsWTTL.appendleft(self.createTupleSocknTTL(newCon, clientConMsg.clientAdd))

        except Queue.Empty:
            print '[%s] X Nothing in Queue...' % self.name 
            #pass

Now this used to work before I put SSL, but now with SSL I get:

Traceback

Traceback (most recent call last):
  File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
    self.run()
  File "/home/max/workspace/Dealer/src/client_msg_processor.py", line 35, in run
    self.processClientSideTasks()
  File "/home/max/workspace/Dealer/src/client_msg_processor.py", line 66, in processClientSideTasks
    self.testSSL()
  File "/home/max/workspace/Dealer/src/client_msg_processor.py", line 113, in testSSL
    ssl_version=ssl.PROTOCOL_SSLv23)
  File "/usr/lib/python2.7/ssl.py", line 381, in wrap_socket
    ciphers=ciphers)
  File "/usr/lib/python2.7/ssl.py", line 111, in __init__
    socket.__init__(self, _sock=sock._sock)
AttributeError: '_socket.socket' object has no attribute '_sock'

Any idea if this can be fixed? I am desperately trying to avoid having to restructure the whole server.


Solution

  • Doing newCon = socket.socket(_sock=newCon) fixes the problem.

    see http://www.velocityreviews.com/forums/t557014-socket-vs-_socketobject.html