Skip to content

ClusterWorkflowEngine retries ClusterAbandon in resume handlers, blocking shard reacquisition after storage recovery #9934

Closed

Description

@IgnisDa

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:

  1. resumeCurrentRun calls waitForRunReply through ensureSuccess.
  2. The resume handler uses Rpc.wrap({ fork: true, uninterruptible: true }).
  3. When the parent retires, unregisterReplyHandler releases the waiter with EntityNotAssignedToRunner.
  4. waitForRunReply converts that into the annotated ClusterAbandon interruption.
  5. ensureSuccess applies Effect.sandbox, unconditional Effect.retry(retryPolicy), then Effect.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, resume handler, and ensureSuccess (around lines 376, 504, 652, and 899).
  • packages/effect/src/cluster/Sharding.ts: runShardRelease and forcedShardReleasePending.

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

This differs from an application deliberately running uninterruptible work: the non-terminating waiter is created and retried inside the workflow engine.

Activity

  1. self-assigned this
    on Oct 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions