# Buggy code
def unhandled_error_observer(self, event):
    """
    This method is called when an unhandled error in Tribler is observed.
    It broadcasts the tribler_exception event.
    """
    if event["isError"]:
        text = ""
        if "log_legacy" in event and "log_text" in event:
            text = event["log_text"]
        elif "log_failure" in event:
            text = str(event["log_failure"])

        # There are some errors that we are ignoring.
        # No route to host: this issue is non-critical since Tribler can still function when a request fails.
        if "socket.error" in text and "[Errno 113]" in text:
            self._logger.error(
                "Observed no route to host error (but ignoring)."
                "This might indicate a problem with your firewall."
            )
            return

        # Socket block: this sometimes occurres on Windows and is non-critical.
        if "socket.error" in text and "[Errno %s]" % SOCKET_BLOCK_ERRORCODE in text:
            self._logger.error(
                "Unable to send data due to socket.error %s", SOCKET_BLOCK_ERRORCODE
            )
            return

        if "socket.error" in text and "[Errno 51]" in text:
            self._logger.error("Could not send data: network is unreachable.")
            return

        if "socket.error" in text and "[Errno 16]" in text:
            self._logger.error("Could not send data: socket is busy.")
            return

        if "socket.error" in text and "[Errno 11001]" in text:
            self._logger.error("Unable to perform DNS lookup.")
            return

        if "socket.error" in text and "[Errno 10053]" in text:
            self._logger.error(
                "An established connection was aborted by the software in your host machine."
            )
            return

        if "socket.error" in text and "[Errno 10054]" in text:
            self._logger.error("Connection forcibly closed by the remote host.")
            return

        if "socket.gaierror" in text and "[Errno 10022]" in text:
            self._logger.error("Failed to get address info. Error code: 10022")
            return

        if "exceptions.ValueError: Invalid DNS-ID" in text:
            self._logger.error("Invalid DNS-ID")
            return

        if "twisted.web._newclient.ResponseNeverReceived" in text:
            self._logger.error(
                "Internal Twisted response error, consider updating your Twisted version."
            )
            return

        if "twisted.internet.error.AlreadyCalled" in text:
            self._logger.error("Tried to cancel an already called event\n%s", text)
            return

        # We already have a check for invalid infohash when adding a torrent, but if somehow we get this
        # error then we simply log and ignore it.
        if "exceptions.RuntimeError: invalid info-hash" in text:
            self._logger.error("Invalid info-hash found")
            return

        if self.lm.api_manager and len(text) > 0:
            self.lm.api_manager.root_endpoint.events_endpoint.on_tribler_exception(text)
            self.lm.api_manager.root_endpoint.state_endpoint.on_tribler_exception(text)


# Traceback (builtins.OSError)
Unhandled error in Deferred:

Traceback (most recent call last):
File "/nix/store/s7vw8y02cqzx8bnjxl4bkI am still getting this error on NixOS 19.09 andhlnwvz6ws1s-python3-3.7.6/lib/python3.7/threading.py", line 926, in _bootstrap_inner
self.run()
File "/nix/store/s7vw8y02cqzx8bnjxl4bkhlnwvzbuiltins.OSError6ws1s-python3-3.7.6/lib/python3.7/threading.py", line 870, in runbuiltins.OSError
self._target(*self._args, **self._kwargs)
File "/nix/store/s1bri1qmkm4nggf4w991si52f02555qn-python3.7-Twisted-19.10.0/lib/python3.7/site-packages/twisted/_threads/_threadworker.py", line 46, in work
task()
File "/nix/store/s1bri1qmkm4nggf4w991si52f02555qn-python3.7-Twisted-19.10.0/lib/python3.7/site-packages/twisted/_threads/_team.py", line 190, in doWork
task()
--- <exception caught here> ---
File "/nix/store/s1bri1qmkm4nggf4w991si52f02555qn-python3.7-Twisted-19.10.0/lib/python3.7/site-packages/twisted/python/threadpool.py", line 250, in inContext
result = inContext.theWork()
File "/nix/store/s1bri1qmkm4nggf4w991si52f02555qn-python3.7-Twisted-19.10.0/lib/python3.7/site-packages/twisted/python/threadpool.py", line 266, in <lambda>
inContext.theWork = lambda: context.call(ctx, func, *args, **kw)
File "/nix/store/s1bri1qmkm4nggf4w991si52f02555qn-python3.7-Twisted-19.10.and0/lib/python3.7/site-packages/twisted/python/context.py", line 122, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "/nix/store/s1bri1qmkm4nggf4w991si52f02555qn-python3.7-Twisted-19.10.0/lib/python3.7/site-packages/twisted/python/context.py", line 85, in callWithContext
return func(*args,**kw)
File "/nix/store/s1bri1qmkm4nggf4w991si52f02555qn-python3.7-Twisted-19.10.0/lib/python3.7/site-packages/twisted/internet/_resolver.py", line 124, in get
socketType)
File "/nix/store/s7vw8y02cqzx8bnjxl4bkhlnwvz6ws1s-python3-3.7.6/lib/python3.7/socket.py", line 752, in getaddrinfo
for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
builtins.OSError: [Errno 16] Device or resource busy

