RpcClient: ClientProtocolError also fails new requests started by failure handlers
What version of Effect is running?
4.0.0-rc.118 (latest RC; also reproduced on 4.0.0-rc.109). The live-map iteration is still present on current main at b07406952b3cb653c78c784f5a402225ae7a583f.
What steps can reproduce the bug?
When a pending call fails due to ClientProtocolError, its error handler can start another call before the error broadcast finishes. If the protocol accepts that new request, RpcClient nevertheless fails it with the original protocol error.
This reproduction uses only Effect and a fake protocol, with no custom transport implementation. Save as repro.mjs, install [email protected], and run node repro.mjs:
import { Deferred, Effect, Schema } from "effect"
import { Rpc, RpcClient, RpcClientError, RpcGroup } from "effect/rpc"
const group = RpcGroup.make(Rpc.make("Probe", { success: Schema.String }))
const error = new RpcClientError.RpcClientError({
reason: new RpcClientError.RpcClientDefect({
message: "connection dropped",
cause: undefined
})
})
await Effect.runPromise(Effect.scoped(Effect.gen(function*() {
const started = yield* Deferred.make()
const requests = []
let deliver
const protocol = yield* RpcClient.Protocol.make((write) => {
deliver = write
return Effect.succeed({
send: (clientId, message) => Effect.sync(() => {
if (message._tag !== "Request") return
requests.push({ clientId, message })
console.log(`send request ${requests.length}`)
Deferred.doneUnsafe(started, Effect.void)
}),
supportsAck: false,
supportsTransferables: false,
codecFor: Schema.toCodecJson
})
})
const client = yield* RpcClient.make(group).pipe(
Effect.provideService(RpcClient.Protocol, protocol)
)
yield* Effect.forkChild(client.Probe().pipe(
Effect.catch(() => {
console.log("call 1 failed; starting call 2")
return client.Probe()
}),
Effect.catch((e) => Effect.sync(() => {
console.log("call 2 failed with:", e.reason.message)
console.log("same error instance:", e === error)
}))
))
yield* Deferred.await(started)
// Let call 1 suspend before delivering the protocol error.
yield* Effect.yieldNow
console.log("begin error delivery")
yield* deliver(requests[0].clientId, { _tag: "ClientProtocolError", error })
console.log("end error delivery")
})))What is the expected behavior?
The protocol error should fail the requests pending when its delivery begins. A new request started by a failure handler and accepted by the protocol should remain pending for its own response, rather than inherit the earlier error.
What do you see instead?
send request 1
begin error delivery
call 1 failed; starting call 2
send request 2
call 2 failed with: connection dropped
same error instance: true
end error deliveryAdditional information
The ClientProtocolError handler iterates entries.keys() directly. Delivering a failure can synchronously resume the caller, which adds a new request to that same map. The live iterator then visits the newly added request too.
In an isolated copy of rc.118, changing the iterable to Array.from(entries.keys()) fixes this reproduction. In a companion check that delivers a successful response to call 2 after the error broadcast, call 2 succeeds with the snapshot change and fails without it. Yielding before starting call 2 also avoids the failure.
I encountered this with a MessagePort protocol that queues new calls while disconnected. The retry was accepted into its queue, then failed by this loop, but still executed after reconnection. That later execution is specific to the queueing transport; the upstream issue is the spurious failure of the newly accepted request. A temporary transport-side guard that rejects sends during error delivery prevents the queued operation from executing later.
Effect's socket protocol already rejects sends while its currentError is set, so I am not claiming the same queued-execution behavior for that protocol.
Source: Effect-TS/effect