Skip to content

gh-73069: fix race conditions in os.scandir and associated direntry #131642

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
45 changes: 45 additions & 0 deletions Lib/test/test_os.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import sysconfig
import tempfile
import textwrap
import threading
import time
import types
import unittest
Expand All @@ -37,6 +38,7 @@
from test.support import socket_helper
from test.support import infinite_recursion
from test.support import warnings_helper
from test.support import threading_helper
from platform import win32_is_iot

try:
Expand Down Expand Up @@ -5406,6 +5408,49 @@ def test_resource_warning(self):
with self.check_no_resource_warning():
del iterator

@support.requires_resource('cpu')
@threading_helper.requires_working_threading()
def test_races_scandir_iterations(self):
N = 4
ITERATIONS = 10 if support.check_sanitizer(address=True) else 1000

self.create_file("file.txt")
self.create_file("file2.txt")

def scan(iter, barrier):
barrier.wait()
for entry in iter:
pass

for _ in range(ITERATIONS):
barrier = threading.Barrier(N)
iter = os.scandir(self.path)

threads = [threading.Thread(target=scan, args=(iter, barrier))
for _ in range(N)]
with threading_helper.start_threads(threads):
pass

@threading_helper.requires_working_threading()
@unittest.skipUnless(support.check_sanitizer(address=True), "requires ASAN")
def test_races_scandir_entry(self):
N = 2
ITERATIONS = 10

self.create_file("file.txt")
self.create_file("file2.txt")

def stat(entry, barrier):
barrier.wait()
entry.stat()

for _ in range(ITERATIONS):
barrier = threading.Barrier(N)
for entry in os.scandir(self.path):
threads = [threading.Thread(target=stat, args=(entry, barrier))
for _ in range(N)]
with threading_helper.start_threads(threads):
pass

class TestPEP519(unittest.TestCase):

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed some race conditions in os.scandir and associated direntry.
Loading
Loading