# Buggy code
def render_GET(self, request):
    """
    .. http:get:: /torrents/(string: torrent infohash)/health

    Fetch the swarm health of a specific torrent. You can optionally specify the timeout to be used in the
    connections to the trackers. This is by default 20 seconds.
    By default, we will not check the health of a torrent again if it was recently checked. You can force a health
    recheck by passing the refresh parameter.

        **Example request**:

        .. sourcecode:: none

            curl http://localhost:8085/torrents/97d2d8f5d37e56cfaeaae151d55f05b077074779/health?timeout=15&refresh=1

        **Example response**:

        .. sourcecode:: javascript

            {
                "http://mytracker.com:80/announce": [{
                    "seeders": 43,
                    "leechers": 20,
                    "infohash": "97d2d8f5d37e56cfaeaae151d55f05b077074779"
                }],
                "http://nonexistingtracker.com:80/announce": {
                    "error": "timeout"
                }
            }

        :statuscode 404: if the torrent is not found in the database
    """
    timeout = 20
    if "timeout" in request.args:
        timeout = int(request.args["timeout"][0])

    refresh = False
    if (
        "refresh" in request.args
        and len(request.args["refresh"]) > 0
        and request.args["refresh"][0] == "1"
    ):
        refresh = True

    torrent_db_columns = [
        "C.torrent_id",
        "num_seeders",
        "num_leechers",
        "next_tracker_check",
    ]
    torrent_info = self.torrent_db.getTorrent(
        self.infohash.decode("hex"), torrent_db_columns
    )

    if torrent_info is None:
        request.setResponseCode(http.NOT_FOUND)
        return json.dumps({"error": "torrent not found in database"})

    def on_health_result(result):
        request.write(json.dumps({"health": result}))
        self.finish_request(request)

    def on_request_error(failure):
        request.setResponseCode(http.BAD_REQUEST)
        request.write(json.dumps({"error": failure.getErrorMessage()}))
        self.finish_request(request)

    self.session.check_torrent_health(
        self.infohash.decode("hex"), timeout=timeout, scrape_now=refresh
    ).addCallback(on_health_result).addErrback(on_request_error)

    return NOT_DONE_YET


# Traceback (RuntimeError)
Traceback (most recent call last):
File "/usr/share/tribler/TriblerGUI/event_request_manager.py", line 99, in on_read_data
raise RuntimeError(json_dict["event"]["text"])
RuntimeError: [Failure instance: Traceback: <type 'exceptions.AttributeError'>: 'NoneType' object has no attribute 'writeSequence'
/usr/lib/python2.7/site-packages/twisted/internet/defer.py:653:_runCallbacks
/usr/lib/python2.7/site-packages/twisted/internet/defer.py:1117:_cbDeferred
/usr/lib/python2.7/site-packages/twisted/internet/defer.py:459:callback
/usr/lib/python2.7/site-packages/twisted/internet/defer.py:567:_startRunCallbacks
--- <exception caught here> ---
/usr/lib/python2.7/site-packages/twisted/internet/defer.py:653:_runCallbacks
/usr/share/tribler/Tribler/Core/Modules/restapi/torrents_endpoint.py:218:on_request_error
/usr/lib/python2.7/site-packages/twisted/web/server.py:234:write
/usr/lib/python2.7/site-packages/twisted/web/http.py:1084:write
]

# Fixed code
def render_GET(self, request):
    """
    .. http:get:: /torrents/(string: torrent infohash)/health

    Fetch the swarm health of a specific torrent. You can optionally specify the timeout to be used in the
    connections to the trackers. This is by default 20 seconds.
    By default, we will not check the health of a torrent again if it was recently checked. You can force a health
    recheck by passing the refresh parameter.

        **Example request**:

        .. sourcecode:: none

            curl http://localhost:8085/torrents/97d2d8f5d37e56cfaeaae151d55f05b077074779/health?timeout=15&refresh=1

        **Example response**:

        .. sourcecode:: javascript

            {
                "http://mytracker.com:80/announce": [{
                    "seeders": 43,
                    "leechers": 20,
                    "infohash": "97d2d8f5d37e56cfaeaae151d55f05b077074779"
                }],
                "http://nonexistingtracker.com:80/announce": {
                    "error": "timeout"
                }
            }

        :statuscode 404: if the torrent is not found in the database
    """
    timeout = 20
    if "timeout" in request.args:
        timeout = int(request.args["timeout"][0])

    refresh = False
    if (
        "refresh" in request.args
        and len(request.args["refresh"]) > 0
        and request.args["refresh"][0] == "1"
    ):
        refresh = True

    torrent_db_columns = [
        "C.torrent_id",
        "num_seeders",
        "num_leechers",
        "next_tracker_check",
    ]
    torrent_info = self.torrent_db.getTorrent(
        self.infohash.decode("hex"), torrent_db_columns
    )

    if torrent_info is None:
        request.setResponseCode(http.NOT_FOUND)
        return json.dumps({"error": "torrent not found in database"})

    def on_health_result(result):
        request.write(json.dumps({"health": result}))
        self.finish_request(request)

    def on_request_error(failure):
        if not request.finished:
            request.setResponseCode(http.BAD_REQUEST)
            request.write(json.dumps({"error": failure.getErrorMessage()}))
        # If the above request.write failed, the request will have already been finished
        if not request.finished:
            self.finish_request(request)

    self.session.check_torrent_health(
        self.infohash.decode("hex"), timeout=timeout, scrape_now=refresh
    ).addCallback(on_health_result).addErrback(on_request_error)

    return NOT_DONE_YET