Преглед изворни кода

fix(gitzones): avoid deadlock when waiting for git diff

Using pipes for stdout, stderr can cause deadlocks, see
https://docs.python.org/3/library/subprocess.html#subprocess.Popen.wait

Also, the old version used busy waiting. Solution used now is
recommended by Python docs.
Nils Wisiol пре 3 година
родитељ
комит
bbf966649b
1 измењених фајлова са 5 додато и 3 уклоњено
  1. 5 3
      api/desecapi/replication.py

+ 5 - 3
api/desecapi/replication.py

@@ -52,11 +52,13 @@ class Repository:
                 stdout=subprocess.PIPE,
                 stdout=subprocess.PIPE,
                 env={'HOME': '/'},  # Celery does not adjust $HOME when dropping privleges
                 env={'HOME': '/'},  # Celery does not adjust $HOME when dropping privleges
         ) as p:
         ) as p:
-            rcode = p.wait()
-            stderr = p.stderr.read()
-            stdout = p.stdout.read()
             try:
             try:
+                stdout, stderr = p.communicate(input=None, timeout=60)
+                rcode = p.returncode
                 stderr, stdout = stderr.decode(), stdout.decode()
                 stderr, stdout = stderr.decode(), stdout.decode()
+            except subprocess.TimeoutExpired:
+                p.kill()
+                raise
             except UnicodeDecodeError:
             except UnicodeDecodeError:
                 GitRepositoryException('git stdout or stderr was not valid unicode!',
                 GitRepositoryException('git stdout or stderr was not valid unicode!',
                                        cmd=cmd, rcode=rcode, stderr=stderr, stdout=stdout)
                                        cmd=cmd, rcode=rcode, stderr=stderr, stdout=stdout)