Skip to content

Commit

Permalink
[AIRFLOW-6397] ensure sub_process attribute exists before trying to k…
Browse files Browse the repository at this point in the history
…ill it (#6958)

(cherry picked from commit 0f7c456)
  • Loading branch information
dstandish authored and potiuk committed Jan 21, 2020
1 parent 2a892ba commit 04cd88e
Showing 1 changed file with 8 additions and 8 deletions.
16 changes: 8 additions & 8 deletions airflow/operators/bash_operator.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def __init__(
self.env = env
self.xcom_push_flag = xcom_push
self.output_encoding = output_encoding
self.sub_process = None

def execute(self, context):
"""
Expand Down Expand Up @@ -113,31 +114,30 @@ def pre_exec():
os.setsid()

self.log.info("Running command: %s", self.bash_command)
sp = Popen(
self.sub_process = Popen(
['bash', fname],
stdout=PIPE, stderr=STDOUT,
cwd=tmp_dir, env=env,
preexec_fn=pre_exec)

self.sp = sp

self.log.info("Output:")
line = ''
for line in iter(sp.stdout.readline, b''):
for line in iter(self.sub_process.stdout.readline, b''):
line = line.decode(self.output_encoding).rstrip()
self.log.info(line)
sp.wait()
self.sub_process.wait()
self.log.info(
"Command exited with return code %s",
sp.returncode
self.sub_process.returncode
)

if sp.returncode:
if self.sub_process.returncode:
raise AirflowException("Bash command failed")

if self.xcom_push_flag:
return line

def on_kill(self):
self.log.info('Sending SIGTERM signal to bash process group')
os.killpg(os.getpgid(self.sp.pid), signal.SIGTERM)
if self.sub_process and hasattr(self.sub_process, 'pid'):
os.killpg(os.getpgid(self.sub_process.pid), signal.SIGTERM)

0 comments on commit 04cd88e

Please sign in to comment.