# Buggy code
def _get_new_peers(self):
    new_conns_needed = self.max_connections_per_stream - len(self._peer_connections)
    if new_conns_needed < 1:
        defer.returnValue([])
    # we always get the peer from the first request creator
    # must be a type BlobRequester...
    request_creator = self._primary_request_creators[0]
    log.debug("%s Trying to get a new peer to connect to", self._get_log_name())

    # find peers for the head blob if configured to do so
    if self.seek_head_blob_first:
        peers = yield request_creator.get_new_peers_for_head_blob()
        peers = self.return_shuffled_peers_not_connected_to(peers, new_conns_needed)
    else:
        peers = []

    # we didn't find any new peers on the head blob,
    # we have to look for the first unavailable blob
    if not peers:
        peers = yield request_creator.get_new_peers_for_next_unavailable()
        peers = self.return_shuffled_peers_not_connected_to(peers, new_conns_needed)

    log.debug("%s Got a list of peers to choose from: %s", self._get_log_name(), peers)
    log.debug(
        "%s Current connections: %s",
        self._get_log_name(),
        self._peer_connections.keys(),
    )
    log.debug(
        "%s List of connection states: %s",
        self._get_log_name(),
        [p_c_h.connection.state for p_c_h in self._peer_connections.values()],
    )

    defer.returnValue(peers)


# Traceback (KeyError)
2017-09-19 09:47:51,891 INFO     lbrynet.file_manager.EncryptedFileDownloader:32: lbry://battlefront-ii-storytrailer (5c3d3c) is finished
2017-09-19 09:47:51,904 INFO     lbrynet.file_manager.EncryptedFileDownloader:32: lbry://hitmans-bodyguard-trailer (3303b0) is running
2017-09-19 09:47:51,914 INFO     lbrynet.file_manager.EncryptedFileDownloader:32: lbry://thelovewitch-trailer (59edac) is finished
2017-09-19 09:47:51,964 INFO     lbrynet.file_manager.EncryptedFileDownloader:32: lbry://bellflower-trailer (618fbb) is finished
2017-09-19 09:47:51,986 INFO     lbrynet.file_manager.EncryptedFileDownloader:32: lbry://itsadisaster-threesome (395066) is finished
2017-09-19 09:47:51,987 INFO     lbrynet.file_manager.EncryptedFileDownloader:32: lbry://jinglebellrocks-trailer (224aa1) is finished
2017-09-19 09:47:52,009 INFO     lbrynet.file_manager.EncryptedFileDownloader:32: lbry://pub (57fdb5) is finished
2017-09-19 09:47:52,025 INFO     lbrynet.file_manager.EncryptedFileDownloader:32: lbry://lbryscrreen1 (2dab0b) is finished
2017-09-19 09:47:52,072 INFO     lbrynet.file_manager.EncryptedFileDownloader:32: lbry://lbryscrreen1 (2dab0b) is finished
2017-09-19 09:47:52,082 INFO     lbrynet.core.client.ConnectionManager:42: Connection Manager The Hitman’s Bodyguard - Redband Trailer (2017).mp4 initialized
2017-09-19 09:47:52,084 INFO     lbrynet.core.client.ConnectionManager:42: Connection Manager 1 - 'Fear the Boom and Bust' - Keynes vs. Hayek Rap Battle.mp4 initialized
2017-09-19 09:47:52,099 INFO     lbrynet.file_manager.EncryptedFileDownloader:32: lbry://kedi-trailer (5731e9) is stopped
2017-09-19 09:47:52,114 INFO     lbrynet.file_manager.EncryptedFileDownloader:32: lbry://kedi-trailer (5731e9) is stopped
2017-09-19 09:47:52,142 CRITICAL twisted:154: Unhandled error in Deferred:
2017-09-19 09:47:52,144 CRITICAL twisted:154:
Traceback (most recent call last):
File "site-packages\twisted\internet\defer.py", line 1299, in _inlineCallbacks
File "site-packages\lbrynet\core\client\BlobRequester.py", line 70, in get_new_peers_for_head_blob
File "site-packages\lbrynet\core\client\DownloadManager.py", line 103, in get_head_blob_hash
KeyError: 0
2017-09-19 09:47:52,150 CRITICAL twisted:154: Unhandled error in Deferred:
2017-09-19 09:47:52,150 CRITICAL twisted:154:
Traceback (most recent call last):
File "site-packages\twisted\internet\defer.py", line 1299, in _inlineCallbacks
File "site-packages\lbrynet\core\client\BlobRequester.py", line 70, in get_new_peers_for_head_blob
File "site-packages\lbrynet\core\client\DownloadManager.py", line 103, in get_head_blob_hash
KeyError: 0

# Fixed code
def _get_new_peers(self):
    new_conns_needed = self.max_connections_per_stream - len(self._peer_connections)
    if new_conns_needed < 1:
        defer.returnValue([])
    # we always get the peer from the first request creator
    # must be a type BlobRequester...
    request_creator = self._primary_request_creators[0]
    log.debug("%s Trying to get a new peer to connect to", self._get_log_name())

    # find peers for the head blob if configured to do so
    if self.seek_head_blob_first:
        try:
            peers = yield request_creator.get_new_peers_for_head_blob()
            peers = self.return_shuffled_peers_not_connected_to(peers, new_conns_needed)
        except KeyError:
            log.warning("%s does not have a head blob", self._get_log_name())
            peers = []
    else:
        peers = []

    # we didn't find any new peers on the head blob,
    # we have to look for the first unavailable blob
    if not peers:
        peers = yield request_creator.get_new_peers_for_next_unavailable()
        peers = self.return_shuffled_peers_not_connected_to(peers, new_conns_needed)

    log.debug("%s Got a list of peers to choose from: %s", self._get_log_name(), peers)
    log.debug(
        "%s Current connections: %s",
        self._get_log_name(),
        self._peer_connections.keys(),
    )
    log.debug(
        "%s List of connection states: %s",
        self._get_log_name(),
        [p_c_h.connection.state for p_c_h in self._peer_connections.values()],
    )
    defer.returnValue(peers)