Skip to content

Comments

fix: add trap to clean up temp directory on unexpected exit#1598

Open
AnveshKolluri wants to merge 1 commit intogithub:mainfrom
AnveshKolluri:fix/cleanup-temp-dir-on-failure
Open

fix: add trap to clean up temp directory on unexpected exit#1598
AnveshKolluri wants to merge 1 commit intogithub:mainfrom
AnveshKolluri:fix/cleanup-temp-dir-on-failure

Conversation

@AnveshKolluri
Copy link

Problem

install.sh uses set -e which terminates the script on any command failure. When this happens (e.g., download 404, network error), the temp directory created by mktemp -d is never cleaned up — it's leaked in /tmp.

The existing rm -rf "$TMP_DIR" calls only cover manually handled error paths, but set -e implicit exits bypass all of them.

Repro:

VERSION=v99.99.99 curl -fsSL https://gh.io/copilot-install | bash
# curl fails with 404, set -e kills script, /tmp/tmp.XXXXX left behind

Fix

Add trap 'rm -rf "$TMP_DIR"' EXIT immediately after mktemp -d. This ensures cleanup on all exit paths — normal, error, and set -e implicit exits.

The 4 redundant manual rm -rf "$TMP_DIR" calls are removed since the trap handles them.

Changes

  • +1 line: trap 'rm -rf "$TMP_DIR"' EXIT after mktemp -d
  • -4 lines: Removed manual rm -rf "$TMP_DIR" calls (now redundant)

install.sh uses \set -e\ which terminates the script on any command
failure. When this happens (e.g., download 404, network error), the
temp directory created by \mktemp -d\ is never cleaned up because
the manual \
m -rf\ calls only cover explicitly handled error paths.

Add a \	rap ... EXIT\ immediately after creating the temp directory
to ensure cleanup on all exit paths. This also removes the now-
redundant manual cleanup calls, simplifying the error handling.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot AI review requested due to automatic review settings February 21, 2026 20:12
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR prevents /tmp temp directory leaks in install.sh when the script exits unexpectedly (including implicit exits from set -e) by centralizing cleanup in an EXIT trap.

Changes:

  • Add an EXIT trap immediately after mktemp -d to always remove the temp directory.
  • Remove now-redundant manual rm -rf "$TMP_DIR" cleanup calls on specific error/success paths.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant