From ce3971093d2992ec26584e09655ac070ae5972b5 Mon Sep 17 00:00:00 2001 From: Kay0u Date: Mon, 25 Jan 2021 18:53:33 +0100 Subject: [PATCH] call_async_output: we can't use stdinfo in kwargs, close stdout, stderr and stdinfo if subprocess.Popen raise an exception --- moulinette/utils/process.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/moulinette/utils/process.py b/moulinette/utils/process.py index e4134498..e49d0d3d 100644 --- a/moulinette/utils/process.py +++ b/moulinette/utils/process.py @@ -59,7 +59,7 @@ def call_async_output(args, callback, **kwargs): Exit status of the command """ - for a in ["stdout", "stderr"]: + for a in ["stdout", "stderr", "stdinfo"]: if a in kwargs: raise ValueError("%s argument not allowed, " "it will be overridden." % a) @@ -72,11 +72,18 @@ def call_async_output(args, callback, **kwargs): kwargs["env"] = os.environ kwargs["env"]["YNH_STDINFO"] = str(stdinfo.fdWrite) - with subprocess.Popen(args, **kwargs) as p: + try: + with subprocess.Popen(args, **kwargs) as p: + kwargs["stdout"].close() + kwargs["stderr"].close() + if stdinfo: + stdinfo.close() + except TypeError: kwargs["stdout"].close() kwargs["stderr"].close() if stdinfo: stdinfo.close() + raise # on slow hardware, in very edgy situations it is possible that the process # isn't finished just after having closed stdout and stderr, so we wait a