diff options
author | Jürg Billeter <j@bitron.ch> | 2019-09-04 08:15:30 +0200 |
---|---|---|
committer | Jürg Billeter <j@bitron.ch> | 2019-09-04 08:16:43 +0200 |
commit | 42b1f166794a161cf4c84166e256f47a11bb6ca5 (patch) | |
tree | b0c7d430403b204af575f05996c40d14831046fa | |
parent | 1568268d749bc5ce14a418e5d2d45d621ec84e1d (diff) | |
download | buildstream-juerg/test-grpc.tar.gz |
utils.py: Wait for threads to exit in _is_single_threaded()juerg/test-grpc
gRPC threads are not joined when shut down.
-rw-r--r-- | src/buildstream/utils.py | 19 |
1 files changed, 16 insertions, 3 deletions
diff --git a/src/buildstream/utils.py b/src/buildstream/utils.py index 872b5bd59..fdd6987ce 100644 --- a/src/buildstream/utils.py +++ b/src/buildstream/utils.py @@ -32,6 +32,7 @@ import stat from stat import S_ISDIR import subprocess import tempfile +import time import itertools from contextlib import contextmanager from pathlib import Path @@ -61,6 +62,9 @@ _MAIN_PID = os.getpid() # This is 1 except for certain test environments (xdist/execnet). _INITIAL_NUM_THREADS_IN_MAIN_PROCESS = 1 +# Number of seconds to wait for background threads to exit. +_AWAIT_THREADS_TIMEOUT_SECONDS = 5 + class UtilError(BstError): """Raised by utility functions when system calls fail. @@ -1406,8 +1410,17 @@ def _get_compression(tar): def _is_single_threaded(): # Use psutil as threading.active_count() doesn't include gRPC threads. process = psutil.Process() - num_threads = process.num_threads() + if process.pid == _MAIN_PID: - return num_threads == _INITIAL_NUM_THREADS_IN_MAIN_PROCESS + expected_num_threads = _INITIAL_NUM_THREADS_IN_MAIN_PROCESS else: - return num_threads == 1 + expected_num_threads = 1 + + # gRPC threads are not joined when shut down. Wait for them to exit. + end_time = time.time() + _AWAIT_THREADS_TIMEOUT_SECONDS + while True: + if process.num_threads() == expected_num_threads: + return True + if time.time() >= end_time: + return False + time.sleep(0.1) |