Skip to content

Commit d7d89d6

Browse files
committed
fix: properly terminate and wait on subprocesses on teardown
1 parent 00d723b commit d7d89d6

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

cmdeploy/src/cmdeploy/tests/plugin.py

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -388,12 +388,15 @@ def cmfactory(rpc, gencreds, maildomain, chatmail_config):
388388

389389
@pytest.fixture
390390
def remote(sshdomain):
391-
return Remote(sshdomain)
391+
r = Remote(sshdomain)
392+
yield r
393+
r.close()
392394

393395

394396
class Remote:
395397
def __init__(self, sshdomain):
396398
self.sshdomain = sshdomain
399+
self._procs = []
397400

398401
def iter_output(self, logcmd="", ready=None):
399402
getjournal = "journalctl -f" if not logcmd else logcmd
@@ -403,19 +406,32 @@ def iter_output(self, logcmd="", ready=None):
403406
case "localhost": command = []
404407
case _: command = ["ssh", f"root@{self.sshdomain}"]
405408
[command.append(arg) for arg in getjournal.split()]
406-
self.popen = subprocess.Popen(
409+
popen = subprocess.Popen(
407410
command,
411+
stdin=subprocess.DEVNULL,
408412
stdout=subprocess.PIPE,
413+
stderr=subprocess.DEVNULL,
409414
)
410-
while 1:
411-
line = self.popen.stdout.readline()
412-
res = line.decode().strip().lower()
413-
if not res:
414-
break
415-
if ready is not None:
416-
ready()
417-
ready = None
418-
yield res
415+
self._procs.append(popen)
416+
try:
417+
while 1:
418+
line = popen.stdout.readline()
419+
res = line.decode().strip().lower()
420+
if not res:
421+
break
422+
if ready is not None:
423+
ready()
424+
ready = None
425+
yield res
426+
finally:
427+
popen.terminate()
428+
popen.wait()
429+
430+
def close(self):
431+
while self._procs:
432+
proc = self._procs.pop()
433+
proc.kill()
434+
proc.wait()
419435

420436

421437
@pytest.fixture

0 commit comments

Comments
 (0)