Skip to content

Commit 60e7b12

Browse files
committed
pythongh-112536: Add --tsan test for reasonable TSAN execution times.
1 parent 6c4fc20 commit 60e7b12

4 files changed

Lines changed: 59 additions & 1 deletion

File tree

‎Lib/test/libregrtest/cmdline.py‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,7 @@ def __init__(self, **kwargs) -> None:
164164
self.match_tests: TestFilter = []
165165
self.pgo = False
166166
self.pgo_extended = False
167+
self.tsan = False
167168
self.worker_json = None
168169
self.start = None
169170
self.timeout = None
@@ -333,6 +334,8 @@ def _create_parser():
333334
help='enable Profile Guided Optimization (PGO) training')
334335
group.add_argument('--pgo-extended', action='store_true',
335336
help='enable extended PGO training (slower training)')
337+
group.add_argument('--tsan', dest='tsan', action='store_true',
338+
help='enable TSAN test')
336339
group.add_argument('--fail-env-changed', action='store_true',
337340
help='if a test file alters the environment, mark '
338341
'the test as failed')

‎Lib/test/libregrtest/main.py‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
from .runtests import RunTests, HuntRefleak
1919
from .setup import setup_process, setup_test_dir
2020
from .single import run_single_test, PROGRESS_MIN_TIME
21+
from .tsan import setup_tsan_tests
2122
from .utils import (
2223
StrPath, StrJSON, TestName, TestList, TestTuple, TestFilter,
2324
strip_py_suffix, count, format_duration,
@@ -56,6 +57,7 @@ def __init__(self, ns: Namespace, _add_python_opts: bool = False):
5657
self.quiet: bool = ns.quiet
5758
self.pgo: bool = ns.pgo
5859
self.pgo_extended: bool = ns.pgo_extended
60+
self.tsan: bool = ns.tsan
5961

6062
# Test results
6163
self.results: TestResults = TestResults()
@@ -87,7 +89,11 @@ def __init__(self, ns: Namespace, _add_python_opts: bool = False):
8789

8890
# Workers
8991
if ns.use_mp is None:
90-
num_workers = 0 # run sequentially
92+
if ns.tsan:
93+
# For TSAN tests, use number of CPU of the current process.
94+
num_workers = os.process_cpu_count()
95+
else:
96+
num_workers = 0 # run sequentially
9197
elif ns.use_mp <= 0:
9298
num_workers = -1 # use the number of CPUs
9399
else:
@@ -183,6 +189,9 @@ def find_tests(self, tests: TestList | None = None) -> tuple[TestTuple, TestList
183189
# add default PGO tests if no tests are specified
184190
setup_pgo_tests(self.cmdline_args, self.pgo_extended)
185191

192+
if self.tsan:
193+
setup_tsan_tests(self.cmdline_args)
194+
186195
exclude_tests = set()
187196
if self.exclude:
188197
for arg in self.cmdline_args:

‎Lib/test/libregrtest/tsan.py‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Set of tests run by default if --tsan is specified. The tests below were
2+
# chosen to run a thread sanitizer in a reasonable executable time.
3+
4+
TSAN_TESTS = [
5+
'test_asyncio',
6+
'test_capi',
7+
'test_code',
8+
'test_compilerall',
9+
'test_concurrent_futures',
10+
'test_enum',
11+
'test_fork1',
12+
'test_functools',
13+
'test_httpservers',
14+
'test_imaplib',
15+
'test_import',
16+
'test_importlib',
17+
'test_io',
18+
'test_logging',
19+
'test_multiprocessing_forkserver',
20+
'test_multiprocessing_spawn',
21+
'test_pickle',
22+
'test_queue',
23+
'test_list',
24+
'test_dict',
25+
'test_set',
26+
'test_tuple',
27+
'test_smtpnet',
28+
'test_socketserver',
29+
'test_ssl',
30+
'test_syslog',
31+
'test_thread',
32+
'test_threadedtempfile',
33+
'test_threading',
34+
'test_threading_local',
35+
'test_threadsignals',
36+
'test_urllib2net',
37+
'test_weakref'
38+
]
39+
40+
41+
def setup_tsan_tests(cmdline_args):
42+
if not cmdline_args:
43+
cmdline_args[:] = TSAN_TESTS[:]

‎Tools/tsan/supressions.txt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## reference: https://github.com/google/sanitizers/wiki/ThreadSanitizerSuppressions
2+
race:get_allocator_unlocked
3+
race:set_allocator_unlocked

0 commit comments

Comments
 (0)