Интернеты пишут:
It is actually safe to iterate over a WeakKeyDictionary, WeakValueDictionary, or WeakSet in Python 2.7 or Python 3.1+. They put in an iteration guard that prevents weakref callbacks from removing references from the underlying dict or set during iteration all the way back in 2010, but the docs never got updated.
With the guard in, if an entry dies before iteration reaches it, iteration will skip that entry, but it won’t result in a segfault or a RuntimeError or anything. Dead entries will be added to a list of pending removals and handled later.
Мда? Это, мять, тогда что?!
File "/root/src/Site/SiteHelpers.py", line 84, in processReqs
for key, req in self.peer_reqs.items():
File "/usr/lib/python3.8/weakref.py", line 208, in items
for k, wr in self.data.items():
RuntimeError: dictionary changed size during iteration
Не верьте интернетам.
Вариант с for k,v in list(d.items())
тоже не прокатит. На последней итерации сборщик мусора может хлопнуть list() и следом начать удалять слабые ссылки из словаря.
list() нужно хранить отдельно на протяжении итерирования:
items = list(d.items())
for k,v in items: