Skip to content

Commit 1ad2d78

Browse files
authored
Merge pull request #142 from samsara-dev/danhansen/soft-fail-propagate-signal-terminations
fix(soft-fail): propagate signal terminations
2 parents 214cfaf + e3c1677 commit 1ad2d78

File tree

4 files changed

+44
-6
lines changed

4 files changed

+44
-6
lines changed

hooks/post-command

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,10 @@ perform_save() {
8080
# shellcheck disable=SC2064 # we want the expansion to happen now
8181
trap "cleanup_compression_tempfile '${ACTUAL_PATH}'" TERM EXIT
8282

83-
compress "${CACHE_PATH}" "${ACTUAL_PATH}" || return 1
83+
compress "${CACHE_PATH}" "${ACTUAL_PATH}" || return $?
8484
already_compressed='true' # avoid re-compressing the files
8585
fi
86-
backend_exec save "${KEY}" "${ACTUAL_PATH}" || return 1
86+
backend_exec save "${KEY}" "${ACTUAL_PATH}" || return $?
8787
lower_level_updated='true' # to force saving higher levels
8888
else
8989
echo "Cache of ${LEVEL} already exists, skipping"

hooks/pre-command

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ perform_restore() {
4848
# shellcheck disable=SC2064 # we want the expansion to happen now
4949
trap "cleanup_compression_tempfile '${TEMP_PATH}'" TERM EXIT
5050

51-
backend_exec get "${KEY}" "${TEMP_PATH}" || return 1
52-
decompress "${TEMP_PATH}" "${RESTORE_PATH}" || return 1
51+
backend_exec get "${KEY}" "${TEMP_PATH}" || return $?
52+
decompress "${TEMP_PATH}" "${RESTORE_PATH}" || return $?
5353
else
54-
backend_exec get "${KEY}" "${RESTORE_PATH}" || return 1
54+
backend_exec get "${KEY}" "${RESTORE_PATH}" || return $?
5555
fi
5656

5757
exit 0

lib/shared.bash

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,11 @@ soft_fail_exec() {
8585
local exit_code=$?
8686
set -e
8787

88-
if [ ${exit_code} -ne 0 ]; then
88+
# Exit codes 128+N indicate the process was killed by signal N
89+
# Always propagate signal terminations (SIGTERM, SIGKILL, SIGINT, etc.)
90+
if [ ${exit_code} -ge 128 ]; then
91+
exit ${exit_code}
92+
elif [ ${exit_code} -ne 0 ]; then
8993
echo "--- ⚠️ Cache ${operation} operation failed, continuing build (soft-fail enabled)"
9094
exit 0
9195
fi

tests/soft-fail.bats

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,4 +225,38 @@ teardown() {
225225
assert_output --partial 'Cache save operation failed, continuing build (soft-fail enabled)'
226226

227227
unstub dummy_compress_wrapper
228+
}
229+
230+
# ============================================================================
231+
# SIGNAL PROPAGATION TESTS
232+
# ============================================================================
233+
234+
@test "Soft-fail restore: signal termination (exit >= 128) is propagated" {
235+
export BUILDKITE_PLUGIN_CACHE_RESTORE=step
236+
237+
stub cache_dummy \
238+
'exists \* : exit 0' \
239+
'get \* \* : exit 143'
240+
241+
run "$PWD/hooks/pre-command"
242+
243+
assert_equal "$status" 143
244+
refute_output --partial 'Cache restore operation failed, continuing build (soft-fail enabled)'
245+
246+
unstub cache_dummy
247+
}
248+
249+
@test "Soft-fail save: signal termination (exit >= 128) is propagated" {
250+
export BUILDKITE_PLUGIN_CACHE_SAVE=step
251+
export BUILDKITE_PLUGIN_CACHE_FORCE=true
252+
253+
stub cache_dummy \
254+
'save \* \* : exit 143'
255+
256+
run "$PWD/hooks/post-command"
257+
258+
assert_equal "$status" 143
259+
refute_output --partial 'Cache save operation failed, continuing build (soft-fail enabled)'
260+
261+
unstub cache_dummy
228262
}

0 commit comments

Comments
 (0)