New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
list(sys.modules.items()) can throw RuntimeError: dictionary changed size during iteration #84507
Comments
We have hit an issue in the pickle module where the code throws an exception in a threaded environment: The interesting piece of the backtrace is: File "/xxx/1004060/lib/python3.7/site-packages/numpy/core/init.py", line 130, in _ufunc_reduce I tried to find a code path that would explain how the dict could be changed while the list is created but have not been able to find a code path that releases the GIL. The executable is using many threads with imports happening in random threads and a custom class loader but we already make sure that the class loader is always holding the GIL. The issue happens quite rarely (maybe once every one thousand's execution) so I don't have a reproducer right now. |
Suggest making this change:
|
We should consider making this change just about everywhere in the standard library. Besides avoiding a race condition, running a for loop over |
I afraid that this is a part of the larger issue (see also bpo-31165). Seems the cause is that the GIL can be released when allocate or reallocate the memory. Does your project use some memory managing hooks? Using sys.modules.copy().items() can help a lot, because you allocate memory for new dict only once. In case of list(sys.modules.items()) you allocate memory for every key-value pair, and may allocate memory several times to resize a list. This reduces the probability of occurring the problem in many times, but not to zero. There is still a chance that the size of the original dict increase when you allocate a memory for a new dict, so copying the data to a new dict can fail or even crash or spoil the memory. |
Does this really affect only python 3.7? We see this in RHEL 8.2, using python 3.6.8: Likely caused by: lvs = dict(self._lvs) Without locking. self._lvs is a dict that may contain 1000's of items. I'm not sure if this is relvant now for upstream, but backport to 3.6 would be useful. |
Just to mention that we have seen similar issues whilst importing scipy in a multiprocessing context. |
We have hit an error with "dictionary changed size during iteration" a few times during this week in telemetry integration tests. It seems like it's hitting only our ubuntu based jobs, I haven't seen this error in a centos based job yet. Example of the failed job can be found in [1], I extracted a traceback, which leads to keystoneauth1 into [2]. According to [3] and [4] using copy() should help with the issue. The python docs [5] indicate, that copy() should always be used when iterating through sys.modules [1] https://zuul.opendev.org/t/openstack/build/c99db592871a441e9cddad2f4e60c2fc/console [2] https://paste.opendev.org/show/bpzng2EUyFh1tvBHczt7/ [3] python/cpython#84507 [4] python/cpython#89516 [5] https://docs.python.org/3/library/sys.html#sys.modules Change-Id: I50500c6a21bbe60050303cea4628ca9b71a3e0eb
* Update keystoneauth from branch 'master' to 3b492a7aa0bcb73c7fff62c8e7b1cd5d42118fbe - Fix "dictionary changed size during iteration" We have hit an error with "dictionary changed size during iteration" a few times during this week in telemetry integration tests. It seems like it's hitting only our ubuntu based jobs, I haven't seen this error in a centos based job yet. Example of the failed job can be found in [1], I extracted a traceback, which leads to keystoneauth1 into [2]. According to [3] and [4] using copy() should help with the issue. The python docs [5] indicate, that copy() should always be used when iterating through sys.modules [1] https://zuul.opendev.org/t/openstack/build/c99db592871a441e9cddad2f4e60c2fc/console [2] https://paste.opendev.org/show/bpzng2EUyFh1tvBHczt7/ [3] python/cpython#84507 [4] python/cpython#89516 [5] https://docs.python.org/3/library/sys.html#sys.modules Change-Id: I50500c6a21bbe60050303cea4628ca9b71a3e0eb
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: