# Buggy code
def _handle_response_error(self, err):
    # If an error gets to this point, log it and kill the connection.
    if err.check(DownloadCanceledError, RequestCanceledError, error.ConnectionAborted):
        # TODO: (wish-list) it seems silly to close the connection over this, and it shouldn't
        # TODO: always be this way. it's done this way now because the client has no other way
        # TODO: of telling the server it wants the download to stop. It would be great if the
        # TODO: protocol had such a mechanism.
        log.info(
            "Closing the connection to %s because the download of blob %s was canceled",
            self.peer,
            self._blob_download_request.blob,
        )
        result = None
    elif not err.check(MisbehavingPeerError, ConnectionClosedBeforeResponseError):
        log.warning("The connection to %s is closing due to: %s", self.peer, err)
        result = err
    else:
        log.error(
            "The connection to %s is closing due to an unexpected error: %s",
            self.peer,
            err,
        )
        result = err

    self.transport.loseConnection()
    return result


# Traceback (exceptions.ValueError)
2017-10-12 13:52:54,172 INFO     lbrynet.core.client.ClientProtocol:185: Closing the connection to x.x.x.x:3333 because the download of blob a5d1ebd863d2b5f7 was canceled
2017-10-12 13:52:54,174 ERROR    lbrynet.core.client.ClientProtocol:130: An unexpected error occurred creating or sending a request to x.x.x.x:3333. Error message: Traceback (most recent call last):
File "site-packages/twisted/internet/defer.py", line 651, in _runCallbacks
File "site-packages/lbrynet/core/client/ClientProtocol.py", line 235, in get_next_request
File "site-packages/lbrynet/core/client/ClientProtocol.py", line 147, in _ask_for_request
File "site-packages/twisted/internet/defer.py", line 1445, in unwindGenerator
--- <exception caught here> ---
File "site-packages/twisted/internet/defer.py", line 1299, in _inlineCallbacks
File "site-packages/lbrynet/core/client/ConnectionManager.py", line 111, in get_next_request
File "site-packages/lbrynet/core/client/ConnectionManager.py", line 137, in _send_primary_requests
File "site-packages/lbrynet/core/client/BlobRequester.py", line 66, in send_next_request
File "site-packages/lbrynet/core/client/BlobRequester.py", line 113, in _send_next_request
File "site-packages/lbrynet/core/client/BlobRequester.py", line 440, in make_request_and_handle_response
File "site-packages/lbrynet/core/client/BlobRequester.py", line 493, in _handle_download_request
File "site-packages/lbrynet/core/client/BlobRequester.py", line 540, in create_add_blob_request
File "site-packages/lbrynet/core/client/ClientProtocol.py", line 108, in add_blob_request
exceptions.ValueError: There is already a blob download request active

# Fixed code
def _handle_response_error(self, err):
    # If an error gets to this point, log it and kill the connection.
    if err.check(
        DownloadCanceledError,
        RequestCanceledError,
        error.ConnectionAborted,
        ConnectionClosedBeforeResponseError,
    ):
        # TODO: (wish-list) it seems silly to close the connection over this, and it shouldn't
        # TODO: always be this way. it's done this way now because the client has no other way
        # TODO: of telling the server it wants the download to stop. It would be great if the
        # TODO: protocol had such a mechanism.
        log.info(
            "Closing the connection to %s because the download of blob %s was canceled",
            self.peer,
            self._blob_download_request.blob,
        )
        result = None
    elif err.check(MisbehavingPeerError):
        log.warning("The connection to %s is closing due to: %s", self.peer, err)
        result = err
    else:
        log.error(
            "The connection to %s is closing due to an unexpected error: %s",
            self.peer,
            err,
        )
        result = err
    self._blob_download_request = None
    self._downloading_blob = False
    self.transport.loseConnection()
    return result