|
3 | 3 |
|
4 | 4 | import gc |
5 | 5 | import os, sys, errno |
| 6 | +import itertools |
6 | 7 | import threading |
7 | 8 | import unittest |
8 | 9 | from platform import machine, win32_edition |
@@ -322,42 +323,33 @@ def test_queryvalueex_race_condition(self): |
322 | 323 | # gh-142282: QueryValueEx could read garbage buffer under race |
323 | 324 | # condition when another thread changes the value size |
324 | 325 | done = False |
325 | | - error_found = None |
| 326 | + ready = threading.Event() |
326 | 327 | values = [b'ham', b'spam'] |
327 | 328 |
|
328 | 329 | class WriterThread(threading.Thread): |
329 | 330 | def run(self): |
330 | 331 | with CreateKey(HKEY_CURRENT_USER, test_key_name) as key: |
331 | | - use_first = True |
| 332 | + values_iter = itertools.cycle(values) |
332 | 333 | while not done: |
333 | | - val = values[0] if use_first else values[1] |
334 | | - use_first = not use_first |
| 334 | + val = next(values_iter) |
335 | 335 | SetValueEx(key, 'test_value', 0, REG_BINARY, val) |
| 336 | + ready.set() |
336 | 337 |
|
337 | 338 | thread = WriterThread() |
338 | 339 | thread.start() |
339 | 340 | try: |
| 341 | + ready.wait() |
340 | 342 | with CreateKey(HKEY_CURRENT_USER, test_key_name) as key: |
341 | 343 | for _ in range(1000): |
342 | | - try: |
343 | | - result, typ = QueryValueEx(key, 'test_value') |
344 | | - except FileNotFoundError: |
345 | | - # Value not yet created |
346 | | - continue |
| 344 | + result, typ = QueryValueEx(key, 'test_value') |
347 | 345 | # The result must be one of the written values, |
348 | 346 | # not garbage data from uninitialized buffer |
349 | | - if result not in values: |
350 | | - error_found = result |
351 | | - break |
| 347 | + self.assertIn(result, values, f"QueryValueEx returned unexpected value: {result!r}") |
352 | 348 | finally: |
353 | 349 | done = True |
354 | 350 | thread.join() |
355 | 351 | DeleteKey(HKEY_CURRENT_USER, test_key_name) |
356 | 352 |
|
357 | | - if error_found is not None: |
358 | | - self.fail(f"QueryValueEx returned unexpected value: {error_found!r}, " |
359 | | - f"expected one of {values}") |
360 | | - |
361 | 353 | def test_long_key(self): |
362 | 354 | # Issue2810, in 2.6 and 3.1 when the key name was exactly 256 |
363 | 355 | # characters, EnumKey raised "WindowsError: More data is |
|
0 commit comments