# Buggy code
def _broadcast_worker(self) -> None:
    def _broadcast(room_name: str, serialized_message: str) -> None:
        if not any(suffix in room_name for suffix in self._config.broadcast_rooms):
            raise RuntimeError(
                f'Broadcast called on non-public room "{room_name}". '
                f"Known public rooms: {self._config.broadcast_rooms}."
            )
        room_name = make_room_alias(self.chain_id, room_name)
        if room_name not in self._broadcast_rooms:
            room = join_broadcast_room(
                self._client, f"#{room_name}:{self._server_name}"
            )
            self._broadcast_rooms[room_name] = room

        existing_room = self._broadcast_rooms.get(room_name)
        assert existing_room, f"Unknown broadcast room: {room_name!r}"

        self.log.debug(
            "Broadcast",
            room_name=room_name,
            room=existing_room,
            data=serialized_message.replace("\n", "\\n"),
        )
        existing_room.send_text(serialized_message)

    while not self._stop_event.ready():
        self._broadcast_event.clear()
        messages: Dict[str, List[Message]] = defaultdict(list)
        while self._broadcast_queue.qsize() > 0:
            room_name, message = self._broadcast_queue.get()
            messages[room_name].append(message)
        for room_name, messages_for_room in messages.items():
            message_text = "\n".join(
                MessageSerializer.serialize(message) for message in messages_for_room
            )
            _broadcast(room_name, message_text)
            for _ in messages_for_room:
                # Every message needs to be marked as done.
                # Unfortunately there's no way to do that in one call :(
                # https://github.com/gevent/gevent/issues/1436
                self._broadcast_queue.task_done()

        # Stop prioritizing broadcast messages after initial queue has been emptied
        self._prioritize_broadcast_messages = False
        self._broadcast_event.wait(self._config.retry_interval)


# Traceback (matrix_client.errors.MatrixRequestError)
Traceback (most recent call last):
File "src/gevent/greenlet.py", line 716, in gevent._greenlet.Greenlet.run
File "site-packages/raiden/network/transport/matrix.py", line 263, in _run
File "site-packages/raiden/network/transport/matrix.py", line 254, in _check_and_send
File "site-packages/raiden/network/transport/matrix.py", line 1023, in _send_raw
File "site-packages/matrix_client/room.py", line 105, in send_text
File "site-packages/matrix_client/api.py", line 336, in send_message
File "site-packages/matrix_client/api.py", line 256, in send_message_event
File "site-packages/raiden_libs/network/matrix/client.py", line 75, in _send
File "site-packages/matrix_client/api.py", line 691, in _send
matrix_client.errors.MatrixRequestError: 413: {"errcode":"M_TOO_LARGE","error":"event too large"}
2019-02-19T12:10:29Z <Greenlet "RetryQueue:b2c94bba" at 0x7f077d3a9158: <bound method _RetryQueue._run of <_RetryQueue for 0xb2c94bbaf307c6dfbf70cc2537385553385a345b>>> failed with MatrixRequestError

# Fixed code
def _broadcast_worker(self) -> None:
    def _broadcast(room_name: str, serialized_message: str) -> None:
        if not any(suffix in room_name for suffix in self._config.broadcast_rooms):
            raise RuntimeError(
                f'Broadcast called on non-public room "{room_name}". '
                f"Known public rooms: {self._config.broadcast_rooms}."
            )
        room_name = make_room_alias(self.chain_id, room_name)
        if room_name not in self._broadcast_rooms:
            room = join_broadcast_room(
                self._client, f"#{room_name}:{self._server_name}"
            )
            self._broadcast_rooms[room_name] = room

        existing_room = self._broadcast_rooms.get(room_name)
        assert existing_room, f"Unknown broadcast room: {room_name!r}"

        self.log.debug(
            "Broadcast",
            room_name=room_name,
            room=existing_room,
            data=serialized_message.replace("\n", "\\n"),
        )
        existing_room.send_text(serialized_message)

    while not self._stop_event.ready():
        self._broadcast_event.clear()
        messages: Dict[str, List[Message]] = defaultdict(list)
        while self._broadcast_queue.qsize() > 0:
            room_name, message = self._broadcast_queue.get()
            messages[room_name].append(message)
        for room_name, messages_for_room in messages.items():
            serialized_messages = (
                MessageSerializer.serialize(message) for message in messages_for_room
            )
            for message_batch in make_message_batches(serialized_messages):
                _broadcast(room_name, message_batch)
            for _ in messages_for_room:
                # Every message needs to be marked as done.
                # Unfortunately there's no way to do that in one call :(
                # https://github.com/gevent/gevent/issues/1436
                self._broadcast_queue.task_done()

        # Stop prioritizing broadcast messages after initial queue has been emptied
        self._prioritize_broadcast_messages = False
        self._broadcast_event.wait(self._config.retry_interval)