Skip to content

Stop the unary RPC client crash-looping on exactly-once ack failures - #119

Open
reidmorrison wants to merge 6 commits into
elixir-broadway:mainfrom
reidmorrison:streaming-exactly-once-ack-id-failure
Open

reidmorrison wants to merge 6 commits into
elixir-broadway:mainfrom
reidmorrison:streaming-exactly-once-ack-id-failure

Conversation

@reidmorrison

@reidmorrison reidmorrison commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

On an exactly-once subscription, Pub/Sub answers an Acknowledge (or ModifyAckDeadline) that contains expired or invalid ack ids with INVALID_ARGUMENT and a google.rpc.ErrorInfo detail (reason EXACTLY_ONCE_ACKID_FAILURE) whose metadata lists the failed ids, for example PERMANENT_FAILURE_INVALID_ACK_ID.

ErrorClassifier treats INVALID_ARGUMENT as terminal, so UnaryRpcClient.handle_rpc_error/4 stops the process before it reads those details. AckBatcher keeps the batch and sends it again to the restarted client, which gets the same answer and stops again. This repeats on every flush until the ack retry deadline, which is 600s under exactly-once.

What we saw

With the StreamingPull producer on an exactly-once subscription, one pipeline's UnaryRpcClient stopped with {:terminal_error, %GRPC.RPCError{status: 3, message: "Some acknowledgement ids in the request were invalid. This could be because the acknowledgement ids have expired or the acknowledgement ids were malformed.", details: [%Google.Protobuf.Any{type_url: "type.googleapis.com/google.rpc.ErrorInfo", ...}]}} about 4, 12, 19 and then 33 times a minute in the first minutes after a deploy, each time with an {:acknowledge, ...} from AckBatcher as its last message. The application stayed up, but the log filled with crash reports and the same ack batches were resent until their retry deadline.

The change

handle_rpc_error/4 now parses the per-ack-id details first. A terminal status that carries them is handled like a retryable status with details: the permanently failed ids are counted on [:broadway_cloud_pub_sub, :streaming, :unary, :permanent_failure], and the client replies with the error and keeps running. A terminal status without per-ack-id details (for example NOT_FOUND) still stops it, as before.

Only an ErrorInfo whose reason is EXACTLY_ONCE_ACKID_FAILURE counts as per-ack-id details; AckResult.parse_error_details/1 now ignores any other ErrorInfo, so a terminal error with a general detail (for example PERMISSION_DENIED with an IAM_PERMISSION_DENIED ErrorInfo) still stops the client. When the client keeps running it does not log anything; the rejected ids are reported only through telemetry. The :permanent_failure docs now say the count is per rejected request, not per unique ack id, since rejected ids are still resent until the ack retry deadline (see below).

Tests cover these cases: INVALID_ARGUMENT with an ErrorInfo detail (permanent, transient and unlisted ids) keeps the client alive, logs nothing and emits :permanent_failure with the permanent count; INVALID_ARGUMENT without details still stops it; and PERMISSION_DENIED with a general ErrorInfo still stops it.

A related issue, left out of this PR

When handle_rpc_error/4 works out the ids to retry and replies {:error, {error, transient_ids}}, chunked_rpc/3 turns any {:error, _} into "retry the whole chunk", so AckBatcher never receives that list. AckBatcher.extract_retained_ids/2 has a clause for {:error, {_rpc_error, transient_ids}}, but nothing reaches it. As a result, ids Pub/Sub rejected for good are resent until the retry deadline, for retryable statuses as well as with this fix.

I did not change that here because the same {:ok, failed_ids} result is also used for exactly-once receipt modacks, where StreamManager.handle_receipt_modack_success/3 must not deliver a message whose receipt modack failed, whether permanently or not. Dropping permanent ids from the list would deliver those messages. Separating "retry these" from "these did not succeed" in the result looks like the right fix, but it changes a contract three callers share, so I would like your view on the shape first. Happy to follow up with a PR.

Testing

mix format --check-formatted, mix deps.unlock --check-unused, mix compile --warnings-as-errors and mix test pass (Elixir 1.18.4, OTP 28; three full runs). I saw one unrelated failure in a single earlier full run, in stream_manager_test.exs "in exactly-once mode, messages are NOT immediately forwarded to producer" (a refute_received racing the receipt modack); it did not reproduce in 14 further runs, with or without this change.

On exactly-once subscriptions, Pub/Sub rejects expired or invalid ack
ids with INVALID_ARGUMENT and an ErrorInfo detail (reason
EXACTLY_ONCE_ACKID_FAILURE) listing them per ack id. ErrorClassifier
treats INVALID_ARGUMENT as terminal, so UnaryRpcClient stopped before
looking at the details. AckBatcher then sent the same batch to the
restarted client, which got the same answer and stopped again, on every
flush until the ack retry deadline (600s under exactly-once).

A terminal status that carries per-ack-id details is now handled like a
retryable one: the permanently failed ids are counted on the
:permanent_failure telemetry event and the client keeps running. A
terminal status without per-ack-id details still stops it.
- AckResult.parse_error_details/1 now reads only ErrorInfo details whose
  reason is EXACTLY_ONCE_ACKID_FAILURE. Before, any ErrorInfo with metadata
  (for example IAM_PERMISSION_DENIED with a "permission" key) looked like a
  per-ack-id answer, so PERMISSION_DENIED and other terminal errors kept the
  client running silently instead of stopping it. The parser now owns the
  rule for what counts as a per-ack-id answer.
