Repository navigation
xds: avoid IllegalStateException on sendMessage/halfClose after internal cancellation - #13158
Open
kannanjgithub wants to merge 4 commits into
Open
kannanjgithub wants to merge 4 commits into
kannanjgithub wants to merge 4 commits into
Conversation
…nal cancellation
When RawMessageClientInterceptor or ExternalProcessorClientInterceptor
encounters an internal error (such as an IOException while reading the
request InputStream in sendMessage) and cancels the underlying call, the
application is not synchronously aware that the call was cancelled.
Callers such as ClientCalls.blockingUnaryCall immediately invoke
halfClose() after sendMessage(), which previously delegated to the
already-cancelled downstream call and threw
IllegalStateException("call was cancelled"), masking the underlying
CANCELLED status and cause.
Fix this by tracking internal cancellation (`aborted`) separately from
explicit application-initiated cancellation (`cancelCalled`):
1. RawMessageClientInterceptor (PartialForwardingClientCall):
- Move `sendSerializedMessage` into `PartialForwardingClientCall` so
`sendMessage`, `halfClose`, and `cancel` share per-call state.
- Set `aborted = true` when `sendSerializedMessage` catches an
`IOException` or `RuntimeException` and cancels `rawCall`, making
subsequent `sendMessage()` and `halfClose()` calls no-ops (matching
`ClientCallImpl.sendMessageInternal` when it cancels the underlying
stream internally).
- Set `cancelCalled = true` (and `aborted = false`) in `cancel()` and
delegate `rawCall.sendMessage(null)` before stream serialization
when `cancelCalled` is true, ensuring `sendMessage()` and
`halfClose()` after explicit application cancellation still throw
`IllegalStateException("call was cancelled")`.
2. ExternalProcessorClientInterceptor (DataPlaneClientCall):
- Set `aborted = true` in `cancelDownstream()` when triggered
internally (`!cancelCalled.get()`), turning subsequent
`sendMessage()`, `halfClose()`, and `proceedWithHalfClose()` calls
into no-ops.
- Set `cancelCalled = true` (and `aborted = false`) in `cancel()` and
check `!cancelCalled.get()` in `sendMessage()` and `halfClose()` so
explicit application cancellation enforces `IllegalStateException`
even when requests or half-close would otherwise be buffered or
sent to the ext_proc sidecar without synchronously reaching
`delayedCall`.
TAG=agy
CONV=56c87717-243d-4f12-af9e-c532e509892a
sauravzg
reviewed
Nov 3, 2026
1. Check !cancelCalled before aborted in sendMessage and halfClose, and have cancel() call delayedCall.cancel() directly (setting cancelCalled under streamLock) so cancelCalled and aborted never overwrite each other. 2. Replace rawCall.sendMessage(null) in RawMessageClientInterceptor with checkState(!cancelCalled, "call was cancelled"). 3. Separate public halfClose() from internalHalfClose() and guard downstream sendMessage/halfClose calls via proceedWithSendMessage() and proceedWithHalfClose() using isCancelledOrAborted() so sidecar and drain callbacks act as no-ops after cancellation or abort. TAG=agy CONV=56c87717-243d-4f12-af9e-c532e509892a
sauravzg
approved these changes
Dec 2, 2026
Skip polling and sending buffered request bodies to ext_proc in drainPendingRequestBodyMessages() when the call has already been cancelled or internally aborted, and verify in unit tests that buffered messages are not sent to ext_proc after ImmediateResponse, cancel(), or sendMessage() IOException. TAG=agy CONV=56c87717-243d-4f12-af9e-c532e509892a
…age() 1. Check !cancelCalled in DataPlaneClientCall.start() so explicit cancel() before start() throws IllegalStateException and does not open an ext_proc stream. 2. Make cancelCalled volatile in PartialForwardingClientCall and check it in start() across all RawMessageClientInterceptor variants as well as in interceptResponseOnly.sendMessage(). 3. Add unit tests covering cancel() before start() and cancel() before sendMessage()/halfClose() across all modes. TAG=agy CONV=56c87717-243d-4f12-af9e-c532e509892a
This branch has not been deployed
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.
When RawMessageClientInterceptor or ExternalProcessorClientInterceptor encounters an internal error (such as an IOException while reading the request InputStream in sendMessage) and cancels the underlying call, the application is not synchronously aware that the call was cancelled. Callers such as ClientCalls.blockingUnaryCall immediately invoke halfClose() after sendMessage(), which previously delegated to the already-cancelled downstream call and threw
IllegalStateException("call was cancelled"), masking the underlying CANCELLED status and cause.
Fix this by tracking internal cancellation (
aborted) separately from explicit application-initiated cancellation (cancelCalled):RawMessageClientInterceptor (PartialForwardingClientCall):
sendSerializedMessageintoPartialForwardingClientCallsosendMessage,halfClose, andcancelshare per-call state.aborted = truewhensendSerializedMessagecatches anIOExceptionorRuntimeExceptionand cancelsrawCall, making subsequentsendMessage()andhalfClose()calls no-ops (matchingClientCallImpl.sendMessageInternalwhen it cancels the underlying stream internally).cancelCalled = true(andaborted = false) incancel()and delegaterawCall.sendMessage(null)before stream serialization whencancelCalledis true, ensuringsendMessage()andhalfClose()after explicit application cancellation still throwIllegalStateException("call was cancelled").ExternalProcessorClientInterceptor (DataPlaneClientCall):
aborted = trueincancelDownstream()when triggered internally (!cancelCalled.get()), turning subsequentsendMessage(),halfClose(), andproceedWithHalfClose()calls into no-ops.cancelCalled = true(andaborted = false) incancel()and check!cancelCalled.get()insendMessage()andhalfClose()so explicit application cancellation enforcesIllegalStateExceptioneven when requests or half-close would otherwise be buffered or sent to the ext_proc sidecar without synchronously reachingdelayedCall.Fixes the issue mentioned in an earlier comment in a PR.