# Buggy code
async def on_POST(self, request):
    await self.auth.get_user_by_req(request, allow_guest=True)

    server = parse_string(request, "server", default=None)
    content = parse_json_object_from_request(request)

    limit = int(content.get("limit", 100))  # type: Optional[int]
    since_token = content.get("since", None)
    search_filter = content.get("filter", None)

    include_all_networks = content.get("include_all_networks", False)
    third_party_instance_id = content.get("third_party_instance_id", None)

    if include_all_networks:
        network_tuple = None
        if third_party_instance_id is not None:
            raise SynapseError(
                400, "Can't use include_all_networks with an explicit network"
            )
    elif third_party_instance_id is None:
        network_tuple = ThirdPartyInstanceID(None, None)
    else:
        network_tuple = ThirdPartyInstanceID.from_string(third_party_instance_id)

    if limit == 0:
        # zero is a special value which corresponds to no limit.
        limit = None

    handler = self.hs.get_room_list_handler()
    if server:
        data = await handler.get_remote_public_room_list(
            server,
            limit=limit,
            since_token=since_token,
            search_filter=search_filter,
            include_all_networks=include_all_networks,
            third_party_instance_id=third_party_instance_id,
        )
    else:
        data = await handler.get_local_public_room_list(
            limit=limit,
            since_token=since_token,
            search_filter=search_filter,
            network_tuple=network_tuple,
        )

    return 200, data


# Traceback (AttributeError)
2019-11-04 19:52:26,074 - synapse.http.server - 109 - ERROR - POST-705775- Failed handle request via 'PublicRoomListRestServlet': <XForwardedForRequest at 0x80e5ef048 method='POST' uri='/_matrix/client/r0/publicRooms?server=domain.tld&amp;access_token=<redacted>' clientproto='HTTP/1.1' site=8008>
Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/synapse/http/server.py", line 77, in wrapped_request_handler
await h(self, request)
File "/usr/local/lib/python3.6/site-packages/synapse/http/server.py", line 326, in _async_render
callback_return = await callback_return
File "/usr/local/lib/python3.6/site-packages/synapse/rest/client/v1/room.py", line 417, in on_POST
third_party_instance_id=third_party_instance_id,
File "/usr/local/lib/python3.6/site-packages/twisted/internet/defer.py", line 1418, in _inlineCallbacks
result = g.send(result)
File "/usr/local/lib/python3.6/site-packages/synapse/handlers/room_list.py", line 391, in get_remote_public_room_list
third_party_instance_id=third_party_instance_id,
File "/usr/local/lib/python3.6/site-packages/synapse/handlers/room_list.py", line 441, in _get_remote_list_cached
third_party_instance_id=third_party_instance_id,
File "/usr/local/lib/python3.6/site-packages/synapse/util/caches/response_cache.py", line 151, in wrap
result = self.set(key, d)
File "/usr/local/lib/python3.6/site-packages/synapse/util/caches/response_cache.py", line 95, in set
result = ObservableDeferred(deferred, consumeErrors=True)
File "/usr/local/lib/python3.6/site-packages/synapse/util/async_helpers.py", line 87, in __init__
deferred.addCallbacks(callback, errback)
AttributeError: 'NoneType' object has no attribute 'addCallbacks'

# Fixed code
async def on_POST(self, request):
    await self.auth.get_user_by_req(request, allow_guest=True)

    server = parse_string(request, "server", default=None)
    content = parse_json_object_from_request(request)

    limit = int(content.get("limit", 100))  # type: Optional[int]
    since_token = content.get("since", None)
    search_filter = content.get("filter", None)

    include_all_networks = content.get("include_all_networks", False)
    third_party_instance_id = content.get("third_party_instance_id", None)

    if include_all_networks:
        network_tuple = None
        if third_party_instance_id is not None:
            raise SynapseError(
                400, "Can't use include_all_networks with an explicit network"
            )
    elif third_party_instance_id is None:
        network_tuple = ThirdPartyInstanceID(None, None)
    else:
        network_tuple = ThirdPartyInstanceID.from_string(third_party_instance_id)

    if limit == 0:
        # zero is a special value which corresponds to no limit.
        limit = None

    handler = self.hs.get_room_list_handler()
    if server and server != self.hs.config.server_name:
        try:
            data = await handler.get_remote_public_room_list(
                server,
                limit=limit,
                since_token=since_token,
                search_filter=search_filter,
                include_all_networks=include_all_networks,
                third_party_instance_id=third_party_instance_id,
            )
        except HttpResponseException as e:
            raise e.to_synapse_error()
    else:
        data = await handler.get_local_public_room_list(
            limit=limit,
            since_token=since_token,
            search_filter=search_filter,
            network_tuple=network_tuple,
        )

    return 200, data