# Buggy code
def can_create_engine_wrapper(self):
    """
    Periodically checks whether the engine wrapper can be created.
    Notifies when it's ready by calling the callback of the deferred being returned.
    :return: A deferred that will be called when you can create the engine wrapper.
    """
    can_create_deferred = Deferred()

    def do_check():
        with self.dllock:
            if not self.cew_scheduled:
                self.ltmgr = self.session.lm.ltmgr
                dht_ok = (
                    not isinstance(self.tdef, TorrentDefNoMetainfo)
                    or self.ltmgr.is_dht_ready()
                )
                tunnel_community = self.ltmgr.tribler_session.lm.tunnel_community
                tunnels_ready = (
                    tunnel_community.tunnels_ready(self.get_hops())
                    if tunnel_community
                    else 1
                )

                if not self.ltmgr or not dht_ok or tunnels_ready < 1:
                    self._logger.info(
                        "LTMGR/DHT/session not ready, rescheduling create_engine_wrapper"
                    )

                    if tunnels_ready < 1:
                        tunnel_community.build_tunnels(self.get_hops())

                    # Schedule this function call to be called again in 5 seconds
                    self.register_task(
                        "check_create_wrapper", reactor.callLater(5, do_check)
                    )
                else:
                    can_create_deferred.callback(True)
            else:
                # Schedule this function call to be called again in 5 seconds
                self.register_task(
                    "check_create_wrapper", reactor.callLater(5, do_check)
                )

    do_check()
    return can_create_deferred


# Traceback (RuntimeError)
Traceback (most recent call last):
File "/usr/share/tribler/TriblerGUI/event_request_manager.py", line 130, in on_read_data
raise RuntimeError(json_dict["event"]["text"])
RuntimeError: Unhandled Error
Traceback (most recent call last):
File "run_tribler.py", line 84, in <module>
start_tribler_core(base_path, api_port)
File "run_tribler.py", line 76, in start_tribler_core
reactor.run()
File "/usr/lib/python2.7/dist-packages/twisted/internet/base.py", line 1243, in run
self.mainLoop()
File "/usr/lib/python2.7/dist-packages/twisted/internet/base.py", line 1252, in mainLoop
self.runUntilCurrent()
--- <exception caught here> ---
File "/usr/lib/python2.7/dist-packages/twisted/internet/base.py", line 878, in runUntilCurrent
call.func(*call.args, **call.kw)
File "/usr/share/tribler/Tribler/Core/Libtorrent/LibtorrentDownloadImpl.py", line 267, in do_check
tunnel_community.build_tunnels(self.get_hops())
File "/usr/share/tribler/Tribler/pyipv8/ipv8/messaging/anonymization/community.py", line 220, in build_tunnels
self.do_circuits()
File "/usr/share/tribler/Tribler/pyipv8/ipv8/messaging/anonymization/community.py", line 197, in do_circuits
if not self.create_circuit(circuit_length):
File "/usr/share/tribler/Tribler/pyipv8/ipv8/messaging/anonymization/community.py", line 317, in create_circuit
circuit.unverified_hop.dh_first_part)))
File "/usr/share/tribler/Tribler/pyipv8/ipv8/messaging/anonymization/community.py", line 474, in send_cell
return self.send_message(candidates, message_type, packet, circuit_id if circuit_id else payload.circuit_id)
File "/usr/share/tribler/Tribler/pyipv8/ipv8/messaging/anonymization/community.py", line 493, in send_message
return self.send_packet(candidates, message_type, packet)
File "/usr/share/tribler/Tribler/pyipv8/ipv8/messaging/anonymization/community.py", line 498, in send_packet
self.endpoint.send(address, packet)
File "/usr/share/tribler/Tribler/pyipv8/ipv8/messaging/interfaces/udp/endpoint.py", line 39, in send
self.assert_open()
File "/usr/share/tribler/Tribler/pyipv8/ipv8/messaging/interfaces/udp/endpoint.py", line 71, in assert_open
raise EndpointClosedException(self)
Tribler.pyipv8.ipv8.messaging.interfaces.endpoint.EndpointClosedException: <class 'Tribler.pyipv8.ipv8.messaging.interfaces.udp.endpoint.UDPEndpoint'> is unexpectedly closed

# Fixed code
def can_create_engine_wrapper(self):
    """
    Periodically checks whether the engine wrapper can be created.
    Notifies when it's ready by calling the callback of the deferred being returned.
    :return: A deferred that will be called when you can create the engine wrapper.
    """
    can_create_deferred = Deferred()

    def do_check():
        with self.dllock:
            if not self.cew_scheduled:
                self.ltmgr = self.session.lm.ltmgr
                dht_ok = (
                    not isinstance(self.tdef, TorrentDefNoMetainfo)
                    or self.ltmgr.is_dht_ready()
                )
                tunnel_community = self.ltmgr.tribler_session.lm.tunnel_community
                tunnels_ready = (
                    tunnel_community.tunnels_ready(self.get_hops())
                    if tunnel_community
                    else 1
                )

                if not self.ltmgr or not dht_ok or tunnels_ready < 1:
                    self._logger.info(
                        "LTMGR/DHT/session not ready, rescheduling create_engine_wrapper"
                    )

                    if tunnel_community and tunnels_ready < 1:
                        tunnel_community.build_tunnels(self.get_hops())

                    # Schedule this function call to be called again in 5 seconds
                    self.register_task(
                        "check_create_wrapper", reactor.callLater(5, do_check)
                    )
                else:
                    can_create_deferred.callback(True)
            else:
                # Schedule this function call to be called again in 5 seconds
                self.register_task(
                    "check_create_wrapper", reactor.callLater(5, do_check)
                )

    do_check()
    return can_create_deferred