Gregory P. Smith <g...@krypto.org> added the comment:

fyi - we just had a test run into this (in a flaky manner - definitely a race 
condition) at work:

```
...
    for f in inspect.stack(context=0)
  File "<embedded stdlib>/inspect.py", line 1499, in stack
    return getouterframes(sys._getframe(1), context)
  File "<embedded stdlib>/inspect.py", line 1476, in getouterframes
    frameinfo = (frame,) + getframeinfo(frame, context)
  File "<embedded stdlib>/inspect.py", line 1446, in getframeinfo
    filename = getsourcefile(frame) or getfile(frame)
  File "<embedded stdlib>/inspect.py", line 696, in getsourcefile
    if getattr(getmodule(object, filename), '__loader__', None) is not None:
  File "<embedded stdlib>/inspect.py", line 732, in getmodule
    for modname, module in list(sys.modules.items()):
RuntimeError: dictionary changed size during iteration
```

We haven't diagnosed what was leading to it though.  Trust in the ability to 
use inspect.stack() -> ... -> inspect.getmodule() in multithreaded code is on 
the way out as a workaround.

(this was on 3.6.7)

A workaround we could checkin without consequences should be to change

list(sys.modules.items())  into  list(sys.modules.copy().items()).

I was a bit surprised to see this happen at all, list(dict.items()) seems like 
it should've been done entirely in C with the GIL held the entire time.  but 
maybe I'm just missing where the GIL would be released in the calls to exhause 
the iterator made by 
https://github.com/python/cpython/blob/master/Objects/listobject.c ?

----------
nosy: +gregory.p.smith

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue13487>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to