From eba3bfc57e1a12e820321eafad1695e79d4c7ef5 Mon Sep 17 00:00:00 2001 From: Joonas Koivunen Date: Thu, 30 Nov 2023 17:48:40 +0200 Subject: [PATCH] test: python needs thread safety as well (#5992) we have test cases which launch processes from threads, and they capture output assuming this counter is thread-safe. at least according to my understanding this operation in python requires a lock to be thread-safe. --- test_runner/fixtures/utils.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/test_runner/fixtures/utils.py b/test_runner/fixtures/utils.py index 6e857766e5..1ec18b9f74 100644 --- a/test_runner/fixtures/utils.py +++ b/test_runner/fixtures/utils.py @@ -138,17 +138,19 @@ def subprocess_capture( _global_counter = 0 +_global_counter_lock = threading.Lock() def global_counter() -> int: - """A really dumb global counter. + """A really dumb but thread-safe global counter. This is useful for giving output files a unique number, so if we run the same command multiple times we can keep their output separate. """ - global _global_counter - _global_counter += 1 - return _global_counter + global _global_counter, _global_counter_lock + with _global_counter_lock: + _global_counter += 1 + return _global_counter def print_gc_result(row: Dict[str, Any]):