Readiness checks and bounded polling
Readiness answers whether a service can accept work; liveness answers whether it should remain running; startup checks can give slow initialization time to finish. Kubernetes treats these probes differently. A process being alive or a container running is not proof of readiness. Define the check contract: zero means ready, one means not ready yet, and other statuses mean unexpected failures in this local lab. Real utilities use different codes: consult their contracts.
Bound every attempt and the overall workflow
Use a maximum attempt count, a finite delay, a per-command timeout and an overall deadline. A count alone cannot stop a hung request. GNU timeout can bound a local command; Kubernetes kubectl wait has a condition and wait timeout, and a request timeout may also be relevant. Never hide authentication or malformed data failures as transient unavailability. Retry only explicitly eligible failures, with bounded backoff and jitter for production. Waiting also costs resources; avoid polling large fleets continuously. The lab uses local counters and short delays, not Kubernetes requests. The counter example checks a failed delay and distinguishes pending (1) from fatal statuses. The fatal example handles status 2 deliberately for demonstration; production callers should propagate it. A timeout without a kill-after policy does not guarantee termination of a command that ignores TERM.
Practical examples
Run each example as a separate script in a fresh noninteractive Bash 5.2 process and empty practice directory. Tests use C locale; do not enter expected output as code. jq and GNU coreutils are external dependencies for selected examples. Use only the explicit local mock paths; do not use real accounts, clusters or sockets.
Bound readiness attempts
attempt=0probe() { (( ++attempt >= 3 )); }for ((i=1;i<=4;i++)); do if probe; then printf 'ready=%s\n' "$i"; exit "$?"; else rc=$?; fi (( rc == 1 )) || exit "$rc" printf 'pending=%s\n' "$i" || exit 1 (( i == 4 )) || sleep 0.01 || exit 1doneexit 1Expected stdout:
pending=1pending=2ready=3The counter reaches readiness before the budget expires.
Distinguish fatal errors from pending
probe() { return 2; }for ((i=1;i<=3;i++)); do if probe; then exit 0; else rc=$?; fi if (( rc != 1 )); then [[ $rc == 2 ]] || exit 1; printf 'fatal=%s\n' "$rc"; exit "$?"; fi printf 'pending\n'doneexit 1Expected stdout:
fatal=2The demonstration reports a fatal status without trying again; in an application propagate rc.
Bound a hung local check
if timeout 0.1s sleep 2; then exit 1else rc=$? [[ $rc == 124 ]] || exit 1 printf 'deadline=%s\n' "$rc"fiExpected stdout:
deadline=124GNU timeout returns 124 for this timed-out command. Other failure statuses need separate handling.
Common mistakes
Never use an unbounded until loop around a potentially hanging or unauthorized command.
Practice
Change readiness to occur on attempt four; then lower the budget to three and verify exhaustion. Explain the difference between request timeout and total deadline.
Next: Cloud CLI argument contracts