Search code examples
python-asyncioautobahn

How to raise exception in autobahn python ApplicationSession.__init__?


I'm trying to use autobahn-0.16.0, with the ApplicationRunner, as shown in this example on github.

If I raise an Exception after join() is called, everything works as expected. However, if the exception is raised in the __init__ method, I get AttributeError: 'ApplicationRunner' object has no attribute 'log'. I wonder if there's a way to make this work.

The code below is an example that reproduces the problem.

#!/usr/bin/python3

import txaio

import asyncio 
from asyncio import coroutine
from autobahn.asyncio.wamp import ApplicationSession
from autobahn.asyncio.wamp import ApplicationRunner

class SessionWithException(ApplicationSession):
    def __init__(self, config):
        ApplicationSession.__init__(self, config)
#        raise Exception('Some error exception')

    def onConnect(self):
        print("Connecting...")
        self.join(self.config.realm)
        raise Exception('Good exception')

    @coroutine
    def onJoin(self, details):
        print("Joining...")
        self.disconnect()

    def onDisconnect(self):
        print("Disconnecting...")
        asyncio.get_event_loop().stop()

def main():
    txaio.start_logging(level='error')

    runner = ApplicationRunner("wss://api.poloniex.com:443", "realm1")
    runner.run(SessionWithException)

if __name__ == "__main__":
    main()

When I out-comment the raise in __init__, I get this error:

Traceback (most recent call last):
  File "/usr/lib/python3.4/site-packages/autobahn/wamp/websocket.py", line 60, in onOpen
    self._session = self.factory._factory()
  File "/usr/lib/python3.4/site-packages/autobahn/asyncio/wamp.py", line 125, in create
    self.log.failure("App session could not be created! ")
AttributeError: 'ApplicationRunner' object has no attribute 'log'

Solution

  • I believe it's a subtle bug in Autobahn itself. The library should initialize all runner's attributes early. Please file an issue on bug tracker