Skip to content

grpc: Avoid sending DATA frame with EOS on dropping the receiver - #2788

Closed
arjan-bal wants to merge 2 commits into
grpc:masterfrom
arjan-bal:cancellation
Closed

arjan-bal wants to merge 2 commits into
grpc:masterfrom
arjan-bal:cancellation

Conversation

@arjan-bal

@arjan-bal arjan-bal commented Aug 4, 2026 •

Copy link
Copy Markdown
Contributor

This change ensures that when a gRPC receiver stream is dropped, the client sends an RST_STREAM to the server instead of a clean close (empty DATA frame with EOS).

Previously, cancellation was handled using Notify and take_until which caused the request stream to yield None, leading to a clean close (DATA frame with EOS) followed by RST_STREAM.

Now, we introduce a CancellableStream wrapper and a shared CancellationState. When the receiver is dropped, CancellationState::cancel is called, which wakes the request stream. The stream then yields StreamItem::Cancellation.

When the encoder (BufEncoder) encounters this item, it returns an error, which triggers Tonic/Hyper to reset the stream
with RST_STREAM and code INTERNAL. Ideally, we would want the reset code to be CANCELLED.

Alternative: Tonic-Level Cancellation Support

An alternative approach is to implement cancellation support directly within the tonic crate:
The client creates a CancellationState and passes it to Tonic via tonic::Request extensions. Tonic's client dispatcher (Grpc::streaming) extracts this state and passes it to the request body wrapper (EncodeBody). When cancellation is requested, EncodeBody yields an error.

This allows EncodeBody to set h2::Error::from(h2::Reason::CANCEL) as the source of the status error, enabling Hyper to send RST_STREAM with the explicit CANCEL reason instead of INTERNAL_ERROR.
Diff: https://github.com/grpc/grpc-rust/compare/master...arjan-bal:cancellation-tonic?expand=1

@arjan-bal arjan-bal closed this Aug 4, 2026
arjan-bal added a commit that referenced this pull request Aug 13, 2026
…ng End-of-Stream (#2791)

## Motivation

Tonic's client streaming and bidirectional (bidi) streaming APIs do not
currently support immediate request cancellation. Because outbound
request streams are infallible, completing them automatically transmits
a `DATA` frame with an `End-of-Stream (EOS)` flag, which signals a
normal, graceful termination. An `RST_STREAM` frame is only sent by the
client after the request stream terminates and the corresponding
response stream is subsequently dropped.

This delayed teardown creates a problem: the server may interpret the
`EOS` flag as a graceful finish and process or commit incomplete
payloads before it ever receives the `RST_STREAM` frame. To prevent
potential data corruption, Tonic requires a mechanism to abort and tear
down the HTTP/2 stream immediately without sending a misleading `EOS`
flag.

## Solution

Introduce a paired `CancellationHandle` to coordinate immediate,
client-side cancellation of outbound request streams.

### Core Changes

* `CancellationHandle` (User-Facing): Users can create a handle by
calling `request.cancellation_handle()`. Invoking `.cancel()` on this
handle triggers an immediate, non-graceful abort.
* The `CancellationHandle` wraps a `CancellationToken` which is
extracted from the request extensions by Tonic's client dispatcher to
actively track the cancellation state of the active stream.
* Immediate Abort in Encoder : `EncodeBody::poll_frame` actively checks
the `CancellationListener`. If cancellation is flagged, it immediately
returns an error mapped with `H2Reason::CANCEL`. This bypasses the empty
`DATA` frame with `EOS` and immediately triggers an HTTP/2 level
`RST_STREAM`.


## Alternative
Use the Encoder API to inject an error causing hyper to cancel the
stream: #2788

This seems like a mis-use of the codec API and causes the RST_STREAM
code to be `internal`.
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.

1 participant