Skip to content

Bugfix: Ensure that we take a copy of the sys.modules before iterating over it#1514

Merged
Carreau merged 1 commit intoipython:mainfrom
pelson:feature/mutable-sys-modules
May 7, 2026
Merged

Bugfix: Ensure that we take a copy of the sys.modules before iterating over it#1514
Carreau merged 1 commit intoipython:mainfrom
pelson:feature/mutable-sys-modules

Conversation

@pelson
Copy link
Copy Markdown
Member

@pelson pelson commented May 4, 2026

👋

I encountered an issue in the CERN Python distribution when running IPykernel, caused by the fact that Python invocations in our distribution trigger a Python thread which does some logging. The issue:

[I 2026-05-04 15:47:50.633 ServerApp] AsyncIOLoopKernelRestarter: restarting kernel (3/5), new random ports
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/tmp/venv26/lib/python3.14/site-packages/ipykernel_launcher.py", line 18, in <module>
    app.launch_new_instance()
    ~~~~~~~~~~~~~~~~~~~~~~~^^
  File "/tmp/venv26/lib/python3.14/site-packages/traitlets/config/application.py", line 1074, in launch_instance
    app.initialize(argv)
    ~~~~~~~~~~~~~~^^^^^^
  File "/tmp/venv26/lib/python3.14/site-packages/traitlets/config/application.py", line 118, in inner
    return method(app, *args, **kwargs)
  File "/tmp/venv26/lib/python3.14/site-packages/ipykernel/kernelapp.py", line 726, in initialize
    self.init_kernel()
    ~~~~~~~~~~~~~~~~^^
  File "/tmp/venv26/lib/python3.14/site-packages/ipykernel/kernelapp.py", line 573, in init_kernel
    kernel = kernel_factory(
        parent=self,
    ...<12 lines>...
        user_ns=self.user_ns,
    )
  File "/tmp/venv26/lib/python3.14/site-packages/traitlets/config/configurable.py", line 583, in instance
    inst = cls(*args, **kwargs)
  File "/tmp/venv26/lib/python3.14/site-packages/ipykernel/ipkernel.py", line 123, in __init__
    m.__file__ for m in sys.modules.values() if hasattr(m, "__file__") and m.__file__
                        ~~~~~~~~~~~~~~~~~~^^
RuntimeError: dictionary keys changed during iteration

Boils down to the fact that sys.modules is a shared, mutable dictionary that can be modified by background threads. Any mutation during iteration raises RuntimeError: dictionary keys changed during iteration on Python 3.14, which tightened its detection of this condition.

Using .copy() the recommended solution as per bugs.python.org/issue40327, so I applied it to one existing location too.

@pelson pelson force-pushed the feature/mutable-sys-modules branch from 93c4dd5 to a5cfbf7 Compare May 4, 2026 15:22
@pelson pelson changed the title Ensure that we take a copy of the sys.modules before iterating over it Bugfix: Ensure that we take a copy of the sys.modules before iterating over it May 4, 2026
@krassowski krassowski added the bug label May 6, 2026
@Carreau Carreau force-pushed the feature/mutable-sys-modules branch from a5cfbf7 to b1fb179 Compare May 7, 2026 08:30
@Carreau
Copy link
Copy Markdown
Member

Carreau commented May 7, 2026

Looks reasonable to me.

@Carreau
Copy link
Copy Markdown
Member

Carreau commented May 7, 2026

Failure unrelated.

@Carreau Carreau merged commit 2818b66 into ipython:main May 7, 2026
41 of 42 checks passed
@Carreau Carreau added this to the 7.3 milestone May 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants