Delgan / loguru

Python logging made (stupidly) simple
MIT License
19.43k stars 690 forks source link

RuntimeError when reinitializing logger #1183

Open jeremyk opened 1 month ago

jeremyk commented 1 month ago

We hit this error intermittently as we have to reinitialize our logger at least once during our application startup.

File "/opt/pysetup/.venv/lib/python3.11/site-packages/loguru/_logger.py", line 2031, in _log
    for handler in core.handlers.values():
RuntimeError: dictionary changed size during iteration

I think this can be fixed by adding a lock before this line: https://github.com/Delgan/loguru/blob/master/loguru/_logger.py#L2036

or making sure it is iterating over a copy:

list(core.handlers.values())
horatiu-negutoiu commented 1 month ago

+1

ericscales commented 1 month ago

Another +1

jeremyk commented 1 month ago

Just to sanity check that this is needed:

>>> x = {"a": "1", "b": "2"}
>>> y = x.values()
>>> z = list(x.values())
>>> x["c"] = "3"
>>> y
dict_values(['1', '2', '3'])
>>> z
['1', '2']