# Buggy code
def load_elements_generator(elements, factory, modeling_language, gaphor_version):
    """Load a file and create a model if possible.

    Exceptions: IOError, ValueError.
    """
    log.debug(f"Loading {len(elements)} elements")

    # The elements are iterated three times:
    size = len(elements) * 3

    def update_status_queue(_n=[0]):
        n = _n[0] = _n[0] + 1
        if n % 30 == 0:
            yield (n * 100) / size

    # First create elements and canvas items in the factory
    # The elements are stored as attribute 'element' on the parser objects:
    yield from _load_elements_and_canvasitems(
        elements, factory, modeling_language, gaphor_version, update_status_queue
    )
    yield from _load_attributes_and_references(elements, update_status_queue)

    for id, elem in list(elements.items()):
        yield from update_status_queue()
        elem.element.postload()


# Traceback (RuntimeError)
Traceback (most recent call last):
File "/home/dan/Projects/gaphor/gaphor/ui/actiongroup.py", line 138, in _action_activate
method(from_variant(param))
File "/home/dan/Projects/gaphor/gaphor/ui/appfilemanager.py", line 121, in action_open_recent
self.load(filename)
File "/home/dan/Projects/gaphor/gaphor/ui/appfilemanager.py", line 49, in load
file_manager.load(filename)
File "/home/dan/Projects/gaphor/gaphor/ui/filemanager.py", line 109, in load
worker.reraise()
File "/home/dan/Projects/gaphor/gaphor/ui/gidlethread.py", line 115, in reraise
raise exc
File "/home/dan/Projects/gaphor/gaphor/ui/gidlethread.py", line 119, in __generator_executer
result = next(self._generator)
File "/home/dan/Projects/gaphor/gaphor/storage/storage.py", line 308, in load_generator
factory.model_ready()
File "/home/dan/Projects/gaphor/gaphor/core/modeling/elementfactory.py", line 159, in model_ready
self.handle(ModelReady(self))
File "/home/dan/Projects/gaphor/gaphor/core/modeling/elementfactory.py", line 182, in handle
self.event_manager.handle(event)
File "/home/dan/Projects/gaphor/gaphor/core/eventmanager.py", line 55, in handle
self._events.handle(e)
File "/home/dan/Projects/gaphor/.venv/lib/python3.9/site-packages/generic/event.py", line 64, in handle
handler(event)
File "/home/dan/Projects/gaphor/gaphor/ui/mainwindow.py", line 237, in _new_model_content
for diagram in self.element_factory.select(
File "/home/dan/Projects/gaphor/gaphor/core/modeling/elementfactory.py", line 116, in select
yield from (e for e in self._elements.values() if expression(e))
File "/home/dan/Projects/gaphor/gaphor/core/modeling/elementfactory.py", line 116, in <genexpr>
yield from (e for e in self._elements.values() if expression(e))
RuntimeError: OrderedDict mutated during iteration

# Fixed code
def load_elements_generator(elements, factory, modeling_language, gaphor_version):
    """Load a file and create a model if possible.

    Exceptions: IOError, ValueError.
    """
    log.debug(f"Loading {len(elements)} elements")

    # The elements are iterated three times:
    size = len(elements) * 3

    def update_status_queue(_n=[0]):
        n = _n[0] = _n[0] + 1
        if n % 30 == 0:
            yield (n * 100) / size

    # First create elements and canvas items in the factory
    # The elements are stored as attribute 'element' on the parser objects:
    yield from _load_elements_and_canvasitems(
        elements, factory, modeling_language, gaphor_version, update_status_queue
    )
    yield from _load_attributes_and_references(elements, update_status_queue)

    ensure_style_sheet_is_present(factory)

    for id, elem in list(elements.items()):
        yield from update_status_queue()
        elem.element.postload()