treewide: fix bash exit handlers

Transform exit handlers of the form
trap cleanup EXIT [INT] [TERM] [QUIT] [HUP] [ERR]
  (where cleanup is idempotent)
to
trap cleanup EXIT

This fixes a common bash antipattern.

Each of the above signals causes the script to exit. For each signal,
bash first handles the signal by running `cleanup` and then runs
`cleanup` again when handling EXIT.
(Exception:  `vscode/*` prevents the second run of `cleanup` by removing
the trap in cleanup`).

Simplify the cleanup logic by just trapping exit, which is always run
when the script exits due to any of the above signals.

Note: In case of borgbackup, the exit handler is not idempotent, but just
trapping EXIT guarantees that it's only run once.
This commit is contained in:
Erik Arvstedt
2022-07-02 12:51:12 +02:00
parent bb7867f1e5
commit 3f54dfa475
6 changed files with 7 additions and 13 deletions

View File

@@ -35,7 +35,7 @@ toRemove=()
cleanup() {
rm -rf "${toRemove[@]}"
}
trap cleanup EXIT SIGINT SIGQUIT ERR
trap cleanup EXIT
MKTEMP='mktemp --tmpdir nix-rebuild-amount-XXXXXXXX'