classification
Title: inspect.getmodule fails when module imports change sys.modules
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.7, Python 3.6
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: eric.araujo Nosy List: Erik.Tollerud, amaury.forgeotdarc, eric.araujo, eric.snow, meador.inge, psimons, python-dev, serhiy.storchaka, tomdzk
Priority: normal Keywords: patch

Created on 2011-11-27 03:06 by Erik.Tollerud, last changed 2017-09-16 12:09 by psimons.

Files
File name Uploaded Description Edit
getmodulefix.patch Erik.Tollerud, 2011-11-27 03:06 review
Messages (12)
msg148438 - (view) Author: Erik Tollerud (Erik.Tollerud) Date: 2011-11-27 03:06
The inspect.getmodule function crashes if packages are installed that futz with sys.modules while they are being tested for module status or the like.  I'm not actually sure which packages are doing this, but the symptom is the for loop over sys.modules raises an Exception because it is modified while the loop is running.

This is *not* a problem in Python 2.x because sys.modules.items() returns a copy of the dictionary instead of an iterator, and 3.x changes that behavior.  The comment above the for loop makes it clear that the expected behavior is a copy rather than an iterator, so the attached patch corrects the problem by simply wrapping the items() call in list().
msg148459 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2011-11-27 21:46
You are certainly right, but I wonder how this can happen.
Are there modules which import something just by looking at them?
Or is is some race condition due to another running thread?
msg148487 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2011-11-28 15:08
Maybe it can be caused by an installation happening during the loop.  I agree with Erik’s reading of the comment and patch, and don’t think a test is needed.
msg148509 - (view) Author: Erik Tollerud (Erik.Tollerud) Date: 2011-11-28 18:37
The package that triggers it for me is the py (http://pypi.python.org/pypi/py) package - when in gets imported, it does some trick with sys.modules that is in place to get around some pickling restriction, but that means sys.modules is altered during the import of the `py` package... and that triggers the exception.
msg148511 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2011-11-28 18:52
When a package is imported sys.modules changes... nothing special here.
But it's true true that py.std, for example, is a "lazy" module with a special __getattr__ that will import submodules.

Patch looks good to me as well.
msg148581 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2011-11-29 16:15
New changeset 2ef359d7a2e9 by Éric Araujo in branch '3.2':
Fix inspect.getmodule to use a copy of sys.modules for iteration (#13487).
http://hg.python.org/cpython/rev/2ef359d7a2e9
msg148583 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2011-11-29 16:19
Committed, thanks.
msg299829 - (view) Author: Patrik Simons (psimons) Date: 2017-08-07 07:57
list(sys.modules.items()) still raises RuntimeError: dictionary changed size during iteration when another thread imports a module.

I would assume dict.copy() is thread-safe so a working fix could use sys.modules.copy().items()

I hit this bug when printing the name of the caller function using
inspect.stack(). The workaround in this case is calling inspect.stack(context=0).
msg301376 - (view) Author: Thomas Dudziak (tomdzk) Date: 2017-09-05 21:12
This bug seems to be still there. I had an application fail with this same error in inspect.getouterframes with Python 3.6.2. As far as I could trace it, during iteration over sys.modules _tracemalloc and tracemalloc were added, not quite sure from where (maybe the warnings module ?)
msg302312 - (view) Author: Éric Araujo (eric.araujo) * (Python committer) Date: 2017-09-16 02:32
Could you give code to reproducer the problem, if possible without third-party dependencies?
msg302327 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2017-09-16 06:24
dict.copy() is not thread-safe still (but it can be made thread-safe).

list(dict) is thread-safe. It copies a list of keys only.
msg302337 - (view) Author: Patrik Simons (psimons) Date: 2017-09-16 12:09
I cannot reproduce. In fact I cannot even get list(d.items())
to raise RuntimeError: dictionary changed size during iteration
for any dict d.
History
Date User Action Args
2017-09-16 12:09:04psimonssetmessages: + msg302337
2017-09-16 06:24:41serhiy.storchakasetnosy: + serhiy.storchaka
messages: + msg302327
2017-09-16 02:32:57eric.araujosetstatus: closed -> open
resolution: fixed ->
messages: + msg302312

versions: + Python 3.6, Python 3.7, - Python 3.2, Python 3.3
2017-09-05 21:12:00tomdzksetnosy: + tomdzk
messages: + msg301376
2017-08-07 07:57:52psimonssetnosy: + psimons
messages: + msg299829
2011-11-29 16:19:19eric.araujosetstatus: open -> closed
resolution: fixed
messages: + msg148583

stage: patch review -> resolved
2011-11-29 16:15:06python-devsetnosy: + python-dev
messages: + msg148581
2011-11-29 15:37:31eric.araujosetassignee: eric.araujo
2011-11-28 18:52:47amaury.forgeotdarcsetmessages: + msg148511
2011-11-28 18:37:46Erik.Tollerudsetmessages: + msg148509
2011-11-28 15:08:04eric.araujosetversions: - Python 3.1
nosy: + eric.araujo

messages: + msg148487

type: behavior
stage: patch review
2011-11-27 21:46:56amaury.forgeotdarcsetnosy: + amaury.forgeotdarc
messages: + msg148459
2011-11-27 17:18:55meador.ingesetnosy: + meador.inge
2011-11-27 03:13:56eric.snowsetnosy: + eric.snow
2011-11-27 03:06:48Erik.Tollerudcreate