Skip to content

Map cancelled request body uploads to OperationCanceledException in AndroidMessageHandler#11683

Draft
Copilot wants to merge 3 commits into
mainfrom
copilot/fix-androidmessagehandler-exception
Draft

Map cancelled request body uploads to OperationCanceledException in AndroidMessageHandler#11683
Copilot wants to merge 3 commits into
mainfrom
copilot/fix-androidmessagehandler-exception

Conversation

Copilot AI commented Jun 17, 2026

Copy link
Copy Markdown
Contributor

When a caller cancels the CancellationToken while AndroidMessageHandler is still uploading a request body (e.g. a PUT whose body the server delays reading), the cancellation surfaced as WebException/IOException ("Socket closed") instead of an OperationCanceledException subtype, diverging from SocketsHttpHandler.

The cancellation callback disconnects the socket, so the in-flight CopyToAsync to httpConnection.OutputStream throws Java.IO.IOException, which was then wrapped into a WebException by DoSendAsync. The response-body read path already mapped these to cancellation; the request-upload path did not.

Changes

  • WriteRequestContentToOutput: catch transport exceptions thrown during the body copy when cancellation has been requested, and rethrow as System.OperationCanceledException tied to the caller's token.
  • ShouldMapToCancellation: hoisted from the nested CancellationAwareResponseStream to the enclosing AndroidMessageHandler so the upload and response-read paths share one mapping rule (no change to the existing response-read behavior).
  • Test: RequestBodyUploadCancellationIsPrompt stalls a raw-socket server after reading the request headers, streams a large body in chunks until the send buffer blocks, cancels mid-upload, and asserts a prompt OperationCanceledException.
try {
    await stream.CopyToAsync (httpConnection.OutputStream!, 4096, cancellationToken).ConfigureAwait (false);
} catch (Exception ex) when (ShouldMapToCancellation (ex, cancellationToken)) {
    throw new System.OperationCanceledException ("Request body upload was canceled.", ex, cancellationToken);
}

Copilot AI and others added 2 commits June 17, 2026 14:17
…Exception

Co-authored-by: jonathanpeppers <840039+jonathanpeppers@users.noreply.github.com>
Co-authored-by: jonathanpeppers <840039+jonathanpeppers@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix cancelled PUT request mapping to IOException Map cancelled request body uploads to OperationCanceledException in AndroidMessageHandler Jun 17, 2026
Copilot AI requested a review from jonathanpeppers June 17, 2026 14:30
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.

AndroidMessageHandler maps cancelled PUT request to IOException/WebException instead of OperationCanceledException

2 participants