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

Unable to catch exception when reconnecting #14

Closed
yuanmwang-wf opened this issue Feb 5, 2019 · 9 comments
Closed

Unable to catch exception when reconnecting #14

yuanmwang-wf opened this issue Feb 5, 2019 · 9 comments

Comments

@yuanmwang-wf
Copy link
Contributor

I don't think it's possible to catch ExceededRetryCount when raised here by connection_lost because it's an isolated task that's never handled?

Example code

async def on_message(frame, message):
    print('on message')
    print(message)
    return True


async def on_error(err):
    print('on error')
    print(err)


async def main():
    client = AioStomp('localhost',
                      '61613',
                      reconnect_max_attempts=0,
                      error_handler=on_error)
    try:
        await client.connect()
    except Exception as e:
        print(repr(e))

    print('Got a connection')
    await asyncio.sleep(10)
    print('Subscribing')

    try:
        sub = client.subscribe('/topic/foo', handler=on_message)
        print(sub.destination)
    except Exception as e:
        print('Exception when subscribing')
        print(repr(e))

if __name__ == '__main__':
    io_loop = asyncio.get_event_loop()
    io_loop.run_until_complete(main())

If I start the broker, run this script then immediately kill the broker, I got stacktraces like this:

2019-02-05 09:34:36,075 - DEBUG - Using selector: KqueueSelector
2019-02-05 09:34:36,075 - DEBUG - connect
2019-02-05 09:34:36,075 - INFO - Connecting to stomp server: localhost:61613
2019-02-05 09:34:36,082 - INFO - Connected
Got a connection
2019-02-05 09:34:39,812 - DEBUG - connection lost
2019-02-05 09:34:39,812 - INFO - Connection lost, will retry.
2019-02-05 09:34:39,813 - INFO - Connecting to stomp server: localhost:61613
2019-02-05 09:34:39,813 - DEBUG - connection lost
2019-02-05 09:34:39,815 - INFO - Connecting to stomp server failed.
2019-02-05 09:34:39,815 - ERROR - All connections attempts failed.
2019-02-05 09:34:39,815 - ERROR - Task exception was never retrieved
future: <Task finished coro=<AioStomp._reconnect() done, defined at /Users/yuanwang/.virtualenvs/frugal-asyncio/lib/python3.7/site-packages/aiostomp/aiostomp.py:132> exception=ExceededRetryCount()>
Traceback (most recent call last):
  File "/Users/yuanwang/.virtualenvs/frugal-asyncio/lib/python3.7/site-packages/aiostomp/aiostomp.py", line 141, in _reconnect
    password=self._password)
  File "/Users/yuanwang/.virtualenvs/frugal-asyncio/lib/python3.7/site-packages/aiostomp/aiostomp.py", line 431, in connect
    ssl=self.ssl_context)
  File "/usr/local/Cellar/python/3.7.0/Frameworks/Python.framework/Versions/3.7/lib/python3.7/asyncio/base_events.py", line 952, in create_connection
    ', '.join(str(exc) for exc in exceptions)))
OSError: Multiple exceptions: [Errno 61] Connect call failed ('::1', 61613), [Errno 61] Connect call failed ('127.0.0.1', 61613)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/yuanwang/.virtualenvs/frugal-asyncio/lib/python3.7/site-packages/aiostomp/aiostomp.py", line 162, in _reconnect
    raise ExceededRetryCount()
aiostomp.errors.ExceededRetryCount
Subscribing
/topic/foo

For background info, what I'm trying to achieve is to catch exceptions like these because I'm trying to use it with Amazon MQ Active/Standby Broker so if it can't connect with one broker it'll need to try connect with the other broker.

Ideally I'd expect error_handler would be called with ExceededRetryCount when this happens, thoughts?

@pedrokiefer
Copy link
Owner

What do you think works best, calling error_handler or having a specific connection_error_handler?

@yuanmwang-wf
Copy link
Contributor Author

Thanks! It doesn't really matter to me - they both make sense considering I can check the exception type in error_handler.

@pedrokiefer
Copy link
Owner

Do you think ExceededRetryCount should have a reference for the AioStomp instance?

@pedrokiefer
Copy link
Owner

@yuanmwang-wf can you please review #16 ?

@yuanmwang-wf
Copy link
Contributor Author

Do you think ExceededRetryCount should have a reference for the AioStomp instance?

It would be nice to have for me, but not necessary.

@yuanmwang-wf can you please review #16 ?

Awesome! I'll pull it down and test locally.

@pedrokiefer
Copy link
Owner

I've added a .ref to ExceededRetryCount. If you have a better name, we can change it :)

@pedrokiefer
Copy link
Owner

I've just merged that branch to master. I'll add some docs / example on how to use it and then release a new version.

@yuanmwang-wf
Copy link
Contributor Author

Sorry something came up and I got distracted, I just pulled the latest commit and confirmed it worked as expected. Thank you so much! 💯

@pedrokiefer
Copy link
Owner

@yuanmwang-wf just released version 1.4.2.

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

2 participants