Skip to content

RpcClient: a discarded call over a protocol that never replies keeps its entry forever in 4.0.x and waits forever in 4.1.0 #10159

Description

@0rierel

What version of Effect is running?

effect 4.0.0 through 4.0.7, and 4.1.0. Related: #9178 (closed by #9182 in 4.1.0), #9460 (4.0.6), #10121.

What happens

Some transports cannot answer a call: an asynchronous serverless invoke, a message queue, a webhook that only acknowledges delivery. A custom RpcClient.Protocol over such a transport sends the request and never writes a reply. discard: true is the obvious way to call through it.

  • 4.0.0 – 4.0.7. client.Tag(payload, { discard: true }) returns once send finishes. RpcClient.make's onFromClient ignores the discard hint, though, and registers an entry { rpc, context, schemas }. The entry is removed only by the server's Exit or by an Interrupt. Neither comes for a discarded call over this kind of protocol, so the entry, holding the caller's whole Context, lives as long as the client. This is the case RPC: calls made with discard: true still get a reply #9178 reported. Fix RpcClient retaining requests that fail without a reply #9460 (4.0.6) fixed the related case of failed sends, but not this one.
  • 4.1.0. After Acknowledge discarded RPC calls before running handlers #9182, a discarded call waits for the server to acknowledge it with an Exit. On a protocol that never replies, the call never completes. Without a timeout, the caller's fiber and the entry both stay forever. Code that completed in 4.0.x hangs after the upgrade.

The protocol interface has no way to say "this transport does not reply". The docs also do not say that a protocol must acknowledge discarded requests itself.

Reproduction

Public APIs only. The protocol accepts every request and never answers. Each round provides ~10 MB of unique strings in the caller's context.

// node --expose-gc rpc-unanswered-request.mjs discard   (or: bun rpc-unanswered-request.mjs discard)
import { randomBytes } from "node:crypto";
import * as Context from "effect/Context";
import * as Effect from "effect/Effect";
import * as Layer from "effect/Layer";
import * as Option from "effect/Option";
import * as Schema from "effect/Schema";
import * as Rpc from "effect/rpc/Rpc";
import * as RpcClient from "effect/rpc/RpcClient";
import * as RpcGroup from "effect/rpc/RpcGroup";
import * as RpcSerialization from "effect/rpc/RpcSerialization";

const collect = () => (globalThis.Bun ? Bun.gc(true) : globalThis.gc());
const heapMB = () => Math.round(process.memoryUsage().heapUsed / 2 ** 20);

const Group = RpcGroup.make(Rpc.make("Record", { payload: { round: Schema.Number } }));
class History extends Context.Service()("History") {}

const OneWay = Layer.effect(
  RpcClient.Protocol,
  RpcClient.Protocol.make(() =>
    Effect.succeed({
      send: () => Effect.void,
      supportsAck: false,
      supportsTransferables: false,
      codecFor: RpcSerialization.json.codecFor,
    }),
  ),
);

const call = (client, round) =>
  client.Record({ round }, { discard: true }).pipe(
    Effect.exit,
    Effect.timeoutOption("200 millis"),
    Effect.map((exit) => (Option.isNone(exit) ? "still waiting after 200 ms" : exit.value._tag)),
    Effect.provideService(History, Array.from({ length: 2000 }, () => randomBytes(2500).toString("hex"))),
  );

await Effect.runPromise(
  Effect.scoped(
    Effect.gen(function* () {
      const client = yield* RpcClient.make(Group);
      for (let round = 0; round < 6; round++) {
        const outcome = yield* call(client, round);
        yield* Effect.promise(() => new Promise((resolve) => setTimeout(resolve, 10)));
        collect();
        console.log(`round ${round}: call ${outcome}, heapUsed ${heapMB()} MB`);
      }
    }),
  ).pipe(Effect.provide(OneWay)),
);

Expected

The discarded call completes once the request is sent, and the client keeps nothing for it, so heapUsed stays flat.

Actual

heapUsed in MB after each round, with a forced GC:

effect Node 22.23.2 Bun 1.4.4 the call
4.0.0 25, 35, 45, 54, 64, 73 14, 24, 33, 43, 52, 62 completes
4.0.7 24, 34, 43, 53, 63, 72 14, 24, 33, 43, 53, 62 completes
4.1.0 15, 15, 15, 15, 16, 16 5, 5, 5, 5, 5, 5 still waiting after 200 ms, every round
4.0.0 with #9460 and the 4.0.x change below 16, 16, 16, 16, 16, 16 4, 4, 4, 4, 4, 4 completes

