# Buggy code
def render_DELETE(self, request):
    """
    .. http:delete:: /downloads/(string: infohash)

    A DELETE request to this endpoint removes a specific download from Tribler. You can specify whether you only
    want to remove the download or the download and the downloaded data using the remove_data parameter.

        **Example request**:

            .. sourcecode:: none

                curl -X DELETE http://localhost:8085/download/4344503b7e797ebf31582327a5baae35b11bda01
                --data "remove_data=1"

        **Example response**:

            .. sourcecode:: javascript

                {"removed": True}
    """
    parameters = http.parse_qs(request.content.read(), 1)

    if "remove_data" not in parameters or len(parameters["remove_data"]) == 0:
        request.setResponseCode(http.BAD_REQUEST)
        return json.dumps({"error": "remove_data parameter missing"})

    download = self.session.get_download(self.infohash)
    if not download:
        return DownloadSpecificEndpoint.return_404(request)

    remove_data = parameters["remove_data"][0] == "1"

    def _on_torrent_removed(_):
        """
        Success callback
        """
        request.write(json.dumps({"removed": True}))
        request.finish()

    def _on_remove_failure(failure):
        """
        Error callback
        :param failure: from remove_download
        """
        self._logger.exception(failure)
        request.write(return_handled_exception(request, failure.value))
        request.finish()

    deferred = self.session.remove_download(download, remove_content=remove_data)
    deferred.addCallback(_on_torrent_removed)
    deferred.addErrback(_on_remove_failure)

    return NOT_DONE_YET


# Traceback (exceptions.RuntimeError)
Unhandled error in Deferred:


Traceback (most recent call last):
File "/opt/tribler/Tribler/Core/Libtorrent/LibtorrentMgr.py", line 487, in _task_process_alerts
self.process_alert(alert)
File "/opt/tribler/Tribler/Core/Libtorrent/LibtorrentMgr.py", line 336, in process_alert
self.torrents[info_hash][0].deferred_removed.callback(None)
File "/usr/lib/python2.7/dist-packages/twisted/internet/defer.py", line 393, in callback
self._startRunCallbacks(result)
File "/usr/lib/python2.7/dist-packages/twisted/internet/defer.py", line 501, in _startRunCallbacks
self._runCallbacks()
--- <exception caught here> ---
File "/usr/lib/python2.7/dist-packages/twisted/internet/defer.py", line 588, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "/opt/tribler/Tribler/Core/Modules/restapi/downloads_endpoint.py", line 336, in _on_remove_failure
request.finish()
File "/usr/lib/python2.7/dist-packages/twisted/web/server.py", line 224, in finish
return http.Request.finish(self)
File "/usr/lib/python2.7/dist-packages/twisted/web/http.py", line 865, in finish
"Request.finish called on a request after its connection was lost; "
exceptions.RuntimeError: Request.finish called on a request after its connection was lost; use Request.notifyFinish to keep track of this.
ERROR:root:Traceback (most recent call last):
File "/opt/tribler/TriblerGUI/event_request_manager.py", line 99, in on_read_data
raise RuntimeError(json_dict["event"]["text"])
RuntimeError: [Failure instance: Traceback: <type 'exceptions.RuntimeError'>: Request.finish called on a request after its connection was lost; use Request.notifyFinish to keep track of this.
/opt/tribler/Tribler/Core/Libtorrent/LibtorrentMgr.py:487:_task_process_alerts
/opt/tribler/Tribler/Core/Libtorrent/LibtorrentMgr.py:336:process_alert
/usr/lib/python2.7/dist-packages/twisted/internet/defer.py:393:callback
/usr/lib/python2.7/dist-packages/twisted/internet/defer.py:501:_startRunCallbacks
--- <exception caught here> ---
/usr/lib/python2.7/dist-packages/twisted/internet/defer.py:588:_runCallbacks
/opt/tribler/Tribler/Core/Modules/restapi/downloads_endpoint.py:336:_on_remove_failure
/usr/lib/python2.7/dist-packages/twisted/web/server.py:224:finish
/usr/lib/python2.7/dist-packages/twisted/web/http.py:865:finish

# Fixed code
def render_DELETE(self, request):
    """
    .. http:delete:: /downloads/(string: infohash)

    A DELETE request to this endpoint removes a specific download from Tribler. You can specify whether you only
    want to remove the download or the download and the downloaded data using the remove_data parameter.

        **Example request**:

            .. sourcecode:: none

                curl -X DELETE http://localhost:8085/download/4344503b7e797ebf31582327a5baae35b11bda01
                --data "remove_data=1"

        **Example response**:

            .. sourcecode:: javascript

                {"removed": True}
    """
    parameters = http.parse_qs(request.content.read(), 1)

    if "remove_data" not in parameters or len(parameters["remove_data"]) == 0:
        request.setResponseCode(http.BAD_REQUEST)
        return json.dumps({"error": "remove_data parameter missing"})

    download = self.session.get_download(self.infohash)
    if not download:
        return DownloadSpecificEndpoint.return_404(request)

    remove_data = parameters["remove_data"][0] == "1"

    def _on_torrent_removed(_):
        """
        Success callback
        """
        request.write(json.dumps({"removed": True}))
        request.finish()

    def _on_remove_failure(failure):
        """
        Error callback
        :param failure: from remove_download
        """
        self._logger.exception(failure)
        request.write(return_handled_exception(request, failure.value))
        # If the above request.write failed, the request will have already been finished
        if not request.finished:
            request.finish()

    deferred = self.session.remove_download(download, remove_content=remove_data)
    deferred.addCallback(_on_torrent_removed)
    deferred.addErrback(_on_remove_failure)

    return NOT_DONE_YET