# Fixed code
def unhandled_error_observer(self, event):
    """
    This method is called when an unhandled error in Tribler is observed.
    It broadcasts the tribler_exception event.
    """
    if event["isError"]:
        text = ""
        if "log_legacy" in event and "log_text" in event:
            text = event["log_text"]
        elif "log_failure" in event:
            text = str(event["log_failure"])

        # There are some errors that we are ignoring.
        # No route to host: this issue is non-critical since Tribler can still function when a request fails.
        if "socket.error" in text and "[Errno 113]" in text:
            self._logger.error(
                "Observed no route to host error (but ignoring)."
                "This might indicate a problem with your firewall."
            )
            return

        # Socket block: this sometimes occurres on Windows and is non-critical.
        if "socket.error" in text and "[Errno %s]" % SOCKET_BLOCK_ERRORCODE in text:
            self._logger.error(
                "Unable to send data due to socket.error %s", SOCKET_BLOCK_ERRORCODE
            )
            return

        if "socket.error" in text and "[Errno 51]" in text:
            self._logger.error("Could not send data: network is unreachable.")
            return

        if "socket.error" in text and "[Errno 16]" in text:
            self._logger.error("Could not send data: socket is busy.")
            return

        if "socket.error" in text and "[Errno 11001]" in text:
            self._logger.error("Unable to perform DNS lookup.")
            return

        if "socket.error" in text and "[Errno 10053]" in text:
            self._logger.error(
                "An established connection was aborted by the software in your host machine."
            )
            return

        if "socket.error" in text and "[Errno 10054]" in text:
            self._logger.error("Connection forcibly closed by the remote host.")
            return

        if "socket.gaierror" in text and "[Errno 10022]" in text:
            self._logger.error("Failed to get address info. Error code: 10022")
            return

        if "exceptions.ValueError: Invalid DNS-ID" in text:
            self._logger.error("Invalid DNS-ID")
            return

        if "twisted.web._newclient.ResponseNeverReceived" in text:
            self._logger.error(
                "Internal Twisted response error, consider updating your Twisted version."
            )
            return

        if "twisted.internet.error.AlreadyCalled" in text:
            self._logger.error("Tried to cancel an already called event\n%s", text)
            return

        if "builtins.OSError" in text and "[Errno 16]" in text:
            self._logger.error("Socket error: Device or resource busy. Error code: 16")
            return

        # We already have a check for invalid infohash when adding a torrent, but if somehow we get this
        # error then we simply log and ignore it.
        if "exceptions.RuntimeError: invalid info-hash" in text:
            self._logger.error("Invalid info-hash found")
            return

        if self.lm.api_manager and len(text) > 0:
            self.lm.api_manager.root_endpoint.events_endpoint.on_tribler_exception(text)
            self.lm.api_manager.root_endpoint.state_endpoint.on_tribler_exception(text)