Skip to content

cluster: WithTransaction rolls back the reply of a failed entity handler, leaving the request unprocessed #8883

Closed

Description

@bweis

What version of Effect is running?

[email protected] (also 4.0.0). The code involved is unchanged on main at 269a7c8. Node 24.13.1.

What steps can reproduce the bug?

A persisted entity RPC annotated with ClusterSchema.WithTransaction, whose handler fails with its declared error, never gets its reply stored.

Runnable repro, no database needed (node:sqlite): https://github.com/TeamSpringbird/effect-cluster-with-transaction-repro

git clone https://github.com/TeamSpringbird/effect-cluster-with-transaction-repro
cd effect-cluster-with-transaction-repro && pnpm install && pnpm repro

The core of it registers the same RPC with and without the annotation, and calls each once and then again with the same primary key:

class Rejected extends Schema.TaggedError<Rejected>()("Rejected", {}) {}

const Reject = Rpc.make("Reject", {
  payload: { requestId: Schema.String },
  primaryKey: ({ requestId }) => requestId,
  error: Rejected
}).annotate(ClusterSchema.Persisted, true)

const WithTx = Entity.make("WithTx", [Reject]).annotateRpcs(ClusterSchema.WithTransaction, true)
const WithoutTx = Entity.make("WithoutTx", [Reject])
const handlers = { Reject: () => Effect.fail(new Rejected()) }

// SingleRunner.layer({ runnerStorage: "memory" }) over SqliteClient.layer({ filename: ":memory:" })
const client = (yield* WithTx.client)("entity-1")
yield* client.Reject({ requestId: "request-1" })   // first call
yield* client.Reject({ requestId: "request-1" })   // retry, same primary key

What is the expected behavior?

The failure reply is stored like any other reply: the request is marked processed, and a retry with the same primary key gets Rejected back. That is what happens without the annotation:

WithoutTx
  first call : Failure(Rejected)
  retry      : Failure(Rejected)
  stored     : processed=true replies=1

Rolling back the handler's own writes on failure is reasonable. Rolling back the reply and the processed flag with them is what looks wrong.

What do you see instead?

WithTx
  first call : Failure(Rejected)
  retry      : TimeoutError after 3s
  stored     : processed=false replies=0
  • The first caller gets the failure, handed over in memory.
  • The request stays unprocessed with no reply row, so a retry with the same primary key, or a caller on another runner, waits on storage forever.
  • After a restart the request is delivered again, the handler runs again, fails again and is rolled back again, on every restart.

We first hit this on @effect/sql-pg (PostgreSQL 18) with SingleRunner.layer(): concurrent callers deduplicated onto one request (same primary key) hung after the handler failed.

Additional information

Why it happens. requestWriteOptions passes storage.withTransaction as the RPC server's onRequest (entityManager.ts#L598-L602). RpcServer builds the request effect as Effect.onExit(handler, (exit) => write reply) (RpcServer.ts#L292) and only then wraps it in onRequest (RpcServer.ts#L328). The reply write is therefore inside the transaction, and withTransaction rolls back when the exit is a failure, taking the reply with it.

Why it hasn't come up. The existing test (Sharding.test.ts#L1677, "WithTransaction is propagated to the entity handler") covers only success. The one internal transactional RPC, the workflow engine's activity, never fails: it turns failures into a successful Workflow.Result (ClusterWorkflowEngine.ts#L496). The WithTransaction docs (ClusterSchema.ts) don't mention that a failing handler loses its reply.

Possible fixes, if this isn't intended:

  • run only the handler inside the transaction, and store a failure reply in its own transaction after the rollback; or
  • commit on a typed failure (Fail) and roll back only on defects and interrupts; or, at least,
  • document that a handler under WithTransaction must not fail.

Workaround we use: return rejections in the success channel (success: Schema.Result(A, E)) and decide before writing anything.

Is the current behavior intended?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    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