# Buggy code
def register_device(self, user_id, device_id=None, initial_display_name=None):
    """Register a device for a user and generate an access token.

    Args:
        user_id (str): full canonical @user:id
        device_id (str|None): The device ID to check, or None to generate
            a new one.
        initial_display_name (str|None): An optional display name for the
            device.

    Returns:
        defer.Deferred[tuple[str, str]]: Tuple of device ID and access token
    """
    return self._hs.get_registration_handler().register_device(
        user_id=user_id,
        device_id=device_id,
        initial_display_name=initial_display_name,
    )


# Traceback (TypeError)
2020-06-11 21:42:52,523 - synapse.http.server - 113 - ERROR -  - Failed handle request via 'LoginRestServlet': <XForwardedForRequest at 0x7f89689119e8 method='POST' uri='/_matrix/client/r0/login' clientproto='HTTP/1.1' site=8008>
Traceback (most recent call last):
File "/app/code/env/lib/python3.6/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 "/app/code/env/lib/python3.6/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 "/app/code/env/lib/python3.6/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 "/app/code/env/lib/python3.6/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 "/app/code/env/lib/python3.6/site-packages/twisted/internet/defer.py", line 1418, in _inlineCallbacks
result = g.send(result)
StopIteration: @girish:cloudron.fun

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/app/code/env/lib/python3.6/site-packages/synapse/http/server.py", line 81, in wrapped_request_handler
await h(self, request)
File "/app/code/env/lib/python3.6/site-packages/synapse/http/server.py", line 350, in _async_render
callback_return = await callback_return
File "/app/code/env/lib/python3.6/site-packages/synapse/rest/client/v1/login.py", line 149, in on_POST
result = await self._do_other_login(login_submission)
File "/app/code/env/lib/python3.6/site-packages/synapse/rest/client/v1/login.py", line 264, in _do_other_login
identifier["user"], login_submission
File "/app/code/env/lib/python3.6/site-packages/synapse/handlers/auth.py", line 722, in validate_login
is_valid = await provider.check_password(qualified_user_id, password)
File "/app/code/env/lib/python3.6/site-packages/twisted/internet/defer.py", line 1416, in _inlineCallbacks
result = result.throwExceptionIntoGenerator(g)
File "/app/code/env/lib/python3.6/site-packages/twisted/python/failure.py", line 512, in throwExceptionIntoGenerator
return g.throw(self.type, self.value, self.tb)
File "/app/code/env/lib/python3.6/site-packages/ldap_auth_provider.py", line 180, in check_password
user_id = yield self.register_user(localpart, givenName, mail)
File "/app/code/env/lib/python3.6/site-packages/twisted/internet/defer.py", line 1416, in _inlineCallbacks
result = result.throwExceptionIntoGenerator(g)
File "/app/code/env/lib/python3.6/site-packages/twisted/python/failure.py", line 512, in throwExceptionIntoGenerator
return g.throw(self.type, self.value, self.tb)
File "/app/code/env/lib/python3.6/site-packages/ldap_auth_provider.py", line 291, in register_user
localpart=localpart, displayname=name, emails=emails,
File "/app/code/env/lib/python3.6/site-packages/twisted/internet/defer.py", line 1418, in _inlineCallbacks
result = g.send(result)
File "/app/code/env/lib/python3.6/site-packages/synapse/module_api/__init__.py", line 113, in register
_, access_token = yield self.register_device(user_id)
TypeError: 'coroutine' object is not iterable

# Fixed code
def register_device(self, user_id, device_id=None, initial_display_name=None):
    """Register a device for a user and generate an access token.

    Args:
        user_id (str): full canonical @user:id
        device_id (str|None): The device ID to check, or None to generate
            a new one.
        initial_display_name (str|None): An optional display name for the
            device.

    Returns:
        defer.Deferred[tuple[str, str]]: Tuple of device ID and access token
    """
    return defer.ensureDeferred(
        self._hs.get_registration_handler().register_device(
            user_id=user_id,
            device_id=device_id,
            initial_display_name=initial_display_name,
        )
    )