-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_resize_race.py
More file actions
executable file
·493 lines (398 loc) · 13.6 KB
/
test_resize_race.py
File metadata and controls
executable file
·493 lines (398 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
#!/usr/bin/env python3
"""
Targeted tests to narrow down the resize race condition.
The lock-free implementation does this during resize:
1. FT_ATOMIC_STORE_PTR_RELEASE(so->table, NULL)
2. FT_ATOMIC_STORE_SSIZE_RELAXED(so->mask, newsize - 1)
3. ... copy entries ...
4. FT_ATOMIC_STORE_PTR_RELEASE(so->table, newtable)
And set_lookkey_threadsafe does:
1. table = FT_ATOMIC_LOAD_PTR_ACQUIRE(so->table)
2. mask = FT_ATOMIC_LOAD_SSIZE_RELAXED(so->mask)
3. if (table == NULL || table != so->table) -> fallback
Potential issues:
- mask could be read after table, getting new mask with old table
- QSBR cleanup might free table while reader is using it
- set_empty_to_minsize also sets table=NULL temporarily
"""
import argparse
import sys
import threading
import time
from dataclasses import dataclass
@dataclass
class Stats:
ops: int = 0
errors: int = 0
false_negatives: int = 0 # Key should be present but wasn't found
false_positives: int = 0 # Key shouldn't be present but was found
def test_resize_during_contains():
"""
Test 1: Single writer grows set, readers check contains.
No clear() - just growth causing resizes.
"""
print("\n=== Test 1: Resize during contains (grow only) ===")
s = set()
stop = threading.Event()
barrier = threading.Barrier(5)
reader_stats = [Stats() for _ in range(4)]
writer_stats = Stats()
# Track what values have been added (for verification)
added_values = set()
added_lock = threading.Lock()
def reader(tid):
stats = reader_stats[tid]
barrier.wait()
while not stop.is_set():
# Check for values we know have been added
with added_lock:
check_values = list(added_values)[:100] # Sample
for v in check_values:
result = v in s
stats.ops += 1
# Value was added, should be found (unless cleared)
if not result:
stats.false_negatives += 1
def writer():
nonlocal added_values
barrier.wait()
val = 0
while not stop.is_set():
s.add(val)
with added_lock:
added_values.add(val)
writer_stats.ops += 1
val += 1
# Don't clear - just grow to trigger resizes
threads = [threading.Thread(target=reader, args=(i,)) for i in range(4)]
threads.append(threading.Thread(target=writer))
for t in threads:
t.start()
time.sleep(5)
stop.set()
for t in threads:
t.join(timeout=2)
total_reader_ops = sum(st.ops for st in reader_stats)
total_false_neg = sum(st.false_negatives for st in reader_stats)
print(f" Writer ops: {writer_stats.ops:,}")
print(f" Reader ops: {total_reader_ops:,}")
print(f" False negatives: {total_false_neg}")
print(f" Final set size: {len(s)}")
# Check final state
missing = added_values - s
if missing:
print(f" ERROR: {len(missing)} values missing from final set!")
return False
return total_false_neg == 0
def test_clear_during_contains():
"""
Test 2: Writer clears set, readers check contains.
Tests set_empty_to_minsize which also sets table=NULL.
"""
print("\n=== Test 2: Clear during contains ===")
s = set()
stop = threading.Event()
barrier = threading.Barrier(5)
reader_stats = [Stats() for _ in range(4)]
writer_stats = Stats()
def reader(tid):
stats = reader_stats[tid]
barrier.wait()
while not stop.is_set():
for i in range(100):
_ = i in s
stats.ops += 1
def writer():
barrier.wait()
while not stop.is_set():
# Add values then clear
for i in range(50):
s.add(i)
writer_stats.ops += 1
s.clear()
writer_stats.ops += 1
threads = [threading.Thread(target=reader, args=(i,)) for i in range(4)]
threads.append(threading.Thread(target=writer))
for t in threads:
t.start()
time.sleep(5)
stop.set()
for t in threads:
t.join(timeout=2)
total_reader_ops = sum(st.ops for st in reader_stats)
total_errors = sum(st.errors for st in reader_stats)
print(f" Writer ops: {writer_stats.ops:,}")
print(f" Reader ops: {total_reader_ops:,}")
print(f" Errors: {total_errors}")
return total_errors == 0
def test_threshold_crossing():
"""
Test 3: Repeatedly cross the PySet_MINSIZE (8) threshold.
This switches between smalltable and malloced table.
"""
print("\n=== Test 3: Threshold crossing (smalltable <-> malloc) ===")
s = set()
stop = threading.Event()
barrier = threading.Barrier(5)
reader_stats = [Stats() for _ in range(4)]
writer_stats = Stats()
errors = []
error_lock = threading.Lock()
def reader(tid):
stats = reader_stats[tid]
barrier.wait()
while not stop.is_set():
try:
for i in range(20):
_ = i in s
stats.ops += 1
except Exception as e:
with error_lock:
errors.append(f"Reader {tid}: {e}")
stats.errors += 1
def writer():
barrier.wait()
while not stop.is_set():
try:
# Grow past threshold (8)
for i in range(12):
s.add(i)
writer_stats.ops += 1
# Shrink below threshold using pop
while len(s) > 4:
s.pop()
writer_stats.ops += 1
except Exception as e:
with error_lock:
errors.append(f"Writer: {e}")
threads = [threading.Thread(target=reader, args=(i,)) for i in range(4)]
threads.append(threading.Thread(target=writer))
for t in threads:
t.start()
time.sleep(5)
stop.set()
for t in threads:
t.join(timeout=2)
total_reader_ops = sum(st.ops for st in reader_stats)
print(f" Writer ops: {writer_stats.ops:,}")
print(f" Reader ops: {total_reader_ops:,}")
print(f" Errors: {len(errors)}")
if errors:
for e in errors[:5]:
print(f" {e}")
return len(errors) == 0
def test_multiple_writers_clear():
"""
Test 4: Multiple writers all clearing - high contention on resize.
"""
print("\n=== Test 4: Multiple writers with clear ===")
s = set()
stop = threading.Event()
barrier = threading.Barrier(6) # 4 readers + 2 writers
reader_stats = [Stats() for _ in range(4)]
writer_stats = [Stats() for _ in range(2)]
errors = []
error_lock = threading.Lock()
def reader(tid):
stats = reader_stats[tid]
barrier.wait()
while not stop.is_set():
try:
for i in range(50):
_ = i in s
stats.ops += 1
except Exception as e:
with error_lock:
errors.append(f"Reader {tid}: {e}")
stats.errors += 1
def writer(tid):
stats = writer_stats[tid]
barrier.wait()
base = tid * 100
while not stop.is_set():
try:
for i in range(20):
s.add(base + i)
stats.ops += 1
s.clear()
stats.ops += 1
except Exception as e:
with error_lock:
errors.append(f"Writer {tid}: {e}")
threads = [threading.Thread(target=reader, args=(i,)) for i in range(4)]
threads.extend(
[threading.Thread(target=writer, args=(i,)) for i in range(2)]
)
for t in threads:
t.start()
time.sleep(5)
stop.set()
for t in threads:
t.join(timeout=2)
total_reader_ops = sum(st.ops for st in reader_stats)
total_writer_ops = sum(st.ops for st in writer_stats)
print(f" Writer ops: {total_writer_ops:,}")
print(f" Reader ops: {total_reader_ops:,}")
print(f" Errors: {len(errors)}")
if errors:
for e in errors[:5]:
print(f" {e}")
return len(errors) == 0
def test_rapid_resize():
"""
Test 5: Maximize resize frequency by using update() and clear().
"""
print("\n=== Test 5: Rapid resize with update/clear ===")
s = set()
stop = threading.Event()
barrier = threading.Barrier(5)
reader_stats = [Stats() for _ in range(4)]
writer_stats = Stats()
errors = []
error_lock = threading.Lock()
def reader(tid):
stats = reader_stats[tid]
barrier.wait()
while not stop.is_set():
try:
# Tight loop of contains
for i in range(100):
_ = i in s
stats.ops += 1
except Exception as e:
with error_lock:
errors.append(f"Reader {tid}: {e}")
stats.errors += 1
def writer():
barrier.wait()
while not stop.is_set():
try:
# update() causes resize if needed
s.update(range(100))
writer_stats.ops += 1
s.clear()
writer_stats.ops += 1
except Exception as e:
with error_lock:
errors.append(f"Writer: {e}")
threads = [threading.Thread(target=reader, args=(i,)) for i in range(4)]
threads.append(threading.Thread(target=writer))
for t in threads:
t.start()
time.sleep(5)
stop.set()
for t in threads:
t.join(timeout=2)
total_reader_ops = sum(st.ops for st in reader_stats)
print(f" Writer ops: {writer_stats.ops:,}")
print(f" Reader ops: {total_reader_ops:,}")
print(f" Errors: {len(errors)}")
if errors:
for e in errors[:5]:
print(f" {e}")
return len(errors) == 0
def test_mask_table_ordering():
"""
Test 6: Try to catch mask/table ordering issue.
The concern: reader loads table, then mask. If resize happens between,
reader has old table pointer but new mask value.
In the patch:
- Resize stores: table=NULL, then mask=new, then table=new
- Reader loads: table, then mask, then checks table again
The second table check should catch this, but let's stress it.
"""
print("\n=== Test 6: Mask/table ordering stress ===")
s = set()
stop = threading.Event()
barrier = threading.Barrier(9) # 8 readers + 1 writer
reader_stats = [Stats() for _ in range(8)]
writer_stats = Stats()
errors = []
error_lock = threading.Lock()
def reader(tid):
stats = reader_stats[tid]
barrier.wait()
while not stop.is_set():
try:
# Very tight loop
for i in range(1000):
_ = i % 50 in s
stats.ops += 1
except Exception as e:
with error_lock:
errors.append(f"Reader {tid}: {e}")
stats.errors += 1
def writer():
barrier.wait()
size = 10
while not stop.is_set():
try:
# Alternate between small and large to force resize
s.clear()
s.update(range(size))
writer_stats.ops += 2
# Vary size to hit different resize points
size = 10 if size > 50 else size + 10
except Exception as e:
with error_lock:
errors.append(f"Writer: {e}")
threads = [threading.Thread(target=reader, args=(i,)) for i in range(8)]
threads.append(threading.Thread(target=writer))
for t in threads:
t.start()
time.sleep(5)
stop.set()
for t in threads:
t.join(timeout=2)
total_reader_ops = sum(st.ops for st in reader_stats)
print(f" Writer ops: {writer_stats.ops:,}")
print(f" Reader ops: {total_reader_ops:,}")
print(f" Errors: {len(errors)}")
if errors:
for e in errors[:5]:
print(f" {e}")
return len(errors) == 0
def main():
parser = argparse.ArgumentParser(description='Targeted resize race tests')
parser.add_argument('--test', type=int, help='Run specific test (1-6)')
args = parser.parse_args()
tests = [
("Resize during contains (grow)", test_resize_during_contains),
("Clear during contains", test_clear_during_contains),
("Threshold crossing", test_threshold_crossing),
("Multiple writers clear", test_multiple_writers_clear),
("Rapid resize", test_rapid_resize),
("Mask/table ordering", test_mask_table_ordering),
]
if args.test:
if 1 <= args.test <= len(tests):
name, func = tests[args.test - 1]
result = func()
print(f"\n{'PASSED' if result else 'FAILED'}: {name}")
sys.exit(0 if result else 1)
else:
print(f"Invalid test number. Choose 1-{len(tests)}")
sys.exit(1)
# Run all tests
results = []
for name, func in tests:
try:
result = func()
results.append((name, result))
except Exception as e:
print(f" EXCEPTION: {e}")
results.append((name, False))
print("\n" + "=" * 50)
print("SUMMARY")
print("=" * 50)
all_passed = True
for name, passed in results:
status = "PASSED" if passed else "FAILED"
print(f" {status}: {name}")
if not passed:
all_passed = False
print("=" * 50)
print(f"OVERALL: {'SUCCESS' if all_passed else 'FAILURE'}")
sys.exit(0 if all_passed else 1)
if __name__ == '__main__':
main()