From e96c6ee5591d252f70abae3355a35b31651fe988 Mon Sep 17 00:00:00 2001 From: Richard Maw Date: Fri, 29 Aug 2014 17:48:58 +0000 Subject: Handle closing the ends of the log pipe we don't care about We only need the read end in the parent, and we only need the write end in the child, so we can close them pre-emptively. Also, we need to close the write end in the parent so that when the subprocess terminates we get EOF on the read end of the pipe. --- morphlib/plugins/deploy_plugin.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/morphlib/plugins/deploy_plugin.py b/morphlib/plugins/deploy_plugin.py index da72f91b..e4968736 100644 --- a/morphlib/plugins/deploy_plugin.py +++ b/morphlib/plugins/deploy_plugin.py @@ -570,14 +570,23 @@ class DeployPlugin(cliapp.Plugin): new_env = env.copy() new_env['MORPH_LOG_FD'] = str(log_write_fd) + # We lack python3's pass_fds to close any of the fds + # we don't care about, such as the read end of the log, + # so we have to close it ourselves here. + def close_read_end(): + os.close(log_read_fd) p = subprocess.Popen( [ext_filename] + args, cwd=gd.dirname, env=new_env, - stdout=subprocess.PIPE, stderr=subprocess.PIPE) + stdout=subprocess.PIPE, stderr=subprocess.PIPE, + preexec_fn=close_read_end) + os.close(log_write_fd) + log_write_fd = -1 self._watch_extension_subprocess(name, kind, p, log_read_fd) finally: os.close(log_read_fd) - os.close(log_write_fd) + if log_write_fd != -1: + os.close(log_write_fd) def _watch_extension_subprocess(self, name, kind, p, log_read_fd): '''Follow stdout, stderr and log output of an extension subprocess.''' -- cgit v1.2.1