# Buggy code
def sesscb_states_callback(self, states_list):
    """
    This method is periodically (every second) called with a list of the download states of the active downloads.
    """
    self.state_cb_count += 1

    # Check to see if a download has finished
    new_active_downloads = []
    do_checkpoint = False
    seeding_download_list = []

    for ds in states_list:
        state = ds.get_status()
        download = ds.get_download()
        tdef = download.get_def()
        safename = tdef.get_name_as_unicode()

        if state == DLSTATUS_DOWNLOADING:
            new_active_downloads.append(safename)
        elif state == DLSTATUS_STOPPED_ON_ERROR:
            self._logger.error("Error during download: %s", repr(ds.get_error()))
            self.downloads.get(tdef.get_infohash()).stop()
            self.session.notifier.notify(
                NTFY_TORRENT, NTFY_ERROR, tdef.get_infohash(), repr(ds.get_error())
            )
        elif state == DLSTATUS_SEEDING:
            seeding_download_list.append(
                {"infohash": tdef.get_infohash(), "download": download}
            )

            if safename in self.previous_active_downloads:
                self.session.notifier.notify(
                    NTFY_TORRENT, NTFY_FINISHED, tdef.get_infohash(), safename
                )
                do_checkpoint = True

            elif download.get_hops() == 0 and download.get_safe_seeding():
                hops = tribler_defaults.get("Tribler", {}).get("default_number_hops", 1)
                self._logger.info(
                    "Moving completed torrent to tunneled session %d for hidden seeding %r",
                    hops,
                    download,
                )
                self.session.remove_download(download)

                # copy the old download_config and change the hop count
                dscfg = download.copy()
                dscfg.set_hops(hops)

                # TODO(emilon): That's a hack to work around the fact that removing a torrent is racy.
                self.register_task(
                    "reschedule_download",
                    reactor.callLater(
                        5, self.session.start_download_from_tdef, tdef, dscfg
                    ),
                )

    self.previous_active_downloads = new_active_downloads
    if do_checkpoint:
        self.session.checkpoint_downloads()

    if self.state_cb_count % 4 == 0 and self.tunnel_community:
        self.tunnel_community.monitor_downloads(states_list)

    return []


# Traceback (RuntimeError)
Traceback (most recent call last):
File "TriblerGUI\event_request_manager.py", line 92, in on_read_data
RuntimeError: Unhandled Error
Traceback (most recent call last):
File "lib\multiprocessing\process.py", line 114, in run

File "TriblerGUI\core_manager.py", line 63, in start_tribler_core

File "lib\site-packages\twisted\internet\base.py", line 1195, in run

File "lib\site-packages\twisted\internet\base.py", line 1204, in mainLoop

--- <exception caught here> ---
File "lib\site-packages\twisted\internet\base.py", line 798, in runUntilCurrent

File "Tribler\dispersy\endpoint.py", line 253, in dispersythread_data_came_in

File "Tribler\dispersy\dispersy.py", line 1488, in on_incoming_packets

File "Tribler\dispersy\dispersy.py", line 685, in get_community

File "Tribler\dispersy\community.py", line 226, in init_community

File "Tribler\community\tunnel\tunnel_community.py", line 316, in initialize

File "Tribler\community\tunnel\Socks5\server.py", line 309, in start

File "lib\site-packages\twisted\internet\posixbase.py", line 478, in listenTCP

File "lib\site-packages\twisted\internet\tcp.py", line 983, in startListening

twisted.internet.error.CannotListenError: Couldn't listen on any:55661: [Errno 10048] Elk socketadres (protocol/netwerkadres/poort) kan normaal slechts n keer worden gebruikt.

# Fixed code
def sesscb_states_callback(self, states_list):
    """
    This method is periodically (every second) called with a list of the download states of the active downloads.
    """
    self.state_cb_count += 1

    # Check to see if a download has finished
    new_active_downloads = []
    do_checkpoint = False
    seeding_download_list = []

    for ds in states_list:
        state = ds.get_status()
        download = ds.get_download()
        tdef = download.get_def()
        safename = tdef.get_name_as_unicode()

        if state == DLSTATUS_DOWNLOADING:
            new_active_downloads.append(safename)
        elif state == DLSTATUS_STOPPED_ON_ERROR:
            self._logger.error("Error during download: %s", repr(ds.get_error()))
            self.downloads.get(tdef.get_infohash()).stop()
            self.session.notifier.notify(
                NTFY_TORRENT, NTFY_ERROR, tdef.get_infohash(), repr(ds.get_error())
            )
        elif state == DLSTATUS_SEEDING:
            seeding_download_list.append(
                {"infohash": tdef.get_infohash(), "download": download}
            )

            if safename in self.previous_active_downloads:
                self.session.notifier.notify(
                    NTFY_TORRENT, NTFY_FINISHED, tdef.get_infohash(), safename
                )
                do_checkpoint = True

            elif download.get_hops() == 0 and download.get_safe_seeding():
                hops = tribler_defaults.get("Tribler", {}).get("default_number_hops", 1)
                self._logger.info(
                    "Moving completed torrent to tunneled session %d for hidden seeding %r",
                    hops,
                    download,
                )
                self.session.remove_download(download)

                # copy the old download_config and change the hop count
                dscfg = download.copy()
                dscfg.set_hops(hops)

                # TODO(emilon): That's a hack to work around the fact that removing a torrent is racy.
                self.register_task(
                    "reschedule_download_%s" % tdef.get_infohash(),
                    reactor.callLater(
                        5, self.session.start_download_from_tdef, tdef, dscfg
                    ),
                )

    self.previous_active_downloads = new_active_downloads
    if do_checkpoint:
        self.session.checkpoint_downloads()

    if self.state_cb_count % 4 == 0 and self.tunnel_community:
        self.tunnel_community.monitor_downloads(states_list)

    return []