Skip to content

xds: avoid IllegalStateException on sendMessage/halfClose after internal cancellation - #13158

Open
kannanjgithub wants to merge 4 commits into
grpc:masterfrom
kannanjgithub:internal_cancellation_on_exception_handling
Open

kannanjgithub wants to merge 4 commits into
grpc:masterfrom
kannanjgithub:internal_cancellation_on_exception_handling

Conversation

@kannanjgithub

Copy link
Copy Markdown
Contributor

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.

Fixes the issue mentioned in an earlier comment in a PR.

…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
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
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

No deployments
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