Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disconnecting a client from Redis leads to a ConnectionError #25

Open
Chri opened this issue Apr 25, 2012 · 7 comments
Open

Disconnecting a client from Redis leads to a ConnectionError #25

Chri opened this issue Apr 25, 2012 · 7 comments

Comments

@Chri
Copy link

Chri commented Apr 25, 2012

When I close the WebSocket my python program prints out the following error:

ERROR:brukva.client:Connection lost
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/brukva/client.py", line 926, in listen
data = yield async(self.connection.readline)()
GeneratorExit: Connection lost
Exception AttributeError: "'ConnectionError' object has no attribute 'body'" in <generator object listen at 0x90a798c> ignored

Although the connection to redis gets closed I still receive that error.

Here a snippet of the code I use:

import tornado.httpserver
import tornado.web
import tornado.websocket
import tornado.ioloop
import brukva

c = brukva.Client()
c.connect()

class WSHandler(tornado.websocket.WebSocketHandler):
    def __init__(self, *args, **kwargs):
        super(WSHandler, self).__init__(*args, **kwargs)
        self.client = brukva.Client()
        self.client.connect()
        self.client.subscribe('measurements')

    def open(self):
        self.client.listen(self.on_message)
        print('new connection')

    def on_message(self, message):
        self.write_message(str(message.body))
        print('message received %s' %message.body)

    def on_close(self):
        self.client.unsubscribe('measurements')
        self.client.disconnect()
        print('connection closed')
@fawzyj
Copy link

fawzyj commented May 5, 2012

+1 , this happens to me also when using tornadio2,
any work around?

@lud4ik
Copy link

lud4ik commented Jul 25, 2012

+1

1 similar comment
@nellessen
Copy link

+1

@nellessen
Copy link

Seems like unsubscribing is done asynchronously and the connection is closed before unsubscribing finishes which includes sending messages to the subscriptions.

My workaround is to delay disconnect():

self.client.unsubscribe('foo')
# Disconnect connection after delay due to this issue:  https://github.com/evilkost/brukva/issues/25
t = Timer(0.1, self.client.disconnect)
t.start()

Not the nicest one though...

@scragg0x
Copy link

+1 ouch, a timer, the problem can still occur if it takes longer than the block period

@scragg0x
Copy link

    def on_close(self):
        self.pubsub.unsubscribe('foo')
        def check():
            if self.pubsub.connection.in_progress:
                print 'Connection still in progress'
                ioloop.IOLoop.instance().add_timeout(datetime.timedelta(0.00001), check)
            else:
                print 'Disconnecting'
                self.pubsub.disconnect()
        ioloop.IOLoop.instance().add_timeout(datetime.timedelta(0.00001), check)

This should work. It makes sure the connection is ready to be disconnected without blocking. It also might work on the disconnect method in brukva but I'm not sure the implications. It might be tweaked by forcing the disconnect after a certain amount of time or iterations.

@ahmpro
Copy link

ahmpro commented May 26, 2013

Same. Probably it isn't bug but feature?
Will someone fix it in source? Code below doesn't look well but it works.

def on_close(self):
    def after_unsubscribe(a):
        self.client.disconnect()

    self.client.unsubscribe('some_channel', after_unsubscribe)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants