From 75e7e046950464a9dfc1afafeb86fc0b1afe6ce2 Mon Sep 17 00:00:00 2001 From: Stan Lagun Date: Tue, 22 Mar 2016 16:47:49 +0300 Subject: [PATCH] Restores SIGPIPE on script processes There is an issue https://bugs.python.org/issue1652 in Python 2.7 which was resolved in Py3. By default Python sets SIGPIPE to SIG_IGN on startup, because it prefers to check every write and raise an IOError exception rather than taking SIGPIPE. This is all well and good for Python itself. However, non-Python Unix subprocesses generally expect to have SIGPIPE set to the default action, since that's what will happen if they're started from a normal shell. As a result scripts with piping that is terminated in reverse chain order are never exit. See more info here: http://www.enricozini.org/blog/2009/debian/python-pipes/ Closes-Bug: #1524347 Change-Id: Ia015e7fd46912f6a0be97a6b9c297f8d14fd3f7c --- muranoagent/executors/application/__init__.py | 20 ++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-) diff --git a/muranoagent/executors/application/__init__.py b/muranoagent/executors/application/__init__.py index 020c9371..64115779 100644 --- a/muranoagent/executors/application/__init__.py +++ b/muranoagent/executors/application/__init__.py @@ -14,6 +14,7 @@ # limitations under the License. import os +import signal import stat import subprocess import sys @@ -52,13 +53,18 @@ class ApplicationExecutor(object): script_name = os.path.relpath(self._path) LOG.debug("Starting '{0}' script execution".format(script_name)) - process = subprocess.Popen( - app, - stdout=stdout, - stderr=stderr, - universal_newlines=True, - cwd=dir_name, - shell=True) + popen_kwargs = { + 'stdout': stdout, + 'stderr': stderr, + 'universal_newlines': True, + 'cwd': dir_name, + 'shell': True + } + if os.name != 'nt': + popen_kwargs['preexec_fn'] = lambda: signal.signal( + signal.SIGPIPE, signal.SIG_DFL) + + process = subprocess.Popen(app, **popen_kwargs) stdout, stderr = process.communicate(input) retcode = process.poll() LOG.debug("Script {0} execution finished "