Repository navigation
Conversation
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`.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This change ensures that when a gRPC receiver stream is dropped, the client sends an
RST_STREAMto the server instead of a clean close (empty DATA frame with EOS).Previously, cancellation was handled using
Notifyandtake_untilwhich caused the request stream to yieldNone, leading to a clean close (DATAframe withEOS) followed byRST_STREAM.Now, we introduce a
CancellableStreamwrapper and a sharedCancellationState. When the receiver is dropped,CancellationState::cancelis called, which wakes the request stream. The stream then yieldsStreamItem::Cancellation.When the encoder (
BufEncoder) encounters this item, it returns an error, which triggersTonic/Hyperto reset the streamwith RST_STREAM and code
INTERNAL. Ideally, we would want the reset code to beCANCELLED.Alternative: Tonic-Level Cancellation Support
An alternative approach is to implement cancellation support directly within the tonic crate:
The client creates a
CancellationStateand passes it to Tonic viatonic::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