# Buggy code
def __init__(self, hs):
    self.store = hs.get_datastore()
    self.federation = hs.get_federation_client()
    self.device_handler = hs.get_device_handler()
    self.is_mine = hs.is_mine
    self.clock = hs.get_clock()

    self._edu_updater = SigningKeyEduUpdater(hs, self)

    self._is_master = hs.config.worker_app is None
    if not self._is_master:
        self._user_device_resync_client = (
            ReplicationUserDevicesResyncRestServlet.make_client(hs)
        )

    federation_registry = hs.get_federation_registry()

    # FIXME: switch to m.signing_key_update when MSC1756 is merged into the spec
    federation_registry.register_edu_handler(
        "org.matrix.signing_key_update",
        self._edu_updater.incoming_signing_key_update,
    )
    # doesn't really work as part of the generic query API, because the
    # query request requires an object POST, but we abuse the
    # "query handler" interface.
    federation_registry.register_query_handler(
        "client_keys", self.on_federation_query_client_keys
    )


# Traceback (AttributeError)
2020-04-08 09:38:48,601 - synapse.federation.federation_server - 781 - ERROR - PUT-22124562 - Failed to handle edu 'org.matrix.signing_key_update'
Capture point (most recent call last):
  File "/usr/local/lib/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/local/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/synapse/src/synapse/app/federation_reader.py", line 24, in <module>
    start(sys.argv[1:])
  File "/home/synapse/src/synapse/app/generic_worker.py", line 930, in start
    _base.start_worker_reactor("synapse-generic-worker", config)
  File "/home/synapse/src/synapse/app/_base.py", line 77, in start_worker_reactor
    run_command=run_command,
  File "/home/synapse/src/synapse/app/_base.py", line 137, in start_reactor
    daemon.start()
  File "/home/synapse/env-py37/lib/python3.7/site-packages/daemonize.py", line 248, in start
    self.action(*privileged_action_result)
  File "/home/synapse/src/synapse/app/_base.py", line 114, in run
    run_command()
  File "/home/synapse/env-py37/lib/python3.7/site-packages/twisted/internet/base.py", line 1283, in run
    self.mainLoop()
  File "/home/synapse/env-py37/lib/python3.7/site-packages/twisted/internet/base.py", line 1292, in mainLoop
    self.runUntilCurrent()
  File "/home/synapse/src/synapse/metrics/__init__.py", line 436, in f
    ret = func(*args, **kwargs)
  File "/home/synapse/env-py37/lib/python3.7/site-packages/twisted/internet/base.py", line 886, in runUntilCurrent
    f(*a, **kw)
  File "/home/synapse/env-py37/lib/python3.7/site-packages/twisted/internet/defer.py", line 460, in callback
    self._startRunCallbacks(result)
  File "/home/synapse/env-py37/lib/python3.7/site-packages/twisted/internet/defer.py", line 568, in _startRunCallbacks
    self._runCallbacks()
  File "/home/synapse/env-py37/lib/python3.7/site-packages/twisted/internet/defer.py", line 654, in _runCallbacks
    current.result = callback(current.result, *args, **kw)
  File "/home/synapse/env-py37/lib/python3.7/site-packages/twisted/internet/defer.py", line 1475, in gotResult
    _inlineCallbacks(r, g, status)
  File "/home/synapse/env-py37/lib/python3.7/site-packages/twisted/internet/defer.py", line 1421, in _inlineCallbacks
    status.deferred.callback(getattr(e, "value", None))
  File "/home/synapse/env-py37/lib/python3.7/site-packages/twisted/internet/defer.py", line 460, in callback
    self._startRunCallbacks(result)
  File "/home/synapse/env-py37/lib/python3.7/site-packages/twisted/internet/defer.py", line 568, in _startRunCallbacks
    self._runCallbacks()
  File "/home/synapse/env-py37/lib/python3.7/site-packages/twisted/internet/defer.py", line 654, in _runCallbacks
    current.result = callback(current.result, *args, **kw)
  File "/home/synapse/env-py37/lib/python3.7/site-packages/twisted/internet/defer.py", line 1475, in gotResult
    _inlineCallbacks(r, g, status)
  File "/home/synapse/env-py37/lib/python3.7/site-packages/twisted/internet/defer.py", line 1421, in _inlineCallbacks
    status.deferred.callback(getattr(e, "value", None))
  File "/home/synapse/env-py37/lib/python3.7/site-packages/twisted/internet/defer.py", line 460, in callback
    self._startRunCallbacks(result)
  File "/home/synapse/env-py37/lib/python3.7/site-packages/twisted/internet/defer.py", line 568, in _startRunCallbacks
    self._runCallbacks()
  File "/home/synapse/env-py37/lib/python3.7/site-packages/twisted/internet/defer.py", line 654, in _runCallbacks
    current.result = callback(current.result, *args, **kw)
  File "/home/synapse/env-py37/lib/python3.7/site-packages/twisted/internet/defer.py", line 1475, in gotResult
    _inlineCallbacks(r, g, status)
  File "/home/synapse/env-py37/lib/python3.7/site-packages/twisted/internet/defer.py", line 1418, in _inlineCallbacks
    result = g.send(result)
  File "/home/synapse/src/synapse/http/server.py", line 209, in wrapped_async_request_handler
    await h(self, request)
  File "/home/synapse/src/synapse/http/server.py", line 78, in wrapped_request_handler
    await h(self, request)
  File "/home/synapse/src/synapse/http/server.py", line 331, in _async_render
    callback_return = await callback_return
  File "/home/synapse/src/synapse/federation/transport/server.py", line 344, in new_func
    origin, content, request.args, *args, **kwargs
  File "/home/synapse/src/synapse/federation/transport/server.py", line 424, in on_PUT
    origin, transaction_data
  File "/home/synapse/src/synapse/federation/federation_server.py", line 130, in on_incoming_transaction
    origin, transaction, request_time
  File "/home/synapse/src/synapse/federation/federation_server.py", line 179, in _handle_incoming_transaction
    run_in_background(self._handle_edus_in_txn, origin, transaction),
  File "/home/synapse/src/synapse/logging/context.py", line 616, in run_in_background
    res = defer.ensureDeferred(res)
  File "/home/synapse/env-py37/lib/python3.7/site-packages/twisted/internet/defer.py", line 911, in ensureDeferred
    return _cancellableInlineCallbacks(coro)
  File "/home/synapse/env-py37/lib/python3.7/site-packages/twisted/internet/defer.py", line 1529, in _cancellableInlineCallbacks
    _inlineCallbacks(None, g, status)
  File "/home/synapse/env-py37/lib/python3.7/site-packages/twisted/internet/defer.py", line 1418, in _inlineCallbacks
    result = g.send(result)
  File "/home/synapse/src/synapse/federation/federation_server.py", line 310, in _handle_edus_in_txn
    TRANSACTION_CONCURRENCY_LIMIT,
  File "/home/synapse/src/synapse/util/async_helpers.py", line 165, in concurrently_execute
    [run_in_background(_concurrently_execute_inner) for _ in range(limit)],
  File "/home/synapse/src/synapse/util/async_helpers.py", line 165, in <listcomp>
    [run_in_background(_concurrently_execute_inner) for _ in range(limit)],
  File "/home/synapse/src/synapse/logging/context.py", line 616, in run_in_background
    res = defer.ensureDeferred(res)
  File "/home/synapse/env-py37/lib/python3.7/site-packages/twisted/internet/defer.py", line 911, in ensureDeferred
    return _cancellableInlineCallbacks(coro)
  File "/home/synapse/env-py37/lib/python3.7/site-packages/twisted/internet/defer.py", line 1529, in _cancellableInlineCallbacks
    _inlineCallbacks(None, g, status)
  File "/home/synapse/env-py37/lib/python3.7/site-packages/twisted/internet/defer.py", line 1418, in _inlineCallbacks
    result = g.send(result)
  File "/home/synapse/src/synapse/util/async_helpers.py", line 159, in _concurrently_execute_inner
    await maybe_awaitable(func(next(it)))
  File "/home/synapse/src/synapse/federation/federation_server.py", line 305, in _process_edu
    await self.registry.on_edu(edu.edu_type, origin, edu.content)
  File "/home/synapse/src/synapse/federation/federation_server.py", line 819, in on_edu
    edu_type, origin, content
