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

gh-114763: Protect lazy loading modules from attribute access race#114781

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
brettcannon merged 12 commits intopython:mainfromeffigies:fix-issue-114763
Feb 24, 2024
Merged
Changes from1 commit
Commits
Show all changes
12 commits
Select commitHold shift + click to select a range
d57f4eb
gh-114763: Protect lazy loading modules from attribute access race
effigiesJan 31, 2024
3db717c
TEST: Reproduce gh-114763
effigiesJan 31, 2024
5424d0c
Add new blurb
effigiesJan 31, 2024
96c08c5
Improve comments around the branching
effigiesFeb 1, 2024
3642ddc
TEST: Use TestCase.assert* methods
effigiesFeb 1, 2024
46238e5
Merge branch 'main' into fix-issue-114763
brettcannonFeb 6, 2024
6accf0c
Apply suggestions from code review
effigiesFeb 17, 2024
bb2292f
TEST: Require working threading for test_module_load_race
effigiesFeb 17, 2024
57fe084
Move threading import to module level of importlib.util
effigiesFeb 17, 2024
40383a0
Make is_loading a bool instead of Event
effigiesFeb 17, 2024
6322dd6
Merge branch 'main' into fix-issue-114763
effigiesFeb 23, 2024
023d65d
Merge branch 'main' into fix-issue-114763
brettcannonFeb 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
PrevPrevious commit
NextNext commit
TEST: Reproducegh-114763
  • Loading branch information
@effigies
effigies committedFeb 5, 2024
commit3db717c7bbf0f3a6c013bf704718ee28a6edd275
40 changes: 38 additions & 2 deletionsLib/test/test_importlib/test_lazy.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2,6 +2,8 @@
from importlib import abc
from importlib import util
import sys
import time
import threading
import types
import unittest

Expand DownExpand Up@@ -40,6 +42,7 @@ class TestingImporter(abc.MetaPathFinder, abc.Loader):
module_name = 'lazy_loader_test'
mutated_name = 'changed'
loaded = None
load_count = 0
source_code = 'attr = 42; __name__ = {!r}'.format(mutated_name)

def find_spec(self, name, path, target=None):
Expand All@@ -48,8 +51,10 @@ def find_spec(self, name, path, target=None):
return util.spec_from_loader(name, util.LazyLoader(self))

def exec_module(self, module):
time.sleep(0.01) # Simulate a slow load.
exec(self.source_code, module.__dict__)
self.loaded = module
self.load_count += 1


class LazyLoaderTests(unittest.TestCase):
Expand All@@ -59,8 +64,9 @@ def test_init(self):
# Classes that don't define exec_module() trigger TypeError.
util.LazyLoader(object)

def new_module(self, source_code=None):
loader = TestingImporter()
def new_module(self, source_code=None, loader=None):
if loader is None:
loader = TestingImporter()
if source_code is not None:
loader.source_code = source_code
spec = util.spec_from_loader(TestingImporter.module_name,
Expand DownExpand Up@@ -140,6 +146,36 @@ def test_module_already_in_sys(self):
# Force the load; just care that no exception is raised.
module.__name__

def test_module_load_race(self):
with test_util.uncache(TestingImporter.module_name):
loader = TestingImporter()
module = self.new_module(loader=loader)
assert loader.load_count == 0

class RaisingThread(threading.Thread):
exc = None
def run(self):
try:
super().run()
except Exception as exc:
self.exc = exc

def access_module():
return module.attr

threads = []
for _ in range(2):
threads.append(thread := RaisingThread(target=access_module))
thread.start()

# Races could cause errors
for thread in threads:
thread.join()
assert thread.exc is None

# Or multiple load attempts
assert loader.load_count == 1


if __name__ == '__main__':
unittest.main()

[8]ページ先頭

©2009-2025 Movatter.jp