summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBenjamin Schubert <contact@benschubert.me>2020-08-29 09:36:16 +0000
committerBenjamin Schubert <contact@benschubert.me>2020-08-30 14:29:27 +0000
commit30f8f2d77daacfd81aa0e82502dbb1e268761fb5 (patch)
tree2347727fefc2111688f5401f60bfa862c244c33c
parenta23223d1527b2342020774e19a62a7b595c606f1 (diff)
downloadbuildstream-30f8f2d77daacfd81aa0e82502dbb1e268761fb5.tar.gz
utils.py: Don't block on the call's `communicate` call
This ensures that, if we were to receive signals or other things while we are on this blocking call, we would be able to process them instead of waiting for the end of the process
-rw-r--r--src/buildstream/utils.py16
1 files changed, 15 insertions, 1 deletions
diff --git a/src/buildstream/utils.py b/src/buildstream/utils.py
index 438543b83..e9f06aad6 100644
--- a/src/buildstream/utils.py
+++ b/src/buildstream/utils.py
@@ -32,6 +32,7 @@ import signal
import stat
from stat import S_ISDIR
import subprocess
+from subprocess import TimeoutExpired
import tempfile
import time
import datetime
@@ -1402,7 +1403,20 @@ def _call(*popenargs, terminate=False, **kwargs):
process = subprocess.Popen( # pylint: disable=subprocess-popen-preexec-fn
*popenargs, preexec_fn=preexec_fn, universal_newlines=True, **kwargs
)
- output, _ = process.communicate()
+ # Here, we don't use `process.communicate()` directly without a timeout
+ # This is because, if we were to do that, and the process would never
+ # output anything, the control would never be given back to the python
+ # process, which might thus not be able to check for request to
+ # shutdown, or kill the process.
+ # We therefore loop with a timeout, to ensure the python process
+ # can act if it needs.
+ while True:
+ try:
+ output, _ = process.communicate(timeout=1)
+ break
+ except TimeoutExpired:
+ continue
+
exit_code = process.poll()
return (exit_code, output)