Skip to content

Image.open: dictionary changed size during iteration (race condition) #7717

@Mindstan

Description

@Mindstan

What did you do?

We are running a Django server with Gunicorn, with a view that is returning a thumbnail from an existing PNG image.
We open it from a BytesIO with PIL.Image.open.

What did you expect to happen?

The Image instance is returned by open.

What actually happened?

Python interpreter raised RuntimeError(dictionary changed size during iteration) on this line :

  File "PIL/Image.py", line 3258, in open
    preinit()
  File "PIL/Image.py", line 320, in preinit
    from . import GifImagePlugin
  File "PIL/GifImagePlugin.py", line 34, in <module>
    from . import (
  File "PIL/ImageMath.py", line 217, in <module>
    for k, v in list(globals().items()):

What are your OS, Python and Pillow versions?

  • OS: Ubuntu 22.04 AArch64
  • Python: 3.9.13 (CPython)
  • Pillow: 10.2.0

Unfortunately, it looks like it is a race condition on globals() with another lazy-loaded dependency, I don't have a minimal code that can reproduce this bug consistently. It is highly unlikely to happen, but is has happened once on our servers.

# in a Django view
pil_image = Image.open(raw_image_bytes)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions