This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: sys.modules: dictionary changed size during iteration
Type: behavior Stage: resolved
Components: Documentation Versions: Python 3.11
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: docs@python Nosy List: Dennis Sweeney, docs@python, gregory.p.smith, idan57, miss-islington, terry.reedy
Priority: normal Keywords: patch

Created on 2021-10-03 14:36 by idan57, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 28842 merged gregory.p.smith, 2021-10-09 19:26
PR 28843 merged miss-islington, 2021-10-09 19:34
Messages (8)
msg403089 - (view) Author: Idan Cohen (idan57) Date: 2021-10-03 14:36
Hi,

When iterating over sys.modules it might be that because of lazy loading or that other module reload is being reloaded (even if sys.modules.copy() is being used) you will get:

"RuntimeError: dictionary changed size during iteration"

The usage of sys.modules and iteration over it is used in many places

I know that this is a known issue but didn't find any solution or fix for that. Also, if there is some work around that might solve this, I would appreciate it.

Thanks,
Idan
msg403097 - (view) Author: Dennis Sweeney (Dennis Sweeney) * (Python committer) Date: 2021-10-03 16:15
One standard way of preventing this is copying the dictionary whenever there's risk of it changing out from under you, as in:

modules = sys.modules.copy()
for key, value in modules.items():
    ...
msg403503 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-10-08 20:41
Do you have an example for which "for m in sys.modules: print(m, end=' ')" fails?  In particular, is there code in the stdlib that fails iterating over sys.modules?  If not the latter, this should be closed as 'Not a bug'.

Note that questions about using Python should go elsewhere, such as python-list or stackoverflow.
msg403529 - (view) Author: Idan Cohen (idan57) Date: 2021-10-09 11:58
An example can be found here: https://github.com/python/cpython/commit/7058d2d96c5ca4dfc6c754c5cd737c6eb2a8fd67
https://bugs.python.org/issue13487

Those links are about an issue that was until March 2020 with how sys.modules is iterated. It is not safe to iterate over since it can get updated during the iteration because of lazy importing for example.

If you look in older reports you can find that there us an issue with iterating over it and it was not addressed yet at all.
msg403545 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2021-10-09 18:13
In #13487, Gregory fixed the problem by using .copy().  That seems to have worked for 1 1/2 years.  You still have not reported an actual bug in the current CPython stdlib.

Perhaps we should mention in https://docs.python.org/3/library/sys.html#sys.modules that sys.modules can be unexpectedly changed during iteration by lazy imports or other threads, so that a copy might be needed.

Gregory, what do you think?
msg403548 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2021-10-09 19:34
New changeset 3d1ca867ed0e3ae343166806f8ddd9739e568ab4 by Gregory P. Smith in branch 'main':
bpo-45353: Remind sys.modules users to copy when iterating. (GH-28842)
https://github.com/python/cpython/commit/3d1ca867ed0e3ae343166806f8ddd9739e568ab4
msg403549 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2021-10-09 19:38
While arguably unnecessary as it is documented as a dictionary and this is a normal Python dict behavior, it is a global dict and it can be modified at times that are unintuitive to users of all experience levels.  A note in the documentation makes sense.
msg403551 - (view) Author: miss-islington (miss-islington) Date: 2021-10-09 19:54
New changeset 459a4db5eae1f5ef063b34c61cc099820aa9ed0a by Miss Islington (bot) in branch '3.10':
bpo-45353: Remind sys.modules users to copy when iterating. (GH-28842)
https://github.com/python/cpython/commit/459a4db5eae1f5ef063b34c61cc099820aa9ed0a
History
Date User Action Args
2022-04-11 14:59:50adminsetgithub: 89516
2021-10-09 21:06:59terry.reedysetstage: commit review -> resolved
2021-10-09 19:54:29miss-islingtonsetmessages: + msg403551
2021-10-09 19:38:38gregory.p.smithsetstatus: open -> closed

assignee: docs@python
components: + Documentation, - Library (Lib)

nosy: + docs@python
messages: + msg403549
resolution: fixed
stage: patch review -> commit review
2021-10-09 19:34:20miss-islingtonsetnosy: + miss-islington
pull_requests: + pull_request27155
2021-10-09 19:34:16gregory.p.smithsetmessages: + msg403548
2021-10-09 19:26:13gregory.p.smithsetkeywords: + patch
stage: test needed -> patch review
pull_requests: + pull_request27154
2021-10-09 18:13:17terry.reedysetnosy: + gregory.p.smith
messages: + msg403545
2021-10-09 11:58:28idan57setmessages: + msg403529
2021-10-08 20:41:27terry.reedysetversions: + Python 3.11, - Python 3.8
nosy: + terry.reedy

messages: + msg403503

type: behavior
stage: test needed
2021-10-03 16:15:17Dennis Sweeneysetnosy: + Dennis Sweeney
messages: + msg403097
2021-10-03 14:36:51idan57create