1
0
Fork 1
mirror of https://github.com/NixOS/nixpkgs.git synced 2024-11-17 19:21:04 +00:00

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
No known key found for this signature in database
GPG key ID: 33312B944DD97846
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'

View file

@ -23,12 +23,10 @@ let
on_exit()
{
exitStatus=$?
# Reset the EXIT handler, or else we're called again on 'exit' below
trap - EXIT
${cfg.postHook}
exit $exitStatus
}
trap 'on_exit' INT TERM QUIT EXIT
trap on_exit EXIT
archiveName="${if cfg.archiveBaseName == null then "" else cfg.archiveBaseName + "-"}$(date ${cfg.dateFormat})"
archiveSuffix="${optionalString cfg.appendFailedSuffix ".failed"}"

View file

@ -569,7 +569,7 @@ in
shopt -s inherit_errexit
create_role="$(mktemp)"
trap 'rm -f "$create_role"' ERR EXIT
trap 'rm -f "$create_role"' EXIT
db_password="$(<"$CREDENTIALS_DIRECTORY/db_password")"
echo "CREATE ROLE keycloak WITH LOGIN PASSWORD '$db_password' CREATEDB" > "$create_role"

View file

@ -39,11 +39,10 @@ prefetchExtensionUnpacked() {
function rm_tmpdir() {
1>&2 printf "rm -rf %q\n" "$tmpDir"
rm -rf "$tmpDir"
trap - INT TERM HUP EXIT
}
function make_trapped_tmpdir() {
tmpDir=$(mktemp -d)
trap rm_tmpdir INT TERM HUP EXIT
trap rm_tmpdir EXIT
}
1>&2 echo

View file

@ -45,11 +45,10 @@ extStoreName="${extPublisher}-${extName}"
function rm_tmpdir() {
rm -rf "$tmpDir"
trap - INT TERM HUP EXIT
}
function make_trapped_tmpdir() {
tmpDir=$(mktemp -d)
trap rm_tmpdir INT TERM HUP EXIT
trap rm_tmpdir EXIT
}
echo

View file

@ -21,13 +21,11 @@ fi
mkTempDir() {
tmpPath="$(mktemp -d "${TMPDIR:-/tmp}/nix-prefetch-cvs-XXXXXXXX")"
trap removeTempDir EXIT SIGINT SIGQUIT
trap removeTempDir EXIT
}
removeTempDir() {
if test -n "$tmpPath"; then
rm -rf "$tmpPath" || true
fi
rm -rf "$tmpPath"
}