[New-bugs-announce] [issue21905] RuntimeError in pickle.whichmodule when sys.modules if mutated

Olivier Grisel report at bugs.python.org
Wed Jul 2 15:03:12 CEST 2014

New submission from Olivier Grisel:

`pickle.whichmodule` performs an iteration over `sys.modules` and tries to perform `getattr` calls on those modules. Unfortunately some modules such as those from the `six.moves` dynamic module can trigger imports when calling `getattr` on them, hence mutating the `sys.modules` dict and causing a `RuntimeError: dictionary changed size during iteration`.

This would also render `pickle.whichmodule` more thread-safe and cause concurrent thread perform new module imports and `whichmodule` calls.

The attach patch protect the iterator by copying the dict items into a fixed list.

I could write a tests involving dynamic module definitions as done in `six.moves` but it sounds very complicated for such a trivial fix.

components: Library (Lib)
files: pickle_whichmodule.patch
keywords: patch
messages: 222099
nosy: Olivier.Grisel
priority: normal
severity: normal
status: open
title: RuntimeError in pickle.whichmodule  when sys.modules if mutated
type: crash
versions: Python 3.4, Python 3.5
Added file: http://bugs.python.org/file35830/pickle_whichmodule.patch

Python tracker <report at bugs.python.org>

More information about the New-bugs-announce mailing list