# 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 + "/" + codecs.decode(mid, "raw_unicode_escape")
        )
    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()
            )

    ltsession.set_settings(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,
        )

        ltsession_settings = ltsession.get_settings()
        ltsession_settings["upload_rate_limit"] = (
            self.tribler_session.config.get_libtorrent_max_upload_rate()
        )
        ltsession_settings["download_rate_limit"] = (
            self.tribler_session.config.get_libtorrent_max_download_rate()
        )
        ltsession.set_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 (UnicodeDecodeError)
======================================================================
ERROR: test_monitor_download_insert_torrent
----------------------------------------------------------------------
Traceback (most recent call last):
File "c:\python27\lib\site-packages\twisted\internet\defer.py", line 1384, in _inlineCallbacks
result = result.throwExceptionIntoGenerator(g)
File "c:\python27\lib\site-packages\twisted\python\failure.py", line 393, in throwExceptionIntoGenerator
return g.throw(self.type, self.value, self.tb)
File "C:\workspace\jenkins2\workspace\GH_Tribler_PR_Tests\PR_win64\tribler\Tribler\Test\Core\CreditMining\test_credit_mining_manager.py", line 108, in setUp
yield super(TestCreditMiningManager, self).setUp()
File "c:\python27\lib\site-packages\twisted\internet\defer.py", line 1386, in _inlineCallbacks
result = g.send(result)
File "C:\workspace\jenkins2\workspace\GH_Tribler_PR_Tests\PR_win64\tribler\Tribler\Test\test_as_server.py", line 278, in setUp
self.tribler_started_deferred = self.session.start()
File "C:\workspace\jenkins2\workspace\GH_Tribler_PR_Tests\PR_win64\tribler\Tribler\Core\Session.py", line 409, in start
startup_deferred = self.lm.register(self, self.session_lock)
File "C:\workspace\jenkins2\workspace\GH_Tribler_PR_Tests\PR_win64\tribler\Tribler\Core\APIImplementation\LaunchManyCore.py", line 153, in register
self.init()
File "C:\workspace\jenkins2\workspace\GH_Tribler_PR_Tests\PR_win64\tribler\Tribler\Core\APIImplementation\LaunchManyCore.py", line 309, in init
self.ltmgr.initialize()
File "C:\workspace\jenkins2\workspace\GH_Tribler_PR_Tests\PR_win64\tribler\Tribler\Core\Libtorrent\LibtorrentMgr.py", line 98, in initialize
self.get_session().start_upnp()
File "C:\workspace\jenkins2\workspace\GH_Tribler_PR_Tests\PR_win64\tribler\Tribler\Core\Libtorrent\LibtorrentMgr.py", line 249, in get_session
self.ltsessions[hops] = self.create_session(hops)
File "C:\workspace\jenkins2\workspace\GH_Tribler_PR_Tests\PR_win64\tribler\Tribler\Core\Libtorrent\LibtorrentMgr.py", line 185, in create_session
codecs.decode(mid, 'raw_unicode_escape'))
UnicodeDecodeError: 'rawunicodeescape' codec can't decode bytes in position 10-11: truncated \uXXXX

# 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 + "/" + mid.encode("hex")
        )
    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()
            )

    ltsession.set_settings(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,
        )

        ltsession_settings = ltsession.get_settings()
        ltsession_settings["upload_rate_limit"] = (
            self.tribler_session.config.get_libtorrent_max_upload_rate()
        )
        ltsession_settings["download_rate_limit"] = (
            self.tribler_session.config.get_libtorrent_max_download_rate()
        )
        ltsession.set_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