# Buggy code
def on_got_metainfo(metainfo):
    if not isinstance(metainfo, dict) or "info" not in metainfo:
        self._logger.warning("Received metainfo is not a valid dictionary")
        request.setResponseCode(http.INTERNAL_SERVER_ERROR)
        request.write(json.twisted_dumps({"error": "invalid response"}))
        self.finish_request(request)
        return

    # Add the torrent to GigaChannel as a free-for-all entry, so others can search it
    self.session.lm.mds.TorrentMetadata.add_ffa_from_dict(
        tdef_to_metadata_dict(TorrentDef.load_from_dict(metainfo))
    )

    # TODO(Martijn): store the stuff in a database!!!
    # TODO(Vadim): this means cache the downloaded torrent in a binary storage, like LevelDB
    infohash = hashlib.sha1(bencode(metainfo["info"])).digest()

    # Check if the torrent is already in the downloads
    metainfo["download_exists"] = infohash in self.session.lm.downloads
    encoded_metainfo = hexlify(json.dumps(metainfo, ensure_ascii=False))

    request.write(json.twisted_dumps({"metainfo": encoded_metainfo}))
    self.finish_request(request)


# Traceback (RuntimeError)
Traceback (most recent call last):
File "TriblerGUI\core_manager.py", line 118, in on_received_state
RuntimeError: [Failure instance: Traceback: <type 'exceptions.ValueError'>: The filename, directory name, or volume label syntax is incorrect
run_tribler.py:85:start_tribler
Tribler\Core\Session.py:432:start
lib\site-packages\twisted\internet\defer.py:321:addCallback
lib\site-packages\twisted\internet\defer.py:310:addCallbacks
--- <exception caught here> ---
lib\site-packages\twisted\internet\defer.py:653:_runCallbacks
Tribler\Core\Session.py:429:load_checkpoint
Tribler\Core\Session.py:384:load_checkpoint
Tribler\Core\APIImplementation\LaunchManyCore.py:570:load_checkpoint
Tribler\Core\APIImplementation\LaunchManyCore.py:567:do_load_checkpoint
Tribler\Core\APIImplementation\LaunchManyCore.py:599:resume_download
Tribler\Core\TorrentDef.py:115:load_from_dict
Tribler\Core\TorrentDef.py:68:__init__
]

# Fixed code
def on_got_metainfo(metainfo):
    if not isinstance(metainfo, dict) or "info" not in metainfo:
        self._logger.warning("Received metainfo is not a valid dictionary")
        request.setResponseCode(http.INTERNAL_SERVER_ERROR)
        request.write(json.twisted_dumps({"error": "invalid response"}))
        self.finish_request(request)
        return

    # Add the torrent to GigaChannel as a free-for-all entry, so others can search it
    self.session.lm.mds.TorrentMetadata.add_ffa_from_dict(
        tdef_to_metadata_dict(TorrentDef.load_from_dict(metainfo))
    )

    # TODO(Martijn): store the stuff in a database!!!
    # TODO(Vadim): this means cache the downloaded torrent in a binary storage, like LevelDB
    infohash = hashlib.sha1(bencode(metainfo["info"])).digest()

    # Check if the torrent is already in the downloads
    metainfo["download_exists"] = infohash in self.session.lm.downloads
    # FIXME: json.dumps garbles binary data that is used by the 'pieces' field
    # However, this is fine as long as the GUI does not use this field.
    encoded_metainfo = hexlify(json.dumps(metainfo, ensure_ascii=False))

    request.write(json.twisted_dumps({"metainfo": encoded_metainfo}))
    self.finish_request(request)