Skip to content

Commit 3d90c43

Browse files
Address the review
Name the lock files, so that it is clear what left them in the temporary directory, and say why they are left. Close the file descriptor whatever goes wrong. Call the resource of bigmem tests 'bigmem'. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
1 parent 730794c commit 3d90c43

1 file changed

Lines changed: 13 additions & 6 deletions

File tree

‎Lib/test/support/__init__.py‎

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,8 @@ def get_resource_value(resource):
339339

340340
# Resources which are machine-wide: two tests using one of them at the same
341341
# time interfere with each other.
342-
EXCLUSIVE_RESOURCES = frozenset({'audio', 'console', 'curses', 'gui'})
342+
EXCLUSIVE_RESOURCES = frozenset({'audio', 'bigmem', 'console', 'curses',
343+
'gui'})
343344

344345
# The environment variable naming the directory in which the test runner keeps
345346
# the lock files. It is set only when tests are run in parallel.
@@ -359,7 +360,7 @@ def _lock_file(fd):
359360
try:
360361
msvcrt.locking(fd, msvcrt.LK_NBLCK, 1)
361362
except OSError:
362-
# LK_LOCK gives up after ten seconds, so retry ourselves.
363+
# It returns at once if the file is locked, so wait ourselves.
363364
time.sleep(0.1)
364365
else:
365366
return
@@ -377,16 +378,22 @@ def _open_exclusive_lock(resource):
377378
if not directory:
378379
# The tests are not run in parallel, so there is nothing to exclude.
379380
return None
381+
# The file is left behind: another process may be waiting on a lock on it,
382+
# and it is empty anyway.
383+
path = os.path.join(directory, f'python-test-exclusive-{resource}.lock')
380384
try:
381-
fd = os.open(os.path.join(directory, f'exclusive-{resource}.lock'),
382-
os.O_WRONLY | os.O_CREAT, 0o600)
385+
fd = os.open(path, os.O_WRONLY | os.O_CREAT, 0o600)
383386
except OSError:
384387
return None
385388
try:
386389
_lock_file(fd)
387-
except OSError:
390+
except (ImportError, OSError):
391+
# Locking is not available here, so the tests are not serialized.
388392
os.close(fd)
389393
return None
394+
except BaseException:
395+
os.close(fd)
396+
raise
390397
return fd
391398

392399

@@ -1454,7 +1461,7 @@ def wrapper(self):
14541461
cls = type(self)
14551462
qualname = f'{cls.__qualname__}.{f.__name__}'
14561463
# A real run allocates most of the memory of the machine.
1457-
with _exclusive_resource('memory'):
1464+
with _exclusive_resource('bigmem'):
14581465
proc = isolation._start_test(cls.__module__, qualname)
14591466
watchdog = _memory_watchdog(proc.pid) if verbose else None
14601467
payload, output, returncode = proc.wait(tick=watchdog)

0 commit comments

Comments
 (0)