Repository navigation
Description
Summary
After a brief runner-storage outage, ClusterWorkflowEngine can prevent forced shard release from finishing. Storage reports recovery, but the runner owns zero shards and new workflows remain unprocessed.
Reproduced with published [email protected] and [email protected], using Bun 1.4.4 on macOS. The reproduction uses only Effect's in-memory message and runner storage: no application code, SQL, network transport, or process restart.
Reproduction
Install [email protected], save the following as repro.ts, then run bun run repro.ts.
The parent starts a non-detached child and remains running. Once the child finishes, its persisted resume handler waits for the parent's run reply. A 1.4-second injected runner-storage failure forces shard teardown. After recovery, an independent probe workflow never executes.
import { Context, Effect, Layer, Schema } from "effect";
import { ClusterError, ClusterWorkflowEngine, EntityId, MessageStorage, RunnerHealth, Runners, RunnerStorage, Sharding, ShardingConfig } from "effect/cluster";
import { Workflow } from "effect/workflow";
import { WorkflowEngine } from "effect/workflow/WorkflowEngine";
let storageDown = false;
const started = Date.now();
const log = (...args: ReadonlyArray<unknown>) => console.log(Date.now() - started, ...args);
const FlakyRunnerStorage = Layer.effect(RunnerStorage.RunnerStorage, Effect.gen(function* () {
const storage = yield* RunnerStorage.makeMemory;
return RunnerStorage.RunnerStorage.of({
...storage,
refresh: (address, shards) => Effect.suspend(() => storageDown
? Effect.fail(new ClusterError.PersistenceError({ cause: new Error("brief storage outage") }))
: storage.refresh(address, shards)),
});
}));
const Parent = Workflow.make("ReproParent", { payload: {}, success: Schema.Void, idempotencyKey: () => "parent" });
const Child = Workflow.make("ReproChild", { payload: {}, success: Schema.Void, idempotencyKey: () => "child" });
const Probe = Workflow.make("ReproProbe", { payload: {}, success: Schema.Void, idempotencyKey: () => "probe" });
const cluster = Sharding.layer.pipe(
Layer.provideMerge(Runners.layerNoop),
Layer.provideMerge(MessageStorage.layerMemory),
Layer.provide(FlakyRunnerStorage),
Layer.provide(RunnerHealth.layerNoop),
Layer.provide(ShardingConfig.layer({
shardsPerGroup: 1,
shardLockRefreshInterval: 500,
shardLockExpiration: 3000,
entityTerminationTimeout: 500,
entityMessagePollInterval: 20,
entityReplyPollInterval: 20,
refreshAssignmentsInterval: 100,
})),
);
const live = Layer.mergeAll(
Child.toLayer(() => Effect.void),
Probe.toLayer(() => Effect.sync(() => log("probe executed"))),
Parent.toLayer(() => Effect.gen(function* () {
log("parent running");
yield* Child.execute({}, { discard: true });
yield* Effect.never;
})),
).pipe(Layer.provideMerge(ClusterWorkflowEngine.layer.pipe(Layer.provideMerge(cluster))));
const program = Effect.scoped(Effect.gen(function* () {
const context = yield* Layer.build(live);
const engine = Context.get(context, WorkflowEngine);
const sharding = Context.get(context, Sharding.Sharding);
const shard = sharding.getShardId(EntityId.make("parent"), "default");
yield* engine.execute(Parent, { executionId: "parent", payload: {}, discard: true });
yield* Effect.sleep(1500);
log("before outage ownsShard", sharding.hasShardId(shard));
storageDown = true;
yield* Effect.sleep(1400);
storageDown = false;
log("outage ended");
yield* Effect.sleep(4000);
log("after recovery ownsShard", sharding.hasShardId(shard));
yield* engine.execute(Probe, { executionId: "probe", payload: {}, discard: true });
yield* Effect.sleep(1000);
log("probe poll", JSON.stringify(yield* engine.poll(Probe, "probe")));
process.exit(0);
}));
Effect.runPromise(program).catch((error) => { console.error(error); process.exit(1); });The explicit process exit is only to stop the reproducer after observing the failure, without waiting for the stuck teardown.
Actual output on 4.0.6
35 parent running
1517 before outage ownsShard true
... Shard lock storage is unhealthy ...
2918 outage ended
... Shard lock storage has recovered
... Forced shard release is still pending; reacquisition is blocked until release completes
shardIds: [ "default:1" ]
entityTypes: [ "Workflow/ReproParent" ]
singletonNames: []
6931 after recovery ownsShard false
7935 probe poll {"_id":"Option","_tag":"None"}
Expected
The parent activation can stop during forced release. Once storage recovers, the runner releases/reacquires its shard, replays durable work, and executes the independent probe.
Controls
- Remove
Child.execute: shard reacquisition and probe execution succeed. - Change it to
Child.execute({}, { discard: true, detached: true }): shard reacquisition and probe execution succeed. On 4.0.6 the probe executed approximately 5 ms after submission. - The parent body is not explicitly uninterruptible; the no-child control replays it after recovery.
Suspected cause, with instrumentation
In ClusterWorkflowEngine.ts:
resumeCurrentRuncallswaitForRunReplythroughensureSuccess.- The
resumehandler usesRpc.wrap({ fork: true, uninterruptible: true }). - When the parent retires,
unregisterReplyHandlerreleases the waiter withEntityNotAssignedToRunner. waitForRunReplyconverts that into the annotatedClusterAbandoninterruption.ensureSuccessappliesEffect.sandbox, unconditionalEffect.retry(retryPolicy), thenEffect.orDie. It therefore retries this abandonment and registers another waiter for the retired run.
Instrumenting MessageStorage.registerReplyHandler and unregisterReplyHandler on 4.0.6 captured:
161 reply wait start ReproParent/parent/run
2393 unregisterReplyHandler <parent run request id>
2393 reply wait end ReproParent/parent/run
2595 reply wait start ReproParent/parent/run
The 202 ms re-registration matches the first retry delay. The run has no stored reply, so this new waiter cannot finish. Forced shard release waits for entity interruption before releasing locks/unregistering shard reply handlers; reacquisition is gated on that release completing.
Relevant 4.0.6 source:
packages/effect/src/cluster/ClusterWorkflowEngine.ts:waitForRunReply,resumeCurrentRun,resumehandler, andensureSuccess(around lines 376, 504, 652, and 899).packages/effect/src/cluster/Sharding.ts:runShardReleaseandforcedShardReleasePending.
The current main source also retains the unconditional sandboxed retry helper; I have not run this reproducer against main.
A possible focused fix is to preserve cluster-abandon interruptions instead of retrying them, so retiring activations can unwind. I have not applied or tested a fix.
Related work
- Sharding: runner silently owns zero shards forever when a singleton/entity on a force-released shard cannot be interrupted #9467 / Warn about stalled forced shard releases #9475 added diagnostics for stalled forced releases. The 4.0.6 reproducer emits that warning, but this workflow-generated stall remains.
- Fix cluster shutdown waiting on queued persisted workflow activities #9817 addresses queued persisted activities during runner shutdown; this reproduction involves a started parent
resumehandler during shard-loss recovery.
This differs from an application deliberately running uninterruptible work: the non-terminating waiter is created and retried inside the workflow engine.
Activity