Skip to content

Commit b6040a5

Browse files
Dana PowersreAsOn2010
authored andcommitted
Respawn crashed services in test/service.py, rather than raise RuntimeError
1 parent e367731 commit b6040a5

File tree

1 file changed

+26
-12
lines changed

1 file changed

+26
-12
lines changed

test/service.py

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,37 @@ def __init__(self, args=None, env=None):
4040
self.captured_stderr = []
4141

4242
self.should_die = threading.Event()
43+
self.child = None
44+
self.alive = False
4345

4446
def run(self):
4547
self.run_with_handles()
4648

47-
def run_with_handles(self):
49+
def _spawn(self):
50+
if self.alive: return
51+
if self.child and self.child.poll() is None: return
52+
4853
self.child = subprocess.Popen(
4954
self.args,
5055
env=self.env,
5156
bufsize=1,
5257
stdout=subprocess.PIPE,
5358
stderr=subprocess.PIPE)
54-
alive = True
59+
self.alive = True
60+
61+
def _despawn(self):
62+
self.child.terminate()
63+
self.alive = False
64+
for _ in range(50):
65+
if self.child.poll() is not None:
66+
self.child = None
67+
break
68+
time.sleep(0.1)
69+
else:
70+
self.child.kill()
5571

72+
def run_with_handles(self):
73+
self._spawn()
5674
while True:
5775
(rds, _, _) = select.select([self.child.stdout, self.child.stderr], [], [], 1)
5876

@@ -64,17 +82,13 @@ def run_with_handles(self):
6482
line = self.child.stderr.readline()
6583
self.captured_stderr.append(line.decode('utf-8'))
6684

85+
if self.child.poll() is not None:
86+
self.dump_logs()
87+
self._spawn()
88+
6789
if self.should_die.is_set():
68-
self.child.terminate()
69-
alive = False
70-
71-
poll_results = self.child.poll()
72-
if poll_results is not None:
73-
if not alive:
74-
break
75-
else:
76-
self.dump_logs()
77-
raise RuntimeError("Subprocess has died. Aborting. (args=%s)" % ' '.join(str(x) for x in self.args))
90+
self._despawn()
91+
break
7892

7993
def dump_logs(self):
8094
log.critical('stderr')

0 commit comments

Comments
 (0)