Skip to content

Isolate pipeline runs: refuse to replace a run in flight, key stage data by run (#943) - #972

Merged
scttfrdmn merged 1 commit into
mainfrom
fix/943
Oct 17, 2026
Merged

scttfrdmn merged 1 commit into
mainfrom
fix/943

Conversation

@scttfrdmn

Copy link
Copy Markdown
Contributor

Fixes #943

Stacked on #957 (fix/929). This PR's base is fix/929. Merge in order: #931, #942, #957, then this.

Cost safety. Found by reading the code, then tested with fakes and the in-process Substrate emulator. No AWS resources were created, nothing was launched and nothing was deployed.

What was wrong

  • pipeline.Store.Put was an unconditional PutItem. pipeline_id comes from the definition file, so launching the same file again while its run was in flight replaced that run's record with an empty INITIALIZING one. The running orchestrator chain reloaded a record that no longer listed its instances, and two chains then ran against one record. Each chain's save was also an unconditional PutItem, so they overwrote each other.
  • Markers and outputs were keyed <s3_prefix>/stages/<stage>/…. A second run under the same prefix found the first run's COMPLETE and completed its stages before they had done anything. Its dependents downloaded the first run's outputs, and a stale FAILED failed a fresh run. The CLI's copy of the definition (spawn-pipelines-<region>/pipelines/<id>/config.json) was also overwritten by the second launch, and the first run's orchestrator re-reads that copy on every cold start.

What changed

Refuse, or replace explicitly.

  • Store.Put is a conditional create: attribute_not_exists(pipeline_id) OR status IN (COMPLETED, FAILED, CANCELLED). Otherwise it returns *pipeline.ActiveRunError. DynamoDB evaluates the condition, so two concurrent launches cannot both pass it. A finished pipeline can still be launched again.
  • spawn pipeline launch refuses over a run in flight and explains the options: wait, spawn pipeline cancel, or --replace.
  • --replace sends a cancel aimed at that run only (SetCancelRequestedForRun, conditional on its run id). It waits up to 5 minutes for the run's own orchestrator to terminate its instances and mark it CANCELLED, then Replaces the record, conditional on it still being that run.
  • Why --replace and not --force. [audit] Flags: execute-vs-dry-run is --force (cleanup) vs --confirm (workspace-destroy) vs --dry-run (capacity-block) #315 retired --force from this CLI because the word meant different things on different commands (cleanup, upgrade-spored). --replace says what it does.

Run ids.

  • Every launch gets a run id: the UTC time plus 32 random bits from crypto/rand, e.g. 20261015t120000z-1a2b3c4d. It is printed at launch, stored as run_id, and sent in the Lambda event.
  • pipeline.RunKeyPrefix puts a run's markers, outputs and stage config under <s3_prefix>/runs/<run-id>/. The orchestrator builds every key from state.DataPrefix().
  • The orchestrator writes that run prefix as the spawn:s3-prefix tag. So the runner stays inside its run whatever its version: the deployed spored needs no change. It also adds a spawn:run-id tag, and exports SPAWN_S3_PREFIX (the run's prefix) and SPAWN_RUN_ID to the stage.
  • The CLI's definition copy is per run too, and collect reads only the run in the record.
  • The stage IAM policy already grants <s3_prefix>/*, which covers runs/.
  • A record with no run id keeps the old layout, so pipelines in flight across the upgrade still find their markers.

One chain per record.

  • The orchestrator's save is conditional on its run id (run_id = :run, or attribute_not_exists(run_id) for a legacy record). On ConditionalCheckFailed it returns errSuperseded and does not retry.
  • If a poll finds the record now belongs to another run, the chain terminates its own instances (only its in-memory state still lists them), deletes its groups and stops without saving or re-invoking.
  • An invocation whose run_id is not the record's exits before doing anything.
  • Security and placement group names carry the run id. Otherwise a replacement could collide with a replaced run that is still deleting its groups.

Tests (each watched failing against a revert that compiles)

revert fails
Put/Replace without the condition TestPutRefusesToReplaceARunInFlight, TestReplaceOverwritesOnlyTheRunItCancelled, TestLaunchOverARunInFlightIsRefused, TestLaunchReplaceCancelsTheOldRunFirst
DataPrefix() returns S3Prefix TestTwoRunsUnderOnePrefixShareNoKeys, TestStaleMarkersFromAnotherRunDoNotDecideThisOne, TestStageInstancesAreLaunchedIntoTheirRun, TestStageRunnerEndToEnd_StaysInsideItsRun (the real spored, against stale outputs left at both the old keys and another run's)
orchestrator save unconditional TestSaveDoesNotOverwriteAnotherRunsRecord (Substrate, which enforces condition expressions)
no supersede check in pollOnce TestReplacedRunTerminatesItsInstancesAndStops
no run check in handler TestInvocationForAReplacedRunExits
cancel-for-run unconditional TestCancelForRunOnlyCancelsThatRun
--replace skips the cancel TestLaunchReplaceCancelsTheOldRunFirst
group name without the run id TestStageInstancesAreLaunchedIntoTheirRun

make check: everything passes except TestGenerateWrapper_ChownFixesCrossUIDStickyUnlink_Docker, the known Docker-only failure (no daemon here). make check-nested: all 9 nested modules clean. go test ./lambda/pipeline-orchestrator/ ./pkg/pipeline/ (not -short): pass.

Deploy

  1. Deploy spawn-pipeline-orchestrator (make -C lambda/pipeline-orchestrator deploy, infra account). It needs no new IAM.
  2. Release the CLI.

The two can go in either order:

  • New CLI with the old Lambda: the refusal works, but runs are not yet isolated.
  • Old CLI with the new Lambda: records have no run id and keep the old layout.

Caveats

  • If a run is replaced in the few seconds between two invocations of its chain, nothing that still lists its instances is running. They are then reclaimed by their own self-termination and their TTL tags (pipeline: a failed stage's instance is never terminated, and the TTL reaper cannot see it (no spawn:managed tag) #912), not by the orchestrator. --replace waits for the cancel first so that this happens only if the old orchestrator is not making progress at all.
  • --replace needs the CLI's credentials to read and update the record. The CLI already reads and writes it.
  • On a refused launch, the CLI makes a best-effort delete of the definition object it uploaded (s3:DeleteObject). If that is not permitted, the object is left behind, which is harmless.

…ata by run (#943)

pipeline_id comes from the definition file, so launching it twice
reused it. Store.Put was an unconditional PutItem: the second launch
replaced the running pipeline's record with an empty INITIALIZING one,
orphaning its instances, and two orchestrator chains then ran against
one record. Markers and outputs were keyed by s3_prefix alone, so a
second run found the first run's COMPLETE (or FAILED) markers and its
dependents read the first run's outputs.

Store.Put is now a conditional create (absent or terminal), returning
*ActiveRunError otherwise. `spawn pipeline launch` refuses over a run
in flight; --replace cancels that run (SetCancelRequestedForRun,
conditional on its run id), waits up to 5 minutes for it to finish,
and then Replace()s it, conditional on it still being that run.

Every launch gets a run id (UTC time + 32 random bits, crypto/rand).
RunKeyPrefix puts a run's markers, outputs and stage config under
<s3_prefix>/runs/<run-id>; the CLI's definition copy is per run too.
The orchestrator uses state.DataPrefix() for every key and writes it as
the spawn:s3-prefix tag, so the runner (of any version) stays inside
its run. Records without a run id keep the old layout.

The orchestrator's save is conditional on its run id and returns
errSuperseded otherwise; a chain that finds the record moved to another
run terminates its own instances, cleans up its groups and stops; an
invocation for a replaced run exits. Network group names carry the run
id so a replacement cannot collide with a run still deleting its own.

Tests fail against compiling reverts of each guard (unconditional put,
save and cancel; DataPrefix = S3Prefix; no supersede/invocation check;
--replace without cancel; group name without run id), including an
end-to-end run of the real spored against stale outputs. (#943)
@codecov

codecov Bot commented Oct 17, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 76.78571% with 39 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
cmd/pipeline.go 54.71% 22 Missing and 2 partials ⚠️
pkg/pipeline/store.go 84.74% 7 Missing and 2 partials ⚠️
lambda/pipeline-orchestrator/main.go 91.11% 3 Missing and 1 partial ⚠️
pkg/pipeline/stage_keys.go 81.81% 1 Missing and 1 partial ⚠️

📢 Thoughts on this report? Let us know!

@scttfrdmn
scttfrdmn merged commit d2eacca into main Oct 17, 2026
8 checks passed
@scttfrdmn
scttfrdmn deleted the fix/943 branch October 17, 2026 04:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pipeline: re-launching a definition overwrites the in-flight record, and stale markers under the same s3_prefix decide the new run

1 participant