# Buggy code
def on_authorize_success(authorization):
    # the call to authorize the action _itself_ succeeded. now go on depending on whether
    # the action was actually authorized or not ..
    #
    if not authorization["allow"]:
        reply = message.Error(
            message.Call.MESSAGE_TYPE,
            call.request,
            ApplicationError.NOT_AUTHORIZED,
            [
                "session is not authorized to call procedure '{0}'".format(
                    call.procedure
                )
            ],
        )
        reply.correlation_id = call.correlation_id
        reply.correlation_uri = call.procedure
        reply.correlation_is_anchor = False
        reply.correlation_is_last = True
        self._router.send(session, reply)

    else:
        # get registrations active on the procedure called
        #
        registration = self._registration_map.best_matching_observation(call.procedure)

        if registration:
            # validate payload (skip in "payload_transparency" mode)
            #
            if call.payload is None:
                try:
                    self._router.validate(
                        "call", call.procedure, call.args, call.kwargs
                    )
                except Exception as e:
                    reply = message.Error(
                        message.Call.MESSAGE_TYPE,
                        call.request,
                        ApplicationError.INVALID_ARGUMENT,
                        [
                            "call of procedure '{0}' with invalid application payload: {1}".format(
                                call.procedure, e
                            )
                        ],
                    )
                    reply.correlation_id = call.correlation_id
                    reply.correlation_uri = call.procedure
                    reply.correlation_is_anchor = False
                    reply.correlation_is_last = True
                    self._router.send(session, reply)
                    return

            # now actually perform the invocation of the callee ..
            #
            self._call(session, call, registration, authorization)
        else:
            reply = message.Error(
                message.Call.MESSAGE_TYPE,
                call.request,
                ApplicationError.NO_SUCH_PROCEDURE,
                ["no callee registered for procedure <{0}>".format(call.procedure)],
            )
            reply.correlation_id = call.correlation_id
            reply.correlation_uri = call.procedure
            reply.correlation_is_anchor = False
            reply.correlation_is_last = True
            self._router.send(session, reply)


# Traceback (builtins.KeyError)
Unhandled error in Deferred:
Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/autobahn/wamp/protocol.py", line 888, in onMessage
txaio.resolve(on_reply, msg.args[0])
File "/usr/local/lib/python3.6/dist-packages/txaio/tx.py", line 468, in resolve
future.callback(result)
File "/usr/local/lib/python3.6/dist-packages/twisted/internet/defer.py", line 460, in callback
self._startRunCallbacks(result)
File "/usr/local/lib/python3.6/dist-packages/twisted/internet/defer.py", line 568, in _startRunCallbacks
self._runCallbacks()
--- <exception caught here> ---
File "/usr/local/lib/python3.6/dist-packages/twisted/internet/defer.py", line 654, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "/usr/local/lib/python3.6/dist-packages/crossbar/router/dealer.py", line 441, in on_authorize_success
self._session_to_registrations[session].add(registration)
builtins.KeyError: <crossbar.router.session.RouterSession object at 0x7f653abfaef0>

# Fixed code
def on_authorize_success(authorization):
    # the call to authorize the action _itself_ succeeded. now go on depending on whether
    # the action was actually authorized or not ..
    #
    if not authorization["allow"]:
        reply = message.Error(
            message.Call.MESSAGE_TYPE,
            call.request,
            ApplicationError.NOT_AUTHORIZED,
            [
                "session is not authorized to call procedure '{0}'".format(
                    call.procedure
                )
            ],
        )
        reply.correlation_id = call.correlation_id
        reply.correlation_uri = call.procedure
        reply.correlation_is_anchor = False
        reply.correlation_is_last = True
        self._router.send(session, reply)

    else:
        # get registrations active on the procedure called
        #
        registration = self._registration_map.best_matching_observation(call.procedure)

        # if the session disconencted while the authorization
        # was being checked, 'registration' will be None and
        # we'll (correctly) fire an error.

        if registration:
            # validate payload (skip in "payload_transparency" mode)
            #
            if call.payload is None:
                try:
                    self._router.validate(
                        "call", call.procedure, call.args, call.kwargs
                    )
                except Exception as e:
                    reply = message.Error(
                        message.Call.MESSAGE_TYPE,
                        call.request,
                        ApplicationError.INVALID_ARGUMENT,
                        [
                            "call of procedure '{0}' with invalid application payload: {1}".format(
                                call.procedure, e
                            )
                        ],
                    )
                    reply.correlation_id = call.correlation_id
                    reply.correlation_uri = call.procedure
                    reply.correlation_is_anchor = False
                    reply.correlation_is_last = True
                    self._router.send(session, reply)
                    return

            # now actually perform the invocation of the callee ..
            #
            self._call(session, call, registration, authorization)
        else:
            reply = message.Error(
                message.Call.MESSAGE_TYPE,
                call.request,
                ApplicationError.NO_SUCH_PROCEDURE,
                ["no callee registered for procedure <{0}>".format(call.procedure)],
            )
            reply.correlation_id = call.correlation_id
            reply.correlation_uri = call.procedure
            reply.correlation_is_anchor = False
            reply.correlation_is_last = True
            self._router.send(session, reply)