From 552a48f09d627c6739e2c802a2821417be981c6f Mon Sep 17 00:00:00 2001 From: Marcelo Vanzin Date: Wed, 4 May 2016 14:40:46 -0700 Subject: [PATCH] [SPARK-13670][launcher] Propagate error from launcher to shell. bash doesn't really propagate errors from subshells when using redirection the way spark-class does; so, instead, this change captures the exit code of the launcher process in the command array, and checks it before executing the actual command. Tested by injecting an error in Main.java (the launcher entry point) and verifying the shell gets the right exit code from spark-class. --- bin/spark-class | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/bin/spark-class b/bin/spark-class index b2a36b9846780..23a60c6ee44fe 100755 --- a/bin/spark-class +++ b/bin/spark-class @@ -64,8 +64,25 @@ fi # The launcher library will print arguments separated by a NULL character, to allow arguments with # characters that would be otherwise interpreted by the shell. Read that in a while loop, populating # an array that will be used to exec the final command. +# +# The exit code of the launcher is appended to the output, so the parent shell removes it from the +# command array and checks the value to see if the launcher succeeded. +build_command() { + "$RUNNER" -cp "$LAUNCH_CLASSPATH" org.apache.spark.launcher.Main "$@" + printf "%d\0" $? +} + CMD=() while IFS= read -d '' -r ARG; do CMD+=("$ARG") -done < <("$RUNNER" -cp "$LAUNCH_CLASSPATH" org.apache.spark.launcher.Main "$@") +done < <(build_command "$@") + +COUNT=${#CMD[@]} +LAST=$((COUNT - 1)) +LAUNCHER_EXIT_CODE=${CMD[$LAST]} +if [ $LAUNCHER_EXIT_CODE != 0 ]; then + exit $LAUNCHER_EXIT_CODE +fi + +CMD=("${CMD[@]:0:$LAST}") exec "${CMD[@]}"