Repository navigation
Description
What version of Effect is running?
4.0.3
What steps can reproduce the bug?
Wrap the SQL message storage so that registerReplyHandler starts late:
const SlowRegistration = Layer.effect(
MessageStorage.MessageStorage,
Effect.gen(function*() {
const storage = yield* MessageStorage.makeEncoded(yield* SqlMessageStorage.makeEncoded())
return MessageStorage.MessageStorage.of({
...storage,
registerReplyHandler: (message) =>
Effect.andThen(Effect.sleep("50 millis"), storage.registerReplyHandler(message))
})
})
).pipe(Layer.provide(Snowflake.layerGenerator))Build Sharding.layer + Runners.layerNoop + this storage + SqlRunnerStorage on Postgres. Register a persisted entity (annotateRpcs(ClusterSchema.Persisted, true)) whose handler returns at once, and call it through its client from the same runner. The call never returns, although cluster_replies holds its WithExit reply. A 20 ms pause after saveRequest instead gives the same result, as soon as the read loop is woken during the pause.
What is the expected behavior?
The call returns the stored reply.
What do you see instead?
A persisted request sent to an entity whose shard the sending runner owns is handled by Runners.notifyLocal. Its order is:
notifyWith→storage.saveRequest(message)(the request row is written);options.notify(...)(the message is handed to the local entity);storage.registerReplyHandler(message)(the caller's wait is registered).
// cluster/Runners.ts, notifyLocal
return options.notify(Message.incomingLocalFromOutgoing(message)).pipe(
Effect.andThen(storage.registerReplyHandler(message)),
Effect.catchTag("EntityNotAssignedToRunner", () => replyFromStorage(message))
)Suppose the entity handles the request and saves its final reply between step 1 and step 3. This happens when the storage read loop claims the row right after step 1, or the entity runs before step 3. Then MessageStorage.make's saveReply finds no handler for the request id: the reply is persisted, and nobody is notified. Step 3 then registers a handler that nothing will ever resume. Unlike replyFromStorage, the local path never reads the reply back from storage. The caller waits forever, and a workflow run stays "running" in memory until the runner restarts.
We hit it in production-like conditions with ClusterWorkflowEngine (an execute that never returns). It happened 3 times in ~45 processes, more often under concurrency (pool exhaustion, GC pauses widen the window). Our storage notifies other runners with pg_notify on saveEnvelope, and that makes the race reachable even sequentially. It is not required for it.
Additional information
Suggested fix
Register the reply handler before the request can be processed, i.e. before saveRequest / notify. Or, after registerReplyHandler, read the request's final reply from storage once (repliesFor) and resume the handler if it is already there.
Workaround we ship
Our storage wraps registerReplyHandler for OutgoingRequests. It races the in-memory wait against a read of the final reply (repliesFor), first one second after registration, then every second. A lost wakeup then costs at most one second.
Activity