# Buggy code
def _dump_message(self, error_traceback, message: dict):
    if message is None or getattr(self.parameters, "testing", False):
        return

    self.logger.info("Dumping message to dump file.")

    dump_file = os.path.join(self.parameters.logging_path, self.__bot_id + ".dump")

    timestamp = datetime.utcnow()
    timestamp = timestamp.isoformat()
    new_dump_data = {}
    new_dump_data[timestamp] = {}
    new_dump_data[timestamp]["bot_id"] = self.__bot_id
    new_dump_data[timestamp]["source_queue"] = self.__source_queues
    new_dump_data[timestamp]["traceback"] = error_traceback

    new_dump_data[timestamp]["message"] = message.serialize()

    if os.path.exists(dump_file):
        # existing dump
        mode = "r+"
    else:
        # new dump file
        mode = "w"
    with open(dump_file, mode) as fp:
        for i in range(60):
            try:
                fcntl.flock(fp, fcntl.LOCK_EX | fcntl.LOCK_NB)
            except BlockingIOError:
                if i == 0:
                    self.logger.warning("Dump file is locked, waiting up to 60s.")
                time.sleep(1)
            else:
                break
        else:
            raise ValueError("Dump file was locked for more than 60s, giving up now.")
        if mode == "r+":
            dump_data = json.load(fp)
            dump_data.update(new_dump_data)
        else:
            dump_data = new_dump_data

        fp.seek(0)

        json.dump(dump_data, fp, indent=4, sort_keys=True)

    self.logger.debug("Message dumped.")


# Traceback (ValueError)
2020-01-24 12:07:28,164 - bot-id - ERROR - Pipeline failed.
Traceback (most recent call last):
File "/.../intelmq/intelmq/lib/pipeline.py", line 249, in _receive
return utils.decode(retval)
File "/.../intelmq/intelmq/lib/utils.py", line 102, in decode
".".format(encodings))
ValueError: Could not decode string with given encodings('utf-8',).

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "/.../intelmq/intelmq/lib/bot.py", line 271, in start
self.process()
File "/.../intelmq/intelmq/bots/experts/sieve/expert.py", line 89, in process
event = self.receive_message()
File "/.../intelmq/intelmq/lib/bot.py", line 591, in receive_message
message = self.__source_pipeline.receive()
File "/.../intelmq/intelmq/lib/pipeline.py", line 124, in receive
retval = self._receive()
File "/.../intelmq/intelmq/lib/pipeline.py", line 251, in _receive
raise exceptions.PipelineError(exc)
intelmq.lib.exceptions.PipelineError: pipeline failed - ValueError("Could not decode string with given encodings('utf-8',).",)
...

# Fixed code
def _dump_message(self, error_traceback, message: dict):
    self.logger.info("Dumping message to dump file.")

    if message is None or getattr(self.parameters, "testing", False):
        return

    dump_file = os.path.join(self.parameters.logging_path, self.__bot_id + ".dump")

    timestamp = datetime.utcnow()
    timestamp = timestamp.isoformat()
    new_dump_data = {}
    new_dump_data[timestamp] = {}
    new_dump_data[timestamp]["bot_id"] = self.__bot_id
    new_dump_data[timestamp]["source_queue"] = self.__source_queues
    new_dump_data[timestamp]["traceback"] = error_traceback

    if isinstance(message, bytes):
        # decoding errors
        new_dump_data[timestamp]["message"] = utils.base64_encode(message)
        new_dump_data[timestamp]["message_type"] = "base64"
    else:
        new_dump_data[timestamp]["message"] = message.serialize()

    if os.path.exists(dump_file):
        # existing dump
        mode = "r+"
    else:
        # new dump file
        mode = "w"
    with open(dump_file, mode) as fp:
        for i in range(60):
            try:
                fcntl.flock(fp, fcntl.LOCK_EX | fcntl.LOCK_NB)
            except BlockingIOError:
                if i == 0:
                    self.logger.warning("Dump file is locked, waiting up to 60s.")
                time.sleep(1)
            else:
                break
        else:
            raise ValueError("Dump file was locked for more than 60s, giving up now.")
        if mode == "r+":
            dump_data = json.load(fp)
            dump_data.update(new_dump_data)
        else:
            dump_data = new_dump_data

        fp.seek(0)

        json.dump(dump_data, fp, indent=4, sort_keys=True)

    self.logger.debug("Message dumped.")