Skip to content

Commit 838b06b

Browse files
committed
fixup! fixup! feat: timeout the postStart hook commands
Signed-off-by: Oleksii Kurinnyi <[email protected]>
1 parent 7d064b9 commit 838b06b

File tree

1 file changed

+29
-11
lines changed

1 file changed

+29
-11
lines changed

pkg/library/lifecycle/poststart.go

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -108,26 +108,44 @@ func buildUserScript(commands []dw.Command) (string, error) {
108108
// generateScriptWithTimeout wraps a given user script with timeout logic,
109109
// environment variable exports, and specific exit code handling.
110110
// The killAfterDurationSeconds is hardcoded to 5s within this generated script.
111+
// It conditionally prefixes the user script with the timeout command if available.
111112
func generateScriptWithTimeout(escapedUserScript string, timeoutSeconds int32) string {
112113
return fmt.Sprintf(`
113114
export POSTSTART_TIMEOUT_DURATION="%d"
114115
export POSTSTART_KILL_AFTER_DURATION="5"
115116
116-
echo "[postStart hook] Executing commands with timeout: ${POSTSTART_TIMEOUT_DURATION} seconds, kill after: ${POSTSTART_KILL_AFTER_DURATION} seconds" >&2
117+
_TIMEOUT_COMMAND_PART=""
118+
_WAS_TIMEOUT_USED="false" # Use strings "true" or "false" for shell boolean
117119
118-
# Run the user's script under the 'timeout' utility.
119-
timeout --preserve-status --kill-after="${POSTSTART_KILL_AFTER_DURATION}" "${POSTSTART_TIMEOUT_DURATION}" /bin/sh -c '%s'
120+
if command -v timeout >/dev/null 2>&1; then
121+
echo "[postStart hook] Executing commands with timeout: ${POSTSTART_TIMEOUT_DURATION} seconds, kill after: ${POSTSTART_KILL_AFTER_DURATION} seconds" >&2
122+
_TIMEOUT_COMMAND_PART="timeout --preserve-status --kill-after=\"${POSTSTART_KILL_AFTER_DURATION}\" \"${POSTSTART_TIMEOUT_DURATION}\""
123+
_WAS_TIMEOUT_USED="true"
124+
else
125+
echo "[postStart hook] WARNING: 'timeout' utility not found. Executing commands without timeout." >&2
126+
fi
127+
128+
# Execute the user's script
129+
${_TIMEOUT_COMMAND_PART} /bin/sh -c '%s'
120130
exit_code=$?
121131
122-
# Check the exit code from 'timeout'
123-
if [ $exit_code -eq 143 ]; then # 128 + 15 (SIGTERM)
124-
echo "[postStart hook] Commands terminated by SIGTERM (likely timed out after ${POSTSTART_TIMEOUT_DURATION}s). Exit code 143." >&2
125-
elif [ $exit_code -eq 137 ]; then # 128 + 9 (SIGKILL)
126-
echo "[postStart hook] Commands forcefully killed by SIGKILL (likely after --kill-after ${POSTSTART_KILL_AFTER_DURATION}s expired). Exit code 137." >&2
127-
elif [ $exit_code -ne 0 ]; then # Catches any other non-zero exit code
128-
echo "[postStart hook] Commands failed with exit code $exit_code." >&2
132+
# Check the exit code based on whether timeout was attempted
133+
if [ "$_WAS_TIMEOUT_USED" = "true" ]; then
134+
if [ $exit_code -eq 143 ]; then # 128 + 15 (SIGTERM)
135+
echo "[postStart hook] Commands terminated by SIGTERM (likely timed out after ${POSTSTART_TIMEOUT_DURATION}s). Exit code 143." >&2
136+
elif [ $exit_code -eq 137 ]; then # 128 + 9 (SIGKILL)
137+
echo "[postStart hook] Commands forcefully killed by SIGKILL (likely after --kill-after ${POSTSTART_KILL_AFTER_DURATION}s expired). Exit code 137." >&2
138+
elif [ $exit_code -ne 0 ]; then # Catches any other non-zero exit code
139+
echo "[postStart hook] Commands failed with exit code $exit_code." >&2
140+
else
141+
echo "[postStart hook] Commands completed successfully within the time limit." >&2
142+
fi
129143
else
130-
echo "[postStart hook] Commands completed successfully within the time limit." >&2
144+
if [ $exit_code -ne 0 ]; then
145+
echo "[postStart hook] Commands failed with exit code $exit_code (no timeout)." >&2
146+
else
147+
echo "[postStart hook] Commands completed successfully (no timeout)." >&2
148+
fi
131149
fi
132150
133151
exit $exit_code

0 commit comments

Comments
 (0)