GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Accessing attributes of a lazily-loaded module is not thread-safe · Issue #114763 · python/cpython

Accessing attributes of a lazily-loaded module is not thread-safe #114763

New issue
Copy link
New issue
Copy link
Closed
#114781
Closed
Accessing attributes of a lazily-loaded module is not thread-safe#114763
#114781
Copy link
Labels
topic-importlibtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@effigies
effigies
opened on Jan 30, 2024
Issue body actions

Bug report

Bug description:

Attempting to access an attribute of a lazily-loaded module causes the module's __class__ to be reset before its attributes have been populated.

import importlib.util
import sys
import threading
import time

# Lazy load http
spec = importlib.util.find_spec("http")
module = importlib.util.module_from_spec(spec)
http = sys.modules["http"] = module

loader = importlib.util.LazyLoader(spec.loader)
loader.exec_module(module)

def check():
    time.sleep(0.2)
    return http.HTTPStatus.ACCEPTED == 202

def multicheck():
    for _ in range(10):
        threading.Thread(target=check).start()

if sys.argv[1:] == ["single"]:
    check()
else:
    multicheck()

The issue is here:

cpython/Lib/importlib/util.py

Lines 168 to 177 in 6de8aa3

class _LazyModule(types.ModuleType):
"""A subclass of the module type which triggers loading upon attribute access."""
def __getattribute__(self, attr):
"""Trigger the load of the module and return the attribute."""
# All module metadata must be garnered from __spec__ in order to avoid
# using mutated values.
# Stop triggering this method.
self.__class__ = types.ModuleType

When attempting to access an attribute, the module's __dict__ is not updated until after __class__ is reset. If other threads attempt to access between these two points, then an attribute lookup can fail.

Assuming this is considered a bug, the two fixes I can think of are:

  1. A module-scoped lock that is used to protect __getattribute__'s critical section. The self.__class__ = type.ModuleType would need to be moved below __dict__.update(), which in turn would mean that self.__spec__ and self.__dict__ would need to change to object.__getattribute__(self, ...) lookups to avoid recursion.
  2. A module-scoped dictionary of locks, one-per-_LazyModule. Here, additional work would be needed to remove no-longer-needed locks without creating another critical section where a thread enters _LazyModule.__getattribute__ but looks up its lock after it is removed by the first thread.

My suspicion is that one lock is enough, so I would suggest going with 1.

CPython versions tested on:

3.8, 3.10, 3.11, 3.12

Operating systems tested on:

Linux

Linked PRs

  • gh-114763: Protect lazy loading modules from attribute access race #114781
  • [3.12] gh-114763: Protect lazy loading modules from attribute access races (GH-114781) #115870
  • [3.11] gh-114763: Protect lazy loading modules from attribute access races (GH-114781) #115871

Activity

  1. effigies
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    on Jan 30, 2024
  2. AlexWaygood
    added
    topic-importlib
    on Jan 30, 2024
  3. effigies
    added 4 commits that reference this issue on Jan 31, 2024

    pythongh-114763: Protect lazy loading modules from attribute access race

    04c6f96

    TEST: Reproduce pythongh-114763

    eb3a51d

    pythongh-114763: Protect lazy loading modules from attribute access race

    d57f4eb

    TEST: Reproduce pythongh-114763

    3db717c
  4. brettcannon
    added a commit that references this issue on Feb 24, 2024

    gh-114763: Protect lazy loading modules from attribute access races (G…

    200271c
  5. brettcannon
    closed this as completedin #114781on Feb 24, 2024
  6. miss-islington
    added 2 commits that reference this issue on Feb 24, 2024

    pythongh-114763: Protect lazy loading modules from attribute access r…

    21221d5

    pythongh-114763: Protect lazy loading modules from attribute access r…

    97d883b
  7. brettcannon
    added 2 commits that reference this issue on Feb 26, 2024

    [3.12] gh-114763: Protect lazy loading modules from attribute access …

    7b91b90

    [3.11] gh-114763: Protect lazy loading modules from attribute access …

    46f821d
  8. woodruffw
    added a commit that references this issue on Mar 4, 2024

    pythongh-114763: Protect lazy loading modules from attribute access r…

    d84c56e
  9. diegorusso
    added a commit that references this issue on Apr 17, 2024

    pythongh-114763: Protect lazy loading modules from attribute access r…

    31c864c
  10. LukasWoodtli
    added a commit that references this issue on Jan 22, 2025

    pythongh-114763: Protect lazy loading modules from attribute access r…

    d990899
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    topic-importlibtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402503 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.