Skip to content

fix(update): always remove temp dir on exit#15

Open
CheapFuck wants to merge 1 commit into
codam-coding-college:mainfrom
CheapFuck:fix/update-tmpdir-leak
Open

fix(update): always remove temp dir on exit#15
CheapFuck wants to merge 1 commit into
codam-coding-college:mainfrom
CheapFuck:fix/update-tmpdir-leak

Conversation

@CheapFuck

Copy link
Copy Markdown

Summary

towel update --apply leaks the mktemp -d download directory in /tmp after every successful update.

Root cause

The --apply path registers two separate EXIT traps:

  1. trap '/usr/bin/rm -rf "$tmpdir"' EXIT — temp-dir cleanup
  2. later: trap 'rm -rf "$TOWEL_DATA"; mv "$backup_current" ...' EXIT — backup rollback

Bash keeps only the most recently registered EXIT trap, so (2) silently replaces (1). On success the final trap - EXIT then clears the trap entirely, so $tmpdir is never removed (and it also leaks on any failure that happens after the backup step).

Fix

Use a single EXIT trap with a cleanup function that always removes $tmpdir and performs the backup rollback only when a restore_backup flag is set. The flag is raised right before installing and cleared on success, so rollback still works on failure while the temp dir is always cleaned up.

Testing

  • shellcheck -x across all scripts: pass
  • shfmt -d across all scripts: pass

Fixes #12

The --apply path installed two separate EXIT traps: one to remove the
download temp dir and a later one to roll back the backup. Bash keeps
only the most recently registered trap, so the second registration
silently dropped the temp-dir cleanup, and the final 'trap - EXIT'
cleared it entirely. As a result the mktemp -d directory was leaked in
/tmp after every successful update (and after failures past the backup
step).

Use a single EXIT trap with a cleanup function that always removes the
temp dir and performs the backup rollback only when a restore_backup
flag is set. The flag is raised before installing and cleared on
success, so the rollback still works while the temp dir is always
cleaned up.

Fixes codam-coding-college#12
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.

towel update hook overwrites exit trap

1 participant