# Buggy code
def create_session(self, hops=0, store_listen_port=True):
    settings = {}

    # Due to a bug in Libtorrent 0.16.18, the outgoing_port and num_outgoing_ports value should be set in
    # the settings dictionary
    settings["outgoing_port"] = 0
    settings["num_outgoing_ports"] = 1
    settings["allow_multiple_connections_per_ip"] = 0

    # Copy construct so we don't modify the default list
    extensions = list(DEFAULT_LT_EXTENSIONS)

    # Elric: Strip out the -rcX, -beta, -whatever tail on the version string.
    fingerprint = ["TL"] + [int(x) for x in version_id.split("-")[0].split(".")] + [0]
    ltsession = (
        lt.session(lt.fingerprint(*fingerprint), flags=0)
        if hops == 0
        else lt.session(flags=0)
    )

    if hops == 0:
        settings["user_agent"] = "Tribler/" + version_id
        enable_utp = self.tribler_session.config.get_libtorrent_utp()
        settings["enable_outgoing_utp"] = enable_utp
        settings["enable_incoming_utp"] = enable_utp

        if LooseVersion(self.get_libtorrent_version()) >= LooseVersion("1.1.0"):
            settings["prefer_rc4"] = True
            settings["listen_interfaces"] = (
                "0.0.0.0:%d" % self.tribler_session.config.get_libtorrent_port()
            )
        else:
            pe_settings = lt.pe_settings()
            pe_settings.prefer_rc4 = True
            ltsession.set_pe_settings(pe_settings)

        mid = self.tribler_session.trustchain_keypair.key_to_hash()
        settings["peer_fingerprint"] = mid
        settings["handshake_client_version"] = (
            "Tribler/" + version_id + "/" + hexlify(mid)
        )
    else:
        settings["enable_outgoing_utp"] = True
        settings["enable_incoming_utp"] = True
        settings["enable_outgoing_tcp"] = False
        settings["enable_incoming_tcp"] = False
        settings["anonymous_mode"] = True
        settings["force_proxy"] = True

        if LooseVersion(self.get_libtorrent_version()) >= LooseVersion("1.1.0"):
            settings["listen_interfaces"] = (
                "0.0.0.0:%d" % self.tribler_session.config.get_anon_listen_port()
            )

    self.set_session_settings(ltsession, settings)
    ltsession.set_alert_mask(self.default_alert_mask)

    # Load proxy settings
    if hops == 0:
        proxy_settings = self.tribler_session.config.get_libtorrent_proxy_settings()
    else:
        proxy_settings = list(self.tribler_session.config.get_anon_proxy_settings())
        proxy_host, proxy_ports = proxy_settings[1]
        proxy_settings[1] = (proxy_host, proxy_ports[hops - 1])
    self.set_proxy_settings(ltsession, *proxy_settings)

    for extension in extensions:
        ltsession.add_extension(extension)

    # Set listen port & start the DHT
    if hops == 0:
        listen_port = self.tribler_session.config.get_libtorrent_port()
        ltsession.listen_on(listen_port, listen_port + 10)
        if listen_port != ltsession.listen_port() and store_listen_port:
            self.tribler_session.config.set_libtorrent_port_runtime(
                ltsession.listen_port()
            )
        try:
            lt_state = lt.bdecode(
                open(
                    os.path.join(
                        self.tribler_session.config.get_state_dir(), LTSTATE_FILENAME
                    )
                ).read()
            )
            if lt_state is not None:
                ltsession.load_state(lt_state)
            else:
                self._logger.warning(
                    "the lt.state appears to be corrupt, writing new data on shutdown"
                )
        except Exception as exc:
            self._logger.info(
                "could not load libtorrent state, got exception: %r. starting from scratch"
                % exc
            )
    else:
        ltsession.listen_on(
            self.tribler_session.config.get_anon_listen_port(),
            self.tribler_session.config.get_anon_listen_port() + 20,
        )

        settings = {
            "upload_rate_limit": self.tribler_session.config.get_libtorrent_max_upload_rate(),
            "download_rate_limit": self.tribler_session.config.get_libtorrent_max_download_rate(),
        }
        self.set_session_settings(ltsession, settings)

    if self.tribler_session.config.get_libtorrent_dht_enabled():
        ltsession.start_dht()
        for router in DEFAULT_DHT_ROUTERS:
            ltsession.add_dht_router(*router)
        ltsession.start_lsd()

    self._logger.debug(
        "Started libtorrent session for %d hops on port %d",
        hops,
        ltsession.listen_port(),
    )
    self.lt_session_shutdown_ready[hops] = False

    return ltsession


