# Buggy code
def _select_torrent(self):
    """
    Function to select which torrent in the torrent list will be downloaded in the
    next iteration. It depends on the source and applied policy
    """
    torrents = {}
    for infohash, torrent in self.torrents.iteritems():
        # we prioritize archive source
        if torrent.get("preload", False):
            if "download" not in torrent:
                self.start_download(torrent)
            elif torrent["download"].get_status() == DLSTATUS_SEEDING:
                self.stop_download(torrent)
        elif not torrent.get("is_duplicate", False):
            if torrent.get("enabled", True):
                torrents[infohash] = torrent

    if self.settings.policy is not None and torrents:
        # Determine which torrent to start and which to stop.
        torrents_start, torrents_stop = self.settings.policy.apply(
            torrents, self.settings.max_torrents_active
        )
        for torrent in torrents_stop:
            self.stop_download(torrent)
        for torrent in torrents_start:
            self.start_download(torrent)

        self._logger.info(
            "Selecting from %s torrents %s start download",
            len(torrents),
            len(torrents_start),
        )


# Traceback (exceptions.RuntimeError)
test archive mode. Use diretory because easier to fetch torrent ... Unhandled error in Deferred:


Traceback (most recent call last):
File "/usr/local/lib/python2.7/site-packages/twisted/internet/base.py", line 1203, in mainLoop
self.runUntilCurrent()
File "/usr/local/lib/python2.7/site-packages/twisted/internet/base.py", line 825, in runUntilCurrent
call.func(*call.args, **call.kw)
File "/usr/local/lib/python2.7/site-packages/twisted/internet/task.py", line 168, in start
self()
File "/usr/local/lib/python2.7/site-packages/twisted/internet/task.py", line 213, in __call__
d = defer.maybeDeferred(self.f, *self.a, **self.kw)
--- <exception caught here> ---
File "/usr/local/lib/python2.7/site-packages/twisted/internet/defer.py", line 150, in maybeDeferred
result = f(*args, **kw)
File "/Users/tribler/Documents/workspace/workspace/GH_Tribler_PR_tests_mac/tribler/Tribler/Policies/BoostingManager.py", line 336, in _select_torrent
for infohash, torrent in self.torrents.iteritems():
exceptions.RuntimeError: dictionary changed size during iteration

# Fixed code
def _select_torrent(self):
    """
    Function to select which torrent in the torrent list will be downloaded in the
    next iteration. It depends on the source and applied policy
    """
    torrents = {}
    for infohash in list(self.torrents):
        torrent = self.torrents.get(infohash)
        # we prioritize archive source
        if torrent.get("preload", False):
            if "download" not in torrent:
                self.start_download(torrent)
            elif torrent["download"].get_status() == DLSTATUS_SEEDING:
                self.stop_download(torrent)
        elif not torrent.get("is_duplicate", False):
            if torrent.get("enabled", True):
                torrents[infohash] = torrent

    if self.settings.policy is not None and torrents:
        # Determine which torrent to start and which to stop.
        torrents_start, torrents_stop = self.settings.policy.apply(
            torrents, self.settings.max_torrents_active
        )
        for torrent in torrents_stop:
            self.stop_download(torrent)
        for torrent in torrents_start:
            self.start_download(torrent)

        self._logger.info(
            "Selecting from %s torrents %s start download",
            len(torrents),
            len(torrents_start),
        )