Traceback (most recent call last):
  File "/home/synapse/env-py37/lib/python3.7/site-packages/twisted/internet/defer.py", line 1418, in _inlineCallbacks
    result = g.send(result)
StopIteration

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/synapse/env-py37/lib/python3.7/site-packages/twisted/internet/defer.py", line 1418, in _inlineCallbacks
    result = g.send(result)
StopIteration

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/synapse/src/synapse/federation/federation_server.py", line 777, in on_edu
    await handler(origin, content)
  File "/home/synapse/env-py37/lib/python3.7/site-packages/twisted/internet/defer.py", line 749, in send
    raise result.value
AttributeError: 'GenericWorkerSlavedStore' object has no attribute 'set_e2e_cross_signing_key'

# Fixed code
def __init__(self, hs):
    self.store = hs.get_datastore()
    self.federation = hs.get_federation_client()
    self.device_handler = hs.get_device_handler()
    self.is_mine = hs.is_mine
    self.clock = hs.get_clock()

    self._edu_updater = SigningKeyEduUpdater(hs, self)

    federation_registry = hs.get_federation_registry()

    self._is_master = hs.config.worker_app is None
    if not self._is_master:
        self._user_device_resync_client = (
            ReplicationUserDevicesResyncRestServlet.make_client(hs)
        )
    else:
        # Only register this edu handler on master as it requires writing
        # device updates to the db
        #
        # FIXME: switch to m.signing_key_update when MSC1756 is merged into the spec
        federation_registry.register_edu_handler(
            "org.matrix.signing_key_update",
            self._edu_updater.incoming_signing_key_update,
        )

    # doesn't really work as part of the generic query API, because the
    # query request requires an object POST, but we abuse the
    # "query handler" interface.
    federation_registry.register_query_handler(
        "client_keys", self.on_federation_query_client_keys
    )