# Buggy code
def start(self):
    self.upnp_component = self.component_manager.get_component(UPNP_COMPONENT)
    self.external_peer_port = self.upnp_component.upnp_redirects.get(
        "TCP", GCS("peer_port")
    )
    self.external_udp_port = self.upnp_component.upnp_redirects.get(
        "UDP", GCS("dht_node_port")
    )
    node_id = CS.get_node_id()
    if node_id is None:
        node_id = generate_id()

    self.dht_node = node.Node(
        node_id=node_id,
        udpPort=GCS("dht_node_port"),
        externalUDPPort=self.external_udp_port,
        externalIP=self.upnp_component.external_ip,
        peerPort=self.external_peer_port,
    )

    self.dht_node.start_listening()
    yield self.dht_node._protocol._listening
    d = self.dht_node.joinNetwork(GCS("known_dht_nodes"))
    d.addCallback(lambda _: self.dht_node.start_looping_calls())
    d.addCallback(lambda _: log.info("Joined the dht"))
    log.info("Started the dht")


# Traceback (Exception)
2018-10-28 21:39:16,952 WARNING  lbrynet.daemon.Components:712: upnp discovery failed: {http://schemas.microsoft.com/windows/pnpx/2005/11}X_hardwareId
2018-10-28 21:39:16,953 ERROR    lbrynet.daemon.Components:782: failed to setup upnp
Unhandled error in Deferred:
2018-10-28 21:40:32,480 CRITICAL twisted:154: Unhandled error in Deferred:

2018-10-28 21:40:32,481 CRITICAL twisted:154:
Traceback (most recent call last):
File "twisted\internet\defer.py", line 1418, in _inlineCallbacks
File "lbrynet\dht\node.py", line 635, in _iterativeFind
File "twisted\internet\defer.py", line 1362, in returnValue
twisted.internet.defer._DefGen_Return: [<lbrynet.dht.contact._Contact object at 0x067573D0>, <lbrynet.dht.contact._Contact object at 0x06796470>, <lbrynet.dht.contact._Contact object at 0x064A5B30>, <lbrynet.dht.contact._Contact object at 0x067798B0>, <lbrynet.dht.contact._Contact object at 0x06793DB0>, <lbrynet.dht.contact._Contact object at 0x0647D510>, <lbrynet.dht.contact._Contact object at 0x06779890>, <lbrynet.dht.contact._Contact object at 0x06488CF0>]

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "twisted\internet\defer.py", line 1416, in _inlineCallbacks
File "twisted\python\failure.py", line 491, in throwExceptionIntoGenerator
File "lbrynet\dht\hashannouncer.py", line 38, in do_store
File "twisted\internet\defer.py", line 1418, in _inlineCallbacks
File "lbrynet\dht\node.py", line 326, in announceHaveBlob
Exception: Cannot determine external IP: None
Unhandled error in Deferred:

# Fixed code
def start(self):
    self.upnp_component = self.component_manager.get_component(UPNP_COMPONENT)
    self.external_peer_port = self.upnp_component.upnp_redirects.get(
        "TCP", GCS("peer_port")
    )
    self.external_udp_port = self.upnp_component.upnp_redirects.get(
        "UDP", GCS("dht_node_port")
    )
    node_id = CS.get_node_id()
    if node_id is None:
        node_id = generate_id()
    external_ip = self.upnp_component.external_ip
    if not external_ip:
        log.warning("UPnP component failed to get external ip")
        external_ip = CS.get_external_ip()
        if not external_ip:
            log.warning("failed to get external ip")

    self.dht_node = node.Node(
        node_id=node_id,
        udpPort=GCS("dht_node_port"),
        externalUDPPort=self.external_udp_port,
        externalIP=external_ip,
        peerPort=self.external_peer_port,
    )

    self.dht_node.start_listening()
    yield self.dht_node._protocol._listening
    d = self.dht_node.joinNetwork(GCS("known_dht_nodes"))
    d.addCallback(lambda _: self.dht_node.start_looping_calls())
    d.addCallback(lambda _: log.info("Joined the dht"))
    log.info("Started the dht")