# Traceback (RuntimeError)
Traceback (most recent call last):
File "TriblerGUI/event_request_manager.py", line 114, in on_read_data
RuntimeError: [Failure instance: Traceback: <class 'RuntimeError'>: expected value (list, dict, int or string) in bencoded string
site-packages/twisted/internet/base.py:1283:run
site-packages/twisted/internet/base.py:1292:mainLoop
site-packages/twisted/internet/base.py:913:runUntilCurrent
site-packages/twisted/internet/task.py:239:__call__
--- <exception caught here> ---
site-packages/twisted/internet/defer.py:151:maybeDeferred
Tribler/Core/Libtorrent/LibtorrentMgr.py:578:_task_process_alerts
]

# Fixed code
def create_session(self, hops=0, store_listen_port=True):
    settings = {}

    # Due to a bug in Libtorrent 0.16.18, the outgoing_port and num_outgoing_ports value should be set in
    # the settings dictionary
    settings["outgoing_port"] = 0
    settings["num_outgoing_ports"] = 1
    settings["allow_multiple_connections_per_ip"] = 0

    # Copy construct so we don't modify the default list
    extensions = list(DEFAULT_LT_EXTENSIONS)

    # Elric: Strip out the -rcX, -beta, -whatever tail on the version string.
    fingerprint = ["TL"] + [int(x) for x in version_id.split("-")[0].split(".")] + [0]
    ltsession = (
        lt.session(lt.fingerprint(*fingerprint), flags=0)
        if hops == 0
        else lt.session(flags=0)
    )

    if hops == 0:
        settings["user_agent"] = "Tribler/" + version_id
        enable_utp = self.tribler_session.config.get_libtorrent_utp()
        settings["enable_outgoing_utp"] = enable_utp
        settings["enable_incoming_utp"] = enable_utp

        if LooseVersion(self.get_libtorrent_version()) >= LooseVersion("1.1.0"):
            settings["prefer_rc4"] = True
            settings["listen_interfaces"] = (
                "0.0.0.0:%d" % self.tribler_session.config.get_libtorrent_port()
            )
        else:
            pe_settings = lt.pe_settings()
            pe_settings.prefer_rc4 = True
            ltsession.set_pe_settings(pe_settings)

        mid = self.tribler_session.trustchain_keypair.key_to_hash()
        settings["peer_fingerprint"] = mid
        settings["handshake_client_version"] = (
            "Tribler/" + version_id + "/" + hexlify(mid)
        )
    else:
        settings["enable_outgoing_utp"] = True
        settings["enable_incoming_utp"] = True
        settings["enable_outgoing_tcp"] = False
        settings["enable_incoming_tcp"] = False
        settings["anonymous_mode"] = True
        settings["force_proxy"] = True

        if LooseVersion(self.get_libtorrent_version()) >= LooseVersion("1.1.0"):
            settings["listen_interfaces"] = (
                "0.0.0.0:%d" % self.tribler_session.config.get_anon_listen_port()
            )

    self.set_session_settings(ltsession, settings)
    ltsession.set_alert_mask(self.default_alert_mask)

    # Load proxy settings
    if hops == 0:
        proxy_settings = self.tribler_session.config.get_libtorrent_proxy_settings()
    else:
        proxy_settings = list(self.tribler_session.config.get_anon_proxy_settings())
        proxy_host, proxy_ports = proxy_settings[1]
        proxy_settings[1] = (proxy_host, proxy_ports[hops - 1])
    self.set_proxy_settings(ltsession, *proxy_settings)

    for extension in extensions:
        ltsession.add_extension(extension)

    # Set listen port & start the DHT
    if hops == 0:
        listen_port = self.tribler_session.config.get_libtorrent_port()
        ltsession.listen_on(listen_port, listen_port + 10)
        if listen_port != ltsession.listen_port() and store_listen_port:
            self.tribler_session.config.set_libtorrent_port_runtime(
                ltsession.listen_port()
            )
        try:
            lt_state = bdecode_compat(
                open(
                    os.path.join(
                        self.tribler_session.config.get_state_dir(), LTSTATE_FILENAME
                    )
                ).read()
            )
            if lt_state is not None:
                ltsession.load_state(lt_state)
            else:
                self._logger.warning(
                    "the lt.state appears to be corrupt, writing new data on shutdown"
                )
        except Exception as exc:
            self._logger.info(
                "could not load libtorrent state, got exception: %r. starting from scratch"
                % exc
            )
    else:
        ltsession.listen_on(
            self.tribler_session.config.get_anon_listen_port(),
            self.tribler_session.config.get_anon_listen_port() + 20,
        )

        settings = {
            "upload_rate_limit": self.tribler_session.config.get_libtorrent_max_upload_rate(),
            "download_rate_limit": self.tribler_session.config.get_libtorrent_max_download_rate(),
        }
        self.set_session_settings(ltsession, settings)

    if self.tribler_session.config.get_libtorrent_dht_enabled():
        ltsession.start_dht()
        for router in DEFAULT_DHT_ROUTERS:
            ltsession.add_dht_router(*router)
        ltsession.start_lsd()

    self._logger.debug(
        "Started libtorrent session for %d hops on port %d",
        hops,
        ltsession.listen_port(),
    )
    self.lt_session_shutdown_ready[hops] = False

    return ltsession