Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions tools/cloud-build/wait_for_available_zone.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
# to prevent it from killing the pod.
ZONE_EXPORT=$(mktemp)
ZONE_OUTPUT=$(mktemp)
trap 'rm -f "$ZONE_EXPORT" "$ZONE_OUTPUT"' EXIT

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Sourcing this script will permanently alter the parent shell's errexit (set -e) setting because of the set +e and set -e calls inside the loop (lines 25 and 34). If the parent shell had set -e disabled, it will end up with set -e enabled after sourcing this script.

To prevent this side-effect, consider saving the original state of errexit at the beginning of the script and restoring it at the end:

# At the beginning of the script
OLD_ERREXIT=$(shopt -po errexit)

# At the end of the script (and before any exit/return)
eval "$OLD_ERREXIT"

while true; do
# Run the script in a subshell, streaming stdout and stderr to the console and a log file.
Expand All @@ -45,10 +44,10 @@ while true; do
sleep 300
else
echo "--- FATAL ERROR: find_available_zone.sh failed due to a configuration or system error. Exiting. ---" >&2
rm -f "$ZONE_EXPORT" "$ZONE_OUTPUT"
exit 1
Comment on lines +47 to 48

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Since this script is sourced by a parent shell, calling exit 1 will terminate the entire parent shell session (such as an interactive terminal or a parent script). To prevent this while still allowing the script to be executed directly, check if the script is being sourced or executed, and use return or exit accordingly.

Suggested change
rm -f "$ZONE_EXPORT" "$ZONE_OUTPUT"
exit 1
rm -f "$ZONE_EXPORT" "$ZONE_OUTPUT"
if [[ "${BASH_SOURCE[0]}" != "${0}" ]]; then
return 1
else
exit 1
fi

fi
fi
done

rm -f "$ZONE_EXPORT" "$ZONE_OUTPUT"
trap - EXIT
Loading