Skip to content

fix: remove EXIT trap from wait_for_available_zone.sh - #6154

Draft
sudheer-quad wants to merge 1 commit into
GoogleCloudPlatform:developfrom
sudheer-quad:fix/prevent-cleanup-trap-override
Draft

fix: remove EXIT trap from wait_for_available_zone.sh#6154
sudheer-quad wants to merge 1 commit into
GoogleCloudPlatform:developfrom
sudheer-quad:fix/prevent-cleanup-trap-override

Conversation

@sudheer-quad

Copy link
Copy Markdown
Contributor

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.

  • Fork your PR branch from the Toolkit "develop" branch (not main)
  • Test all changes with pre-commit in a local branch #
  • Confirm that "make tests" passes all tests
  • Add or modify unit tests to cover code changes
  • Ensure that unit test coverage remains above 80%
  • Update all applicable documentation
  • Follow Cluster Toolkit Contribution guidelines #

@github-actions github-actions Bot added the external PR from external contributor label Aug 17, 2026
@sudheer-quad sudheer-quad self-assigned this Aug 17, 2026
@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, 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

  • Trap Removal: Removed the global EXIT trap from the script to prevent it from overriding the parent shell's trap configuration.
  • Manual Cleanup: Added explicit removal of temporary files before script exit to ensure proper resource cleanup without relying on the trap.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

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.

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.

Comment on lines +47 to 48
rm -f "$ZONE_EXPORT" "$ZONE_OUTPUT"
exit 1

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

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"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

external PR from external contributor

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant