| 1 |
# It's most useful to run these tests with ThreadSanitizer enabled. |
| 2 |
import sys |
| 3 |
import functools |
| 4 |
import threading |
| 5 |
import time |
| 6 |
import unittest |
| 7 |
import _testinternalcapi |
| 8 |
import warnings |
| 9 |
|
| 10 |
from test.support import threading_helper |
| 11 |
|
| 12 |
|
| 13 |
class TestBase(unittest.TestCase): |
| 14 |
pass |
| 15 |
|
| 16 |
|
| 17 |
def do_race(func1, func2): |
| 18 |
"""Run func1() and func2() repeatedly in separate threads.""" |
| 19 |
n = 1000 |
| 20 |
|
| 21 |
barrier = threading.Barrier(2) |
| 22 |
|
| 23 |
def repeat(func): |
| 24 |
barrier.wait() |
| 25 |
for _i in range(n): |
| 26 |
func() |
| 27 |
|
| 28 |
threads = [ |
| 29 |
threading.Thread(target=functools.partial(repeat, func1)), |
| 30 |
threading.Thread(target=functools.partial(repeat, func2)), |
| 31 |
] |
| 32 |
for thread in threads: |
| 33 |
thread.start() |
| 34 |
for thread in threads: |
| 35 |
thread.join() |
| 36 |
|
| 37 |
|
| 38 |
@threading_helper.requires_working_threading() |
| 39 |
class TestRaces(TestBase): |
| 40 |
def test_racing_cell_set(self): |
| 41 |
"""Test cell object gettr/settr properties.""" |
| 42 |
|
| 43 |
def nested_func(): |
| 44 |
x = 0 |
| 45 |
|
| 46 |
def inner(): |
| 47 |
nonlocal x |
| 48 |
x += 1 |
| 49 |
|
| 50 |
# This doesn't race because LOAD_DEREF and STORE_DEREF on the |
| 51 |
# cell object use critical sections. |
| 52 |
do_race(nested_func, nested_func) |
| 53 |
|
| 54 |
def nested_func2(): |
| 55 |
x = 0 |
| 56 |
|
| 57 |
def inner(): |
| 58 |
y = x |
| 59 |
frame = sys._getframe(1) |
| 60 |
frame.f_locals["x"] = 2 |
| 61 |
|
| 62 |
return inner |
| 63 |
|
| 64 |
def mutate_func2(): |
| 65 |
inner = nested_func2() |
| 66 |
cell = inner.__closure__[0] |
| 67 |
old_value = cell.cell_contents |
| 68 |
cell.cell_contents = 1000 |
| 69 |
time.sleep(0) |
| 70 |
cell.cell_contents = old_value |
| 71 |
time.sleep(0) |
| 72 |
|
| 73 |
# This revealed a race with cell_set_contents() since it was missing |
| 74 |
# the critical section. |
| 75 |
do_race(nested_func2, mutate_func2) |
| 76 |
|
| 77 |
def test_racing_cell_cmp_repr(self): |
| 78 |
"""Test cell object compare and repr methods.""" |
| 79 |
|
| 80 |
def nested_func(): |
| 81 |
x = 0 |
| 82 |
y = 0 |
| 83 |
|
| 84 |
def inner(): |
| 85 |
return x + y |
| 86 |
|
| 87 |
return inner.__closure__ |
| 88 |
|
| 89 |
cell_a, cell_b = nested_func() |
| 90 |
|
| 91 |
def mutate(): |
| 92 |
cell_a.cell_contents += 1 |
| 93 |
|
| 94 |
def access(): |
| 95 |
cell_a == cell_b |
| 96 |
s = repr(cell_a) |
| 97 |
|
| 98 |
# cell_richcompare() and cell_repr used to have data races |
| 99 |
do_race(mutate, access) |
| 100 |
|
| 101 |
def test_racing_load_super_attr(self): |
| 102 |
"""Test (un)specialization of LOAD_SUPER_ATTR opcode.""" |
| 103 |
|
| 104 |
class C: |
| 105 |
def __init__(self): |
| 106 |
try: |
| 107 |
super().__init__ |
| 108 |
super().__init__() |
| 109 |
except RuntimeError: |
| 110 |
pass # happens if __class__ is replaced with non-type |
| 111 |
|
| 112 |
def access(): |
| 113 |
C() |
| 114 |
|
| 115 |
def mutate(): |
| 116 |
# Swap out the super() global with a different one |
| 117 |
real_super = super |
| 118 |
globals()["super"] = lambda s=1: s |
| 119 |
time.sleep(0) |
| 120 |
globals()["super"] = real_super |
| 121 |
time.sleep(0) |
| 122 |
# Swap out the __class__ closure value with a non-type |
| 123 |
cell = C.__init__.__closure__[0] |
| 124 |
real_class = cell.cell_contents |
| 125 |
cell.cell_contents = 99 |
| 126 |
time.sleep(0) |
| 127 |
cell.cell_contents = real_class |
| 128 |
|
| 129 |
# The initial PR adding specialized opcodes for LOAD_SUPER_ATTR |
| 130 |
# had some races (one with the super() global changing and one |
| 131 |
# with the cell binding being changed). |
| 132 |
do_race(access, mutate) |
| 133 |
|
| 134 |
def test_racing_to_bool(self): |
| 135 |
|
| 136 |
seq = [1] |
| 137 |
|
| 138 |
class C: |
| 139 |
def __bool__(self): |
| 140 |
return False |
| 141 |
|
| 142 |
def access(): |
| 143 |
if seq: |
| 144 |
return 1 |
| 145 |
else: |
| 146 |
return 2 |
| 147 |
|
| 148 |
def mutate(): |
| 149 |
nonlocal seq |
| 150 |
seq = [1] |
| 151 |
time.sleep(0) |
| 152 |
seq = C() |
| 153 |
time.sleep(0) |
| 154 |
|
| 155 |
do_race(access, mutate) |
| 156 |
|
| 157 |
def test_racing_store_attr_slot(self): |
| 158 |
class C: |
| 159 |
__slots__ = ['x', '__dict__'] |
| 160 |
|
| 161 |
c = C() |
| 162 |
|
| 163 |
def set_slot(): |
| 164 |
for i in range(10): |
| 165 |
c.x = i |
| 166 |
time.sleep(0) |
| 167 |
|
| 168 |
def change_type(): |
| 169 |
def set_x(self, x): |
| 170 |
pass |
| 171 |
|
| 172 |
def get_x(self): |
| 173 |
pass |
| 174 |
|
| 175 |
C.x = property(get_x, set_x) |
| 176 |
time.sleep(0) |
| 177 |
del C.x |
| 178 |
time.sleep(0) |
| 179 |
|
| 180 |
do_race(set_slot, change_type) |
| 181 |
|
| 182 |
def set_getattribute(): |
| 183 |
C.__getattribute__ = lambda self, x: x |
| 184 |
time.sleep(0) |
| 185 |
del C.__getattribute__ |
| 186 |
time.sleep(0) |
| 187 |
|
| 188 |
do_race(set_slot, set_getattribute) |
| 189 |
|
| 190 |
def test_racing_store_attr_instance_value(self): |
| 191 |
class C: |
| 192 |
pass |
| 193 |
|
| 194 |
c = C() |
| 195 |
|
| 196 |
def set_value(): |
| 197 |
for i in range(100): |
| 198 |
c.x = i |
| 199 |
|
| 200 |
set_value() |
| 201 |
|
| 202 |
def read(): |
| 203 |
x = c.x |
| 204 |
|
| 205 |
def mutate(): |
| 206 |
# Adding a property for 'x' should unspecialize it. |
| 207 |
C.x = property(lambda self: None, lambda self, x: None) |
| 208 |
time.sleep(0) |
| 209 |
del C.x |
| 210 |
time.sleep(0) |
| 211 |
|
| 212 |
do_race(read, mutate) |
| 213 |
|
| 214 |
def test_racing_store_attr_with_hint(self): |
| 215 |
class C: |
| 216 |
pass |
| 217 |
|
| 218 |
c = C() |
| 219 |
for i in range(29): |
| 220 |
setattr(c, f"_{i}", None) |
| 221 |
|
| 222 |
def set_value(): |
| 223 |
for i in range(100): |
| 224 |
c.x = i |
| 225 |
|
| 226 |
set_value() |
| 227 |
|
| 228 |
def read(): |
| 229 |
x = c.x |
| 230 |
|
| 231 |
def mutate(): |
| 232 |
# Adding a property for 'x' should unspecialize it. |
| 233 |
C.x = property(lambda self: None, lambda self, x: None) |
| 234 |
time.sleep(0) |
| 235 |
del C.x |
| 236 |
time.sleep(0) |
| 237 |
|
| 238 |
do_race(read, mutate) |
| 239 |
|
| 240 |
def make_shared_key_dict(self): |
| 241 |
class C: |
| 242 |
pass |
| 243 |
|
| 244 |
a = C() |
| 245 |
a.x = 1 |
| 246 |
return a.__dict__ |
| 247 |
|
| 248 |
def test_racing_store_attr_dict(self): |
| 249 |
"""Test STORE_ATTR with various dictionary types.""" |
| 250 |
class C: |
| 251 |
pass |
| 252 |
|
| 253 |
c = C() |
| 254 |
|
| 255 |
def set_value(): |
| 256 |
for i in range(20): |
| 257 |
c.x = i |
| 258 |
|
| 259 |
def mutate(): |
| 260 |
nonlocal c |
| 261 |
c.x = 1 |
| 262 |
self.assertTrue(_testinternalcapi.has_inline_values(c)) |
| 263 |
for i in range(30): |
| 264 |
setattr(c, f"_{i}", None) |
| 265 |
self.assertFalse(_testinternalcapi.has_inline_values(c.__dict__)) |
| 266 |
c.__dict__ = self.make_shared_key_dict() |
| 267 |
self.assertTrue(_testinternalcapi.has_split_table(c.__dict__)) |
| 268 |
c.__dict__[1] = None |
| 269 |
self.assertFalse(_testinternalcapi.has_split_table(c.__dict__)) |
| 270 |
c = C() |
| 271 |
|
| 272 |
do_race(set_value, mutate) |
| 273 |
|
| 274 |
def test_racing_recursion_limit(self): |
| 275 |
def something_recursive(): |
| 276 |
def count(n): |
| 277 |
if n > 0: |
| 278 |
return count(n - 1) + 1 |
| 279 |
return 0 |
| 280 |
|
| 281 |
count(50) |
| 282 |
|
| 283 |
def set_recursion_limit(): |
| 284 |
for limit in range(100, 200): |
| 285 |
sys.setrecursionlimit(limit) |
| 286 |
|
| 287 |
do_race(something_recursive, set_recursion_limit) |
| 288 |
|
| 289 |
|
| 290 |
@threading_helper.requires_working_threading() |
| 291 |
class TestWarningsRaces(TestBase): |
| 292 |
def setUp(self): |
| 293 |
self.saved_filters = warnings.filters[:] |
| 294 |
warnings.resetwarnings() |
| 295 |
# Add multiple filters to the list to increase odds of race. |
| 296 |
for lineno in range(20): |
| 297 |
warnings.filterwarnings('ignore', message='not matched', category=Warning, lineno=lineno) |
| 298 |
# Override showwarning() so that we don't actually show warnings. |
| 299 |
def showwarning(*args): |
| 300 |
pass |
| 301 |
warnings.showwarning = showwarning |
| 302 |
|
| 303 |
def tearDown(self): |
| 304 |
warnings.filters[:] = self.saved_filters |
| 305 |
warnings.showwarning = warnings._showwarning_orig |
| 306 |
|
| 307 |
def test_racing_warnings_filter(self): |
| 308 |
# Modifying the warnings.filters list while another thread is using |
| 309 |
# warn() should not crash or race. |
| 310 |
def modify_filters(): |
| 311 |
time.sleep(0) |
| 312 |
warnings.filters[:] = [('ignore', None, UserWarning, None, 0)] |
| 313 |
time.sleep(0) |
| 314 |
warnings.filters[:] = self.saved_filters |
| 315 |
|
| 316 |
def emit_warning(): |
| 317 |
warnings.warn('dummy message', category=UserWarning) |
| 318 |
|
| 319 |
do_race(modify_filters, emit_warning) |
| 320 |
|
| 321 |
|
| 322 |
if __name__ == "__main__": |
| 323 |
unittest.main() |