# Buggy code
def cleanup(self):
    """
    Remove uninstalled tools from tool cache if they are not on disk anymore or if their content has changed.

    Returns list of tool_ids that have been removed.
    """
    removed_tool_ids = []
    try:
        paths_to_cleanup = {
            path: tool.all_ids
            for path, tool in self._tools_by_path.items()
            if self._should_cleanup(path)
        }
        for config_filename, tool_ids in paths_to_cleanup.items():
            del self._hash_by_tool_paths[config_filename]
            del self._tools_by_path[config_filename]
            for tool_id in tool_ids:
                if tool_id in self._tool_paths_by_id:
                    del self._tool_paths_by_id[tool_id]
            removed_tool_ids.extend(tool_ids)
        for tool_id in removed_tool_ids:
            self._removed_tool_ids.add(tool_id)
            if tool_id in self._new_tool_ids:
                self._new_tool_ids.remove(tool_id)
    except Exception:
        # If by chance the file is being removed while calculating the hash or modtime
        # we don't want the thread to die.
        pass
    return removed_tool_ids


# Traceback (KeyError)
galaxy.tools.toolbox.base INFO 2018-08-09 10:57:04,513 Parsing the tool configuration /private/var/folders/df/6xqpqpcd7h73b6jpx9t6cwhw0000gn/T/tmpicezz6xb/tool_conf.xml
galaxy.tools.toolbox.base ERROR 2018-08-09 10:57:04,519 Error reading tool from path: /Users/mvandenb/src/tools-iuc/tools/fastp/fastp.xml
Traceback (most recent call last):
File "/private/var/folders/df/6xqpqpcd7h73b6jpx9t6cwhw0000gn/T/tmpicezz6xb/galaxy-dev/lib/galaxy/tools/toolbox/base.py", line 567, in _load_tool_tag_set
tool = self.load_tool(concrete_path, use_cached=False)
File "/private/var/folders/df/6xqpqpcd7h73b6jpx9t6cwhw0000gn/T/tmpicezz6xb/galaxy-dev/lib/galaxy/tools/toolbox/base.py", line 751, in load_tool
tool = self.create_tool(config_file=config_file, repository_id=repository_id, guid=guid, **kwds)
File "/private/var/folders/df/6xqpqpcd7h73b6jpx9t6cwhw0000gn/T/tmpicezz6xb/galaxy-dev/lib/galaxy/tools/__init__.py", line 270, in create_tool
raise e
KeyError: 'adapter_trimming_options'

# Fixed code
def cleanup(self):
    """
    Remove uninstalled tools from tool cache if they are not on disk anymore or if their content has changed.

    Returns list of tool_ids that have been removed.
    """
    removed_tool_ids = []
    try:
        paths_to_cleanup = {
            path: tool.all_ids
            for path, tool in self._tools_by_path.items()
            if self._should_cleanup(path)
        }
        for config_filename, tool_ids in paths_to_cleanup.items():
            del self._hash_by_tool_paths[config_filename]
            if os.path.exists(config_filename):
                # This tool has probably been broken while editing on disk
                # We record it here, so that we can recover it
                self._removed_tools_by_path[config_filename] = self._tools_by_path[
                    config_filename
                ]
            del self._tools_by_path[config_filename]
            for tool_id in tool_ids:
                if tool_id in self._tool_paths_by_id:
                    del self._tool_paths_by_id[tool_id]
            removed_tool_ids.extend(tool_ids)
        for tool_id in removed_tool_ids:
            self._removed_tool_ids.add(tool_id)
            if tool_id in self._new_tool_ids:
                self._new_tool_ids.remove(tool_id)
    except Exception:
        # If by chance the file is being removed while calculating the hash or modtime
        # we don't want the thread to die.
        pass
    return removed_tool_ids