Skip to content

Commit c3982ea

Browse files
mmohrhardogrisel
authored andcommitted
make iterating over sys.modules (more) threadsafe (#322)
1 parent 8cf9ec4 commit c3982ea

File tree

2 files changed

+8
-3
lines changed

2 files changed

+8
-3
lines changed

Diff for: CHANGES.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
1.2.3
22
=====
33

4+
- Fix a bug when a thread imports a module while cloudpickle iterates
5+
over the module list
6+
([PR #322](https://github.com/cloudpipe/cloudpickle/pull/322)).
7+
48
1.2.2
59
=====
610

Diff for: cloudpickle/cloudpickle.py

+4-3
Original file line numberDiff line numberDiff line change
@@ -151,9 +151,10 @@ def _whichmodule(obj, name):
151151
module_name = getattr(obj, '__module__', None)
152152
if module_name is not None:
153153
return module_name
154-
# Protect the iteration by using a list copy of sys.modules against dynamic
155-
# modules that trigger imports of other modules upon calls to getattr.
156-
for module_name, module in list(sys.modules.items()):
154+
# Protect the iteration by using a copy of sys.modules against dynamic
155+
# modules that trigger imports of other modules upon calls to getattr or
156+
# other threads importing at the same time.
157+
for module_name, module in sys.modules.copy().items():
157158
if module_name == '__main__' or module is None:
158159
continue
159160
try:

0 commit comments

Comments
 (0)