Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

Closed
Labels
@effigies

Description

@effigies

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.

importimportlib.utilimportsysimportthreadingimporttime# Lazy load httpspec=importlib.util.find_spec("http")module=importlib.util.module_from_spec(spec)http=sys.modules["http"]=moduleloader=importlib.util.LazyLoader(spec.loader)loader.exec_module(module)defcheck():time.sleep(0.2)returnhttp.HTTPStatus.ACCEPTED==202defmulticheck():for_inrange(10):threading.Thread(target=check).start()ifsys.argv[1:]== ["single"]:check()else:multicheck()

The issue is here:

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. Theself.__class__ = type.ModuleType would need to be moved below__dict__.update(), which in turn would mean thatself.__spec__ andself.__dict__ would need to change toobject.__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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp