majek / puka

Puka - the opinionated RabbitMQ client
https://github.com/majek/puka
Other
182 stars 34 forks source link

fix non-blocking TLS connect #63

Closed jmptbl closed 9 years ago

jmptbl commented 9 years ago

I love how puka provides the freedom for me to write a truly non-blocking, asynchronous, single threaded app, but I was having a major headache with performing TLS connections like this. Below sample code demonstrates what I'm trying to do.

import puka
import select

def on_connection (promise, result):
        print 'on_connection %r' % result

client = puka.Client('amqps:///')

client.connect(callback=on_connection)
fd = client.fileno()

while True:
        exception = readers = [fd]
        if client.needs_write():
                writers = [fd]
        else:
                writers = []
        rready, wready, xready = select.select(readers, writers, exception)
        if len(rready) or len(xready):
                client.on_read()
        if len(wready):
                client.on_write()
        client.run_any_callbacks()

But in the case of TLS enabled brokers, I would randomly get the following traceback during start up.

on_connection {}
Traceback (most recent call last):
  File "sub2.py", line 19, in <module>
    rready, wready, xready = select.select(readers, writers, exception)
select.error: (9, 'Bad file descriptor')

There might be room to remove some of the TLS handshaking code, but for now this fixes my problem.