Skip to content

spawn launch --command runs under bash -e, invisibly: 'set -uo pipefail' does not clear it, and the script dies before it can report why #707

Closed

Description

@scttfrdmn

Measured

spawn launch --command runs the user's script with errexit already enabled. Printing $- as the first real statement, before touching any options:

inherited_dollar_dash   ehB
errexit_inherited       YES
shell                   spawn-command.s / BASH_VERSION=5.2.15(1)-release

e is errexit. And critically, the idiomatic hardening line does not clear it:

with_pipefail_only_dollar_dash   ehuB     # after `set -uo pipefail`

So a script that opens with set -uo pipefail — believing it has chosen its own error policy — is actually running -e -u -o pipefail.

Why this is worth surfacing rather than just documenting somewhere

The failure mode is silent and actively misleading: the script exits before the line that would have reported why. Combined with spawn launch --command not staging logs to S3, the operator gets a truncated result file and no cause.

Two cases bit me, and both are ordinary shell idioms rather than mistakes:

1. A SIGPIPE'd pipeline under pipefail. zcat f.gz | sed -n '1,4000000p;4000001q' — sed quits at the cut, zcat takes SIGPIPE, the pipeline returns 141, and -e ends the run:

step_07_gz                 1938959399
step_08_df_root_after_dl   18G avail, 12% used
TRAP_EXIT                  line=1 rc=141

Same for the very common ... | head -n N.

2. wait on a background job that failed. A progress-sampling pattern —

docker run ... &
DP=$!
while kill -0 $DP 2>/dev/null; do sleep 20; report_progress; done
wait $DP; RC=$?        # <-- under -e, a non-zero job exits HERE
say rc "$RC"           #     so this never runs

— is specifically a pattern for capturing a failure, and -e destroys it at the moment it matters.

The cost, honestly accounted

Four runs on r8gd.8xlarge (~$4), each of which completed a 28-minute 1,107 GiB S3→NVMe copy and then died before producing the measurement. I misdiagnosed it three times — first as a TTL kill, then as an mmap/parallelism property of the workload (which I wrote into a published page and had to withdraw), then as a bash arithmetic error. I only found it by printing $-.

I'll also own my half: this project's own notes already record that a task command runs under set -e (in the context of rm -f returning EPERM and killing a task). I didn't connect that to launch --command, and $- is one line I could have printed on day one.

Ask

I'm not asking for -e to be removed — fail-fast is a defensible default, and silently running on after a broken step to TTL would be worse. The asks are cheaper than that:

  1. Echo the effective shell options into command.log at start. One line ($- plus set -o state) turns an invisible behaviour into a greppable one. This alone would have ended my afternoon.
  2. Document it where --command is documented, with the explicit note that set -uo pipefail does not clear it and that | head / wait are the common casualties.
  3. Optionally, a --no-errexit (or --shell-opts) escape hatch for scripts that do their own status checking.

(1) is the one I'd actually want. Happy to send a diff for the docs change if that's useful — issue first rather than a surprise PR.

Reproduce

spawn launch optcheck --instance-type c8g.large --ttl 15m --cost-limit 0.03 \
  --on-complete terminate --command 'echo "dollar-dash=$-"; case "$-" in *e*) echo ERREXIT;; esac'

Scripts and raw output: measurements/lith-kraken2-roda/ in scttfrdmn/scientific-codes-cookbook (shellopt.sh is the two-line probe above).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions