Relationships
#4769 A step that depends on an empty forEach starts before that forEach's own dependencies have finished
Opened by nckrtl · 11/15/2026· Shipped 11/16/2026
What happens
A job has three steps: slow (2 s), then fan (a forEach whose collection is empty, dependsOn: slow), then after (dependsOn: fan, condition always). Expected order: slow ends, fan is skipped (empty), then after runs. Actual: after starts at once, in parallel with slow, and ends about 2 s before slow ends.
swamp 20261103.000636.0-sha.db3f6373: "after" started BEFORE "slow" ended (after.start - slow.end = -2010 ms)
swamp 20261114.210101.0-sha.537e03d1: "after" started BEFORE "slow" ended (after.start - slow.end = -2007 ms)The same happens with succeeded-free conditions that run on an empty fan-out on 537e03d1 (skipped, or(succeeded, skipped), not(failed)). Any step that a workflow author places "after X and after the fan-out of items of X" can run before X.
Why it matters
Authors use an empty-able forEach between two phases, for example "create directories, then write the files of a list, then reload the service". When the list is empty, the reload runs before the directories exist, although the graph says otherwise. A guard that reads the earlier step's status does not help: the earlier step has not run yet.
Where it comes from (as read)
src/domain/workflows/execution_service.ts at 537e03d1, lines 5415-5440, builds the graph of expanded steps:
- a forEach node with zero expanded items is dropped (
continue); - a dependent's dependency on it keeps the template name (
: [dep]); - no node has that name, so the sort places the dependent as if that edge did not exist, and with it every transitive edge back to
slow.
A fix could replace the dropped node's name in its dependents' dependencies with the dropped node's own dependencies (transitively), or keep the empty template as a node that completes at once.
Reproduction
foreach-order-repro.ts (at the end of this report) (Deno; deno run -A foreach-order-repro.ts <path to swamp>) creates a repository in a temporary folder, writes the workflow below, runs it and prints the order.
name: foreach-order
jobs:
- name: main
steps:
- name: slow
task: {type: behavior, behaviorType: command/shell, behaviorName: repro-slow, methodName: execute,
inputs: {run: 'echo "slow start $(date +%s%N)" >> order.log; sleep 2; echo "slow end $(date +%s%N)" >> order.log'}}
- name: fan
dependsOn: [{step: slow, condition: {type: succeeded}}]
forEach: {item: x, in: '${{ [] }}'}
task: {type: behavior, behaviorType: command/shell, behaviorName: repro-fan, methodName: execute, inputs: {run: 'true'}}
- name: after
dependsOn: [{step: fan, condition: {type: always}}]
task: {type: behavior, behaviorType: command/shell, behaviorName: repro-after, methodName: execute,
inputs: {run: 'echo "after start $(date +%s%N)" >> order.log; echo "after end $(date +%s%N)" >> order.log'}}Output on 537e03d1:
after start 1794738127886088725
slow start 1794738127888539872
after end 1794738127890279672
slow end [REDACTED-CC]Workaround that we use: the dependent also depends directly on the forEach's own dependencies.
Reproduced on Linux x86_64 with the published binaries 20261103.000636.0-sha.db3f6373 and 20261114.210101.0-sha.537e03d1 (main on 14 November).
foreach-order-repro.ts
/**
* Reproduction: a dependent of a forEach whose collection is empty starts before that forEach's own dependency has finished.
* Synthetic workflow, local shell steps only. Run: deno run -A upstream-foreach-order-repro.ts <path to swamp binary>
* It needs a signed-in swamp CLI.
*/
import { stringify } from "jsr:@std/yaml@1";
const binary = Deno.args[0];
const text = (b: Uint8Array) => new TextDecoder().decode(b);
const folder = Deno.makeTempDirSync({ prefix: "foreach-order-repro-" });
const run = async (...args: string[]) => {
const done = await new Deno.Command(binary, { args: [...args, "--json"], cwd: folder }).output();
if (!done.success) throw new Error(text(done.stderr) + text(done.stdout));
return text(done.stdout);
};
await run("repo", "init", folder, "--tool", "none", "--no-vault");
const log = `${folder}/order.log`;
// Each step appends "<name> start <ns>" and "<name> end <ns>"; "slow" takes 2 s.
const step = (name: string, seconds = 0) => ({
type: "behavior", behaviorType: "command/shell", behaviorName: `repro-${name}`, methodName: "execute",
inputs: { run: `echo "${name} start $(date +%s%N)" >> ${log}; sleep ${seconds}; echo "${name} end $(date +%s%N)" >> ${log}` },
});
const created = JSON.parse(await run("workflow", "create", "foreach-order"));
Deno.writeTextFileSync(created.path, stringify({
id: created.id, name: "foreach-order",
jobs: [{
name: "main",
steps: [
{ name: "slow", task: step("slow", 2) },
// depends on "slow"; its collection is empty
{ name: "fan", dependsOn: [{ step: "slow", condition: { type: "succeeded" } }], forEach: { item: "x", in: "${{ [] }}" }, task: step("fan") },
// depends only on "fan"; expected to start after "slow" has ended
{ name: "after", dependsOn: [{ step: "fan", condition: { type: "always" } }], task: step("after") },
],
}],
}));
await run("workflow", "run", "foreach-order");
const events = Deno.readTextFileSync(log).trim().split("\n").map((line) => line.split(" ")).map(([name, kind, ns]) => ({ name, kind, ns: BigInt(ns) }));
const at = (name: string, kind: string) => events.find((e) => e.name === name && e.kind === kind)?.ns;
const version = text((await new Deno.Command(binary, { args: ["--version"] }).output()).stdout).trim();
const slowEnd = at("slow", "end")!, afterStart = at("after", "start")!;
console.log(`${version}: "after" started ${afterStart < slowEnd ? "BEFORE" : "after"} "slow" ended ` +
`(after.start - slow.end = ${Number(afterStart - slowEnd) / 1e6} ms)`);
console.log(Deno.readTextFileSync(log).trim());Shipped
Click a lifecycle step above to view its details.
nckrtl commented 11/15/2026, 10:51:32 AM
Note on the output block: the CLI redactor replaced one nanosecond timestamp (the last line, slow end …) because it looked like a card number. The four lines in order were: after start, slow start 2 ms later, after end 2 ms later, slow end 2,003 ms after that. So after ran and ended about 2 s before slow ended.
stack72 commented 11/16/2026, 12:32:54 PM
Thanks @nckrtl for reporting this! We shipped: A step that depends on a forEach whose collection is empty is scheduled without waiting for that forEach's own dependencies, because the expanded-step graph drops the empty forEach node and the sort ignores the dangling dependency name. Fix: extract the expanded-step graph build into a pure domain function that replaces a dependency on an empty forEach with that forEach's effective dependencies, resolved transitively, and cover it with unit, property and execution tests. The empty forEach stays out of the graph and stays recorded as skipped, so events, history and condition semantics are unchanged; only when its dependents become eligible changes.. The fix has been merged and a release is on its way. We appreciate your contribution to swamp.
Sign in to post a ripple.