On 4.1.0, the heap is flat only because the repro's timeout interrupts the call, and the interrupt removes the entry. Without the timeout, the first call never returns.

For comparison, a send that fails (Effect.fail(new RpcClientError(...)) from send) leaks the same way on 4.0.0, and is flat on 4.0.7 and 4.1.0 thanks to #9460.

Proposed change

Either of these would let a one-way protocol work:

  1. Let a protocol declare that it does not reply, for example supportsReplies: false next to supportsAck. RpcClient.make would then complete a discarded call once send succeeds, without registering an entry, and reject non-discarded calls on that protocol.
  2. Document that a protocol over a transport that cannot reply must write the acknowledgement itself, a successful Exit for the request id, once the transport accepts a discarded request. That is what a one-way protocol can do on 4.1.0 today.

For 4.0.x, the smallest fix is in RpcClient.make: remove the entry of a discarded request once its send is over. On an answering transport, a later Exit finds no entry and is ignored, as it already is in makeNoSerialization.

-    onFromClient({ message }) {
+    onFromClient({ message, discard }) {
 ...
-          ) as Effect.Effect<void, RpcClientError>
+          ).pipe(discard ? Effect.ensuring(Effect.sync(() => entries.delete(message.id))) : identity) as Effect.Effect<void, RpcClientError>

Activity

  1. effect-bot commented on Oct 29, 2026

    @effect-bot
    Contributor

    Thanks for the detailed report and the reproduction.

    We're not adding a separate "no replies" mode. The client relies on every request it sends ending with a reply for its id, usually an Exit. The protocol in the reproduction accepts requests and never replies, so on 4.1.0 the discarded call waits forever, and on 4.0.x the request entry is never removed. The client is behaving as designed; the protocol isn't meeting that contract.

    A transport that only delivers requests can still meet it on 4.1.0 and later:

    • Once your transport has delivered a discard request, write a void Exit for it. That's the strongest acknowledgement a one-way transport can give.
    • Fail send with an RpcClientError for any other request, so calls that expect a result fail immediately instead of hanging.

    Adapted from your reproduction:

    import { RpcClientDefect, RpcClientError } from "effect/rpc/RpcClientError"
    
    const OneWay = Layer.effect(
      RpcClient.Protocol,
      RpcClient.Protocol.make((writeResponse) =>
        Effect.succeed({
          send: (clientId, request) => {
            if (request._tag !== "Request") return Effect.void
            if (request.discard !== true) {
              return Effect.fail(
                new RpcClientError({
                  reason: new RpcClientDefect({
                    message: `${request.tag} must be called with discard: true`,
                    cause: undefined
                  })
                })
              )
            }
            return deliver(request).pipe( // your transport's write
              // lazy, so the acknowledgement is only written after delivery
              Effect.andThen(() =>
                writeResponse(clientId, {
                  _tag: "Exit",
                  requestId: request.id,
                  exit: { _tag: "Success", value: undefined }
                })
              )
            )
          },
          supportsAck: false,
          supportsTransferables: false,
          codecFor: RpcSerialization.json.codecFor
        })
      )
    )

    Keep the acknowledgement lazy (() => writeResponse(...)). Written eagerly, it is recorded as soon as send starts, so the client stops tracking the call before deliver runs and closing the client can no longer interrupt a pending delivery.

    I checked this against the published effect@4.1.0 and against current main:

    • On both:
      • client.Record({ round: 0 }, { discard: true }) completes with void once deliver succeeds, and nothing is kept after the reply.
      • A call without discard, whether unary or stream, fails with an RpcClientError, and nothing is delivered.
      • A pending discarded call is unaffected when another call is rejected.
    • Client shutdown: on 4.1.0, closing the client doesn't interrupt a discarded call whose delivery is still pending. That applies to every protocol, not just this one, and it's fixed on main for the next release.
    • 4.0.x: encoded requests carry no discard marker, so a protocol can't tell discarded calls apart. There's no maintained 4.0.x release line to fix that on, so please upgrade to 4.1.

    #10171 documents this contract on RpcClient.Protocol and on the discard option, and #10172 aligns those docs with the pattern above.

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