Skip to content

fix: task env reaches containers; task run --wait fails on a failed task (#1059, #1143) - #1176

Merged
scttfrdmn merged 2 commits into
mainfrom
fix/1059-1143-task-wrapper
Oct 19, 2026
Merged

scttfrdmn merged 2 commits into
mainfrom
fix/1059-1143-task-wrapper

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Summary

#1059: TaskSpec env now reaches a container task.
pkg/taskproto/wrapper.go exported env in the host wrapper shell, but docker run never received it. writeContainerRun now adds one -e 'KEY=VALUE' argv word per key, in sorted order, before the image. The value is the literal one from the spec, single-quoted with shQuote.

  • Why not a bare -e KEY (the issue's first suggestion): sudo resets the environment by default, so docker would never see the exported value. --preserve-env=list depends on the sudoers policy.
  • Why not -e "KEY=$KEY": a key such as STAGE_RC or rc names a wrapper variable that is reassigned after the export. The container would get the wrapper's value, not the spec's.
  • Why not an env-file: docker's --env-file can't carry a multi-line value.
  • Keys are already restricted by Validate to [A-Za-z_][A-Za-z0-9_]* (taskspec.go, validEnvKey).
  • Trade-off: values now sit on the instance's docker run command line, so they show in the process list while the task runs. They were already written in clear text in the wrapper script on disk. The CHANGELOG entry says so.
  • Host mode is unchanged. The export block is shared and untouched.

#1143: task run --wait now exits non-zero on a failed task.
On OUT_RC != 0, the wrapper writes state=failed, retry_class=output_delivery_error and the command's exit_code, which is 0. --wait looked only at exit_code, so it exited 0.

  • New waitExitCode(rec) returns the task's exit code when non-zero, otherwise 1 for any state other than completed.
  • 1 matches task status --check-complete, which exits 1 for State == failed || ExitCode != 0.
  • The --wait flag help now says so.

Gates (each watched failing against a revert that compiles)

pkg/taskproto/wrapper_env_test.go

TestContainerTaskEnvReachesTheContainer:

  • Generates a container wrapper with env values holding a space, a ', a $, a backtick and ", plus the key STAGE_RC.
  • Runs the wrapper's own export lines, then STAGE_RC=0, then the extracted sudo docker run … under bash.
  • The stand-in sudo clears the environment (env -i), like env_reset. The stand-in docker resolves -e the way docker does: KEY=VALUE as given, a bare KEY from its own env.
  • Every spec key must arrive with exactly the spec's value.

Reverts tried:

Revert Result
No -e at all fails
Bare -e KEY fails
-e "KEY=$KEY" fails (STAGE_RC arrives as 0)
Unquoted -e KEY=VALUE fails

The other two gates:

  • TestContainerTaskEnvFlagsPrecedeTheImage: every -e must come before the image, otherwise it is container argv. The revert that moves the flags after the image fails here and only here.
  • TestHostTaskEnvIsStillExported: anchored ^export KEY='VALUE'$ for every key. Removing the export fails it.

cmd/task_wait_exit_test.go

TestTaskRunWaitFailsOnAFailedTask:

  • Discovers every RetryClass and TaskState constant by parsing pkg/taskproto/*.go with go/parser. It fails if discovery finds none, and fails if it misses output_delivery_error.
  • For each non-empty retry class, state=failed, exit_code=0 must give 1. For each state other than completed, exit_code=0 must give 1. An empty state must give 1.
  • Revert tried: if false && rec.State != …. It fails: state=failed retry_class=output_delivery_error exit_code=0: --wait exits 0, want 1.

TestTaskRunWaitKeepsTheTaskExitCode: completed/0 gives 0, and a non-zero task exit code is passed through.

Real docker, run locally, not committed:

  • The generated fragment was executed against local Docker with alpine:latest running printenv PLAIN WITH_SPACE TRICKY STAGE_RC. A stand-in sudo (env -i) was used.
  • All four values printed exactly, including it's $HOME \id` "q"andthe-spec-value`. rc=0.
  • The same line with the -e flags stripped (main's behaviour) printed nothing, rc=1.

Not verified

  • Not run on an instance; no AWS calls. Real sudo on AL2023 was not exercised. The -e KEY=VALUE form doesn't depend on sudo's env handling.
  • task status --check-complete exits 0 for state=cancelled, exit_code=0, because it checks only failed. --wait now treats every state other than completed as failure. Left unchanged here.

Checks

  • make check: fmt, vet (all GOOS), hermetic short tests, nested and tidy all pass.
  • staticcheck: GOOS=linux staticcheck ./... is clean. On darwin, SA4023 in pkg/agent/sys_other.go is known noise.
  • check-build-tags output is identical to unmodified origin/main on this machine, so it's local-only.
  • semgrep scan --config=auto --error . is clean.

Fixes #1059
Fixes #1143

@scttfrdmn scttfrdmn added this to the v0.135.2 milestone Oct 19, 2026
…ask (#1059, #1143)

TaskSpec env was exported in the host wrapper shell and never passed to
docker run. Pass each key as -e 'KEY=VALUE' with the literal, single-quoted
value: a bare -e KEY loses the value through sudo's env_reset, and a
"$KEY" reference picks up wrapper variables reassigned after the export.

task run --wait exited with the record's exit_code alone, so a failed task
whose command exited 0 (output_delivery_error) exited 0. Exit 1 for any
state other than completed, matching task status --check-complete.

Fixes #1059
Fixes #1143
@scttfrdmn
scttfrdmn force-pushed the fix/1059-1143-task-wrapper branch from 6fd6388 to 7aa97d4 Compare October 19, 2026 05:09
@codecov

codecov Bot commented Oct 19, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 84.61538% with 2 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
cmd/task.go 77.77% 2 Missing ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit d72b579 into main Oct 19, 2026
8 checks passed
@scttfrdmn
scttfrdmn deleted the fix/1059-1143-task-wrapper branch October 19, 2026 05:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant