ClusterWorkflowEngine replays suspended workflows for unawaited deferred checkpoints
What version of Effect is running?
[email protected], @effect/[email protected], and @effect/[email protected]; Bun 1.4.2, macOS arm64, PostgreSQL 17.
Summary
A SQL-backed ClusterWorkflowEngine workflow completes three durable deferred checkpoints that it does not await, then suspends on a different durable deferred. The checkpoint completions cause an extra workflow body activation even though the awaited signal has not completed.
Expected body activation counts: 1 before releasing the awaited signal, 2 after completion. On unpatched rc.117 the reproduction prints 2 before, 3 after, then fails its assertion.
The checkpoints are read through engine.deferredResult and are only completed when absent. There are no child workflows, external resumes, failure injection, or application reconciliation.
Reproduction
Create these two files in an empty directory. All Effect packages are pinned; the override keeps the platform package's transitive dependency on the same release candidate.
package.json:
{
"name": "effect-unawaited-deferred-repro",
"private": true,
"type": "module",
"scripts": { "repro": "bun run repro.ts" },
"overrides": { "@effect/platform-node-shared": "4.0.0-rc.117" },
"dependencies": {
"effect": "4.0.0-rc.117",
"@effect/platform-bun": "4.0.0-rc.117",
"@effect/sql-pg": "4.0.0-rc.117"
}
}repro.ts:
import assert from "node:assert/strict";
import { BunServices } from "@effect/platform-bun";
import { PgClient } from "@effect/sql-pg";
import { Effect, Layer, Option, Redacted, Schema } from "effect";
import { ClusterWorkflowEngine, SingleRunner } from "effect/unstable/cluster";
import { Reactivity } from "effect/unstable/reactivity";
import { DurableDeferred, Workflow } from "effect/unstable/workflow";
import { WorkflowEngine, WorkflowInstance } from "effect/unstable/workflow/WorkflowEngine";
const url = process.env.DATABASE_URL ?? "postgres://postgres:postgres@localhost:55432/postgres";
const Parent = Workflow.make("UnawaitedCheckpoints", {
success: Schema.String,
payload: {},
idempotencyKey: () => "one",
});
const release = DurableDeferred.make("release", { success: Schema.Void });
const checkpoints = Array.from({ length: 3 }, (_, i) =>
DurableDeferred.make(`checkpoint-${i}`, { success: Schema.Void }),
);
const program = Effect.scoped(Effect.gen(function* () {
const admin = yield* PgClient.make({ url: Redacted.make(url) });
const schema = `repro_${crypto.randomUUID().replaceAll("-", "")}`;
yield* Effect.acquireRelease(admin.unsafe(`CREATE SCHEMA "${schema}"`), () =>
admin.unsafe(`DROP SCHEMA "${schema}" CASCADE`).pipe(Effect.orDie),
);
const engineLayer = ClusterWorkflowEngine.layer.pipe(
Layer.provide(SingleRunner.layer({
runnerStorage: "sql",
shardingConfig: { shardLockDisableAdvisory: true },
})),
Layer.provide(PgClient.layer({
url: Redacted.make(url), startupParameters: { search_path: schema },
})),
Layer.provide(BunServices.layer),
);
let activations = 0;
const registration = Parent.toLayer(Effect.fnUntraced(function* (_payload, executionId) {
activations++;
const engine = yield* WorkflowEngine;
for (const checkpoint of checkpoints) {
const stored = yield* engine.deferredResult(checkpoint);
if (Option.isNone(stored)) {
yield* DurableDeferred.succeed(checkpoint, {
value: undefined,
token: DurableDeferred.tokenFromExecutionId(checkpoint, { workflow: Parent, executionId }),
});
}
}
yield* DurableDeferred.await(release);
return "done";
})).pipe(Layer.provideMerge(engineLayer));
const context = yield* Layer.build(registration);
yield* Effect.gen(function* () {
const engine = yield* WorkflowEngine;
const executionId = "parent";
yield* engine.execute(Parent, { payload: {}, executionId, discard: true });
yield* Effect.gen(function* () {
for (;;) {
const result = yield* engine.poll(Parent, executionId);
const stored = yield* Effect.forEach(checkpoints, (checkpoint) =>
engine.deferredResult(checkpoint).pipe(Effect.provideService(
WorkflowInstance, WorkflowInstance.initial(Parent, executionId),
)),
);
if (Option.isSome(result) && result.value._tag === "Suspended" && stored.every(Option.isSome)) break;
yield* Effect.sleep("20 millis");
}
}).pipe(Effect.timeout("5 seconds"));
const beforeRelease = activations;
yield* DurableDeferred.succeed(release, {
value: undefined,
token: DurableDeferred.tokenFromExecutionId(release, { workflow: Parent, executionId }),
});
assert.equal(yield* engine.execute(Parent, { payload: {}, executionId }), "done");
const actual = { beforeRelease, afterRelease: activations };
console.log(JSON.stringify(actual));
assert.deepEqual(actual, { beforeRelease: 1, afterRelease: 2 });
}).pipe(Effect.provideContext(context));
})).pipe(
Effect.provide(Layer.merge(BunServices.layer, Reactivity.layer)),
Effect.timeout("15 seconds"),
);
await Effect.runPromise(program);Run:
docker run --rm -d --name effect-unawaited-repro-postgres \
-e POSTGRES_PASSWORD=postgres -p 127.0.0.1:55432:5432 postgres:17-alpine
# Wait until PostgreSQL is ready (retry this command if necessary).
docker exec effect-unawaited-repro-postgres pg_isready -U postgres
bun install
bun run repro
docker stop effect-unawaited-repro-postgresAlternatively, set DATABASE_URL to a local PostgreSQL database. The repro creates and drops its own schema.
Observed output:
{"beforeRelease":2,"afterRelease":3}
AssertionError: Expected values to be strictly deep-equalSource investigation
The installed rc.117 TypeScript, exported JavaScript, and JavaScript source map were byte-for-byte checked against the published npm tarball; its SHA-512 integrity also matched registry metadata.
In rc.117's src/unstable/cluster/ClusterWorkflowEngine.ts:509-520, the deferred handler caches payload.exit, then uses:
currentRun && !currentRun.awaitedDeferreds.has(payload.name)
? ensureSuccess(resume(workflow, executionId))
: deferredState.deferredDone(executionId, payload.name).pipe(Effect.andThen(resumeCurrentRun))Once the run suspends on another deferred, the first branch resets it despite the completed name not being awaited. Later awaits can already read the cached result, so an ordinary unawaited completion should not need that wake while currentRun exists. The Workflow/InterruptSignal is different: it must still wake a suspended run even when not explicitly awaited.
The same branch is present on current main in packages/effect/src/cluster/ClusterWorkflowEngine.ts. I checked current releases (including rc.118 and 4.0.0) and searched existing issues/PRs before filing. Related #6294, #8238/#8243, #8235, #8229, #8573, and #8574 concern other wake, handover, self-completion, or reset behavior; this repro specifically isolates unnecessary replay from ordinary unawaited checkpoint completions.
Source: Effect-TS/effect