fix: remove EXIT trap from wait_for_available_zone.sh - #6154
Conversation
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a resource management issue where a sourced script was unintentionally interfering with the parent shell's trap settings. By removing the global EXIT trap and implementing explicit cleanup of temporary files, the script now avoids potential side effects on the parent environment while maintaining resource hygiene. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request modifies the wait_for_available_zone.sh script to remove the EXIT trap and manually clean up temporary files upon fatal errors and normal completion. The review feedback points out two main issues when the script is sourced by a parent shell: first, calling exit 1 will terminate the parent shell session, which can be prevented by checking if the script is sourced and using return instead; second, the script's internal toggling of errexit will permanently alter the parent shell's settings, so the original state should be saved and restored.
| rm -f "$ZONE_EXPORT" "$ZONE_OUTPUT" | ||
| exit 1 |
There was a problem hiding this comment.
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.
| 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 |
| ZONE_EXPORT=$(mktemp) | ||
| ZONE_OUTPUT=$(mktemp) | ||
| trap 'rm -f "$ZONE_EXPORT" "$ZONE_OUTPUT"' EXIT | ||
|
|
There was a problem hiding this comment.
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"
This PR removes an EXIT trap from a sourced script that was unintentionally overriding the parent shell's trap, causing resource leaks.
Source
Submission Checklist
NOTE: Community submissions can take up to 2 weeks to be reviewed.
Please take the following actions before submitting this pull request.