- Log a warning when a terminal status is handled as per-ack-id rejections,
  so these failures stay visible.
- Document that :permanent_failure counts rejected requests, not unique ack
  ids, since rejected ids are still resent until the ack retry deadline. The
  producer docs said they were "dropped and not retried", which was wrong.
- Stop the new test from asserting that "ack-expired" is retained, which is
  a known limitation, not intended behavior.
- Fix AckResult tests to use the real ErrorInfo reason, and add tests for a
  general ErrorInfo being ignored and for PERMISSION_DENIED still stopping
  the client.
@reidmorrison
reidmorrison force-pushed the streaming-exactly-once-ack-id-failure branch from 49634df to 2b0aa06 Compare November 19, 2026 20:43

@rockneurotiko rockneurotiko left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was getting some weird feelings reviewing the PR, because I remember doing a research on official pubsub libraries and did not see anything about that kind of error, so I did the research again, and I think we should try to emulate what they do, I'll leave here my research but feel free to do one on your own:

I checked how the official Go (pubsub/v2/[iterator.go](https://github.com/home/rock/.t3/worktrees/broadway_cloud_pub_sub/t3-3ddfadca-266733843cf77ca6/pubsub/v2/iterator.go)) and Node.js (src/[message-queues.ts](https://github.com/home/rock/.t3/worktrees/broadway_cloud_pub_sub/t3-3ddfadca-266733843cf77ca6/src/message-queues.ts), src/[ack-metadata.ts](https://github.com/home/rock/.t3/worktrees/broadway_cloud_pub_sub/t3-3ddfadca-266733843cf77ca6/src/ack-metadata.ts)) clients handle these cases. They agree with each other, so there's a good model to copy.

What the other clients do

  • Per-ack-id results: both read the ErrorInfo metadata map as ack_id => code.
    • TRANSIENT_* is retried.
    • Any other value fails that id immediately, and it is never resent.
    • An id that is in the request but not in the metadata, with no RPC error, counts as a success.
    • So valid ids in a partly rejected request are not penalised.
  • Only the transient ids are re-sent. The batch isn't retried as a whole. Retries use a 1s to 64s jittered backoff, with an overall deadline of 600s in Go and 10 minutes in Node.
  • Errors with no per-id details: the RPC status code decides.
    • DEADLINE_EXCEEDED, RESOURCE_EXHAUSTED, ABORTED, INTERNAL and UNAVAILABLE are retried.
    • Anything else, such as PERMISSION_DENIED, fails all the ids with that error.
    • Neither client crashes or restarts on a terminal status. They report the failure per message and keep going.
  • Receipt modack under exactly-once:
    • A message is delivered only if its receipt modack succeeds.
    • A transient failure is retried a few times (3 in Node, 4 in Go).
    • A permanent failure, or running out of retries, drops the message so the server redelivers it.
    • The other ids in the same request are unaffected.
  • One difference from this PR: neither client checks reason == EXACTLY_ONCE_ACKID_FAILURE. Your check is the safer choice here, because it stops IAM metadata keys being read as ack ids. I'd keep it.

Suggested follow-up

Rather than a {retry_ids, failed_ids} split, I'd return a per-id outcome from the unary client, for example %{ack_id => :ok | :transient | {:permanent, reason}}.

  1. chunked_rpc/3 merges the per-chunk maps instead of collapsing a failed chunk into "retry everything".
  2. AckBatcher retries only :transient ids and drops :ok and {:permanent, _} ids. That stops the resends until the deadline, and :permanent_failure telemetry would then count each id once.
  3. The receipt-modack path in StreamManager delivers :ok messages and drops permanent ones. For transient ones it either retries a bounded number of times or drops them. A chunk with one expired id then no longer loses all its valid messages.
  4. Ids in the request but absent from the metadata resolve to :ok when there is no error, as in Go and Node. When there is an RPC error, the status code decides, as above.
  5. Longer term, terminal statuses without per-id details could fail the ids and keep the client running, as the official clients do, instead of stopping it. That removes the restart path altogether.

This also settles your concern about the shared {:ok, failed_ids} contract. Receipt modacks would use the same per-id map, so permanent and transient failures stay distinguishable and nothing gets delivered whose modack failed.

Comment on lines +319 to +321
Logger.warning(
"Cloud Pub/Sub rejected some ack ids while trying to #{operation} via gRPC - reason: #{inspect(error)}"
)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not a big fan of having logs in libraries, at least for warning messages, I prefer to have telemetry events and let the users decide what to do with that information.

If we keeep the logs, it would be better to have the permanent and transient counts than the raw reason

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed, thanks. I removed the warning in 0c49b86, so the rejected ids are now reported only through the :permanent_failure telemetry event. I've put a transient count, and whether to add it as a telemetry event, in #121 as decision 4.

The ids Pub/Sub rejects are already counted on the :permanent_failure
telemetry event, and the RPC error itself is available to telemetry
handlers, so the library does not log a warning for them.
@reidmorrison

Copy link
Copy Markdown
Contributor Author

Thanks for the research, it's a much better model to follow. I read the Go and Node sources as well; they match what you found, with three small corrections, which I've listed in #121. The most important one: when the RPC returns an error, ids missing from the ErrorInfo metadata follow the status code; they are not counted as successes.

I've written up the per-ack-id outcome design in #121, along with four decisions I'd like your view on before I build it. I'd keep this PR to the crash-loop fix (now without the warning log) and do the rest in follow-up PRs, if that works for you.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants