Skip to content

[NSUrlSessionHandler] Surface cancelled requests with a better exception. Fixes #25667.#25699

Open
rolfbjarne wants to merge 10 commits into
mainfrom
dev/rolf/issue-25667-stream-readasync-cancellation-test
Open

[NSUrlSessionHandler] Surface cancelled requests with a better exception. Fixes #25667.#25699
rolfbjarne wants to merge 10 commits into
mainfrom
dev/rolf/issue-25667-stream-readasync-cancellation-test

Conversation

@rolfbjarne

@rolfbjarne rolfbjarne commented Jun 16, 2026

Copy link
Copy Markdown
Member

Surface cancelled requests with an OperationCanceledException instead of a TimeoutException. This makes it easier for consumers to determine what happened.

Co-authored-by: Copilot 223556219+Copilot@users.noreply.github.com

🤖 Pull request created by Copilot

Fixes: #25667

…ion. Fixes #25667.

Add a test: that sets up a stalling HTTP server, reads the first byte
successfully, then cancels the second ReadAsync via a caller-owned
CancellationToken.

TODO:

* Implement the fix.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@rolfbjarne

Copy link
Copy Markdown
Member Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 3 pipeline(s).

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

Localhost server access should be reliable, so there's no need to
paper over failures with IgnoreInCI/Inconclusive.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@rolfbjarne

Copy link
Copy Markdown
Member Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 3 pipeline(s).

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

rolfbjarne and others added 2 commits June 17, 2026 08:49
With Content-Length set, HttpListener may buffer the output until all
bytes are written. Switching to chunked transfer encoding ensures the
first byte reaches the client immediately, so the first ReadAsync
completes and the test can proceed to the cancellation scenario.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@rolfbjarne

Copy link
Copy Markdown
Member Author

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 3 pipeline(s).

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

rolfbjarne and others added 2 commits June 18, 2026 09:06
When the caller's CancellationToken triggers cancellation in the
ReadAsync polling loop, rethrow as OperationCanceledException (via
ThrowIfCancellationRequested) instead of wrapping it in TimeoutException.

TimeoutException is now only thrown when the cancellation comes from
an internal source (e.g. HttpClient.Timeout), not from the caller's
token.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Use 'new OperationCanceledException(message, ex, cancellationToken)'
instead of ThrowIfCancellationRequested so the original
TaskCanceledException stack trace is preserved as InnerException.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

This comment has been minimized.

@rolfbjarne rolfbjarne marked this pull request as ready for review June 22, 2026 17:17
Copilot AI review requested due to automatic review settings June 22, 2026 17:17

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates NSUrlSessionHandler’s response stream read behavior so that caller-requested cancellation surfaces as an OperationCanceledException/TaskCanceledException rather than being converted into a TimeoutException (fixing #25667).

Changes:

  • Adjusts NSUrlSessionDataTaskStream.ReadAsync cancellation/timeout exception mapping.
  • Adds a regression test that reproduces stalled-response reads and asserts caller cancellation surfaces as OperationCanceledException (or subclass).

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.

File Description
src/Foundation/NSUrlSessionHandler.cs Changes exception behavior when ReadAsync is canceled while polling for incoming data.
tests/monotouch-test/System.Net.Http/NSUrlSessionHandlerTest.cs Adds a TCP-based repro test ensuring caller cancellation during a stalled read yields OperationCanceledException.

Comment on lines 1495 to +1499
} catch (TaskCanceledException ex) {
// add a nicer exception for the user to catch, add the cancelation exception
// to have a decent stack
// If the caller's token triggered the cancellation, surface it
// as OperationCanceledException so callers can distinguish
// between a caller-requested cancellation and a request timeout.
if (cancellationToken.IsCancellationRequested)
Comment on lines +367 to +368
var response = await httpClient.SendAsync (request, HttpCompletionOption.ResponseHeadersRead).ConfigureAwait (false);
var stream = await response.Content.ReadAsStreamAsync ().ConfigureAwait (false);
Comment on lines +347 to +348
// Stall: never send the remaining body
await Task.Delay (TimeSpan.FromMinutes (5)).ConfigureAwait (false);
@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

🔥 [PR Build #4ef4e4a] Build failed (Detect API changes) 🔥

Build failed for the job 'Detect API changes' (with job status 'Failed')

Pipeline on Agent
Hash: 4ef4e4a0a321be013fd8bf94c4bfc794bfe11ca2 [PR build]

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

🔥 Unable to find the contents for the comment: D:\a\1\s\change-detection\results\gh-comment.md does not exist :fire

Pipeline on Agent
Hash: 4ef4e4a0a321be013fd8bf94c4bfc794bfe11ca2 [PR build]

@vs-mobiletools-engineering-service2

This comment has been minimized.

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

✅ [PR Build #4ef4e4a] Build passed (Build packages) ✅

Pipeline on Agent
Hash: 4ef4e4a0a321be013fd8bf94c4bfc794bfe11ca2 [PR build]

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

✅ [PR Build #4ef4e4a] Build passed (Build macOS tests) ✅

Pipeline on Agent
Hash: 4ef4e4a0a321be013fd8bf94c4bfc794bfe11ca2 [PR build]

@vs-mobiletools-engineering-service2

Copy link
Copy Markdown
Collaborator

🔥 [CI Build #4ef4e4a] Test results 🔥

Test results

❌ Tests failed on VSTS: test results

0 tests crashed, 22 tests failed, 198 tests passed.

Failures

❌ monotouch tests (tvOS)

21 tests failed, 0 tests passed.

Failed tests

  • monotouch-test/tvOS - simulator/Debug: LaunchTimedOut
  • monotouch-test/tvOS - simulator/Release (link sdk): LaunchTimedOut
  • monotouch-test/tvOS - simulator/Release (link all): LaunchTimedOut
  • monotouch-test/tvOS - simulator/Debug (PrepareAssemblies): LaunchTimedOut
  • monotouch-test/tvOS - simulator/Debug (LinkSdk): LaunchTimedOut
  • monotouch-test/tvOS - simulator/Debug (static registrar): LaunchTimedOut
  • monotouch-test/tvOS - simulator/Release (all optimizations): LaunchTimedOut
  • monotouch-test/tvOS - simulator/Debug (ARM64): LaunchTimedOut
  • monotouch-test/tvOS - simulator/Release (NativeAOT, ARM64): LaunchTimedOut
  • monotouch-test/tvOS - simulator/Release (trimmable static registrar, NativeAOT, ARM64): LaunchTimedOut
  • monotouch-test/tvOS - simulator/Debug (managed static registrar): LaunchTimedOut
  • monotouch-test/tvOS - simulator/Release (managed static registrar, all optimizations): LaunchTimedOut
  • monotouch-test/tvOS - simulator/Release (NativeAOT, x64): LaunchTimedOut
  • monotouch-test/tvOS - simulator/Release (trimmable static registrar, NativeAOT, x64): LaunchTimedOut
  • monotouch-test/tvOS - simulator/Debug (interpreter): LaunchTimedOut
  • monotouch-test/tvOS - simulator/Release (interpreter): LaunchTimedOut
  • monotouch-test/tvOS - simulator/Release (compat inline Class.GetHandle): LaunchTimedOut
  • monotouch-test/tvOS - simulator/Release (strict inline Class.GetHandle): LaunchTimedOut
  • monotouch-test/tvOS - simulator/Release (compat inline dlfcn): LaunchTimedOut
  • monotouch-test/tvOS - simulator/Release (strict inline dlfcn, link sdk): LaunchTimedOut
  • monotouch-test/tvOS - simulator/Release (NativeAOT, .NET 11 defaults): LaunchTimedOut

Html Report (VSDrops) Download

❌ Tests on macOS Tahoe (26) tests

1 tests failed, 4 tests passed.

Failed tests

  • monotouch-test: Failed (exit code 2)
    • No test failure details available. stderr output:
      • 2026-06-22 11:25:54.944 monotouchtest[80840:3775045] [PASS] VeryGeneric
      • 2026-06-22 11:25:54.945 monotouchtest[80840:3775045] [PASS] WrapperTypeLookupTest
      • 2026-06-22 11:25:54.945 monotouchtest[80840:3775045] Xamarin.Tests.RuntimeTest : 338.409 ms
      • 2026-06-22 11:25:54.946 monotouchtest[80840:3775045] Xamarin.Tests : 338.462 ms
      • 2026-06-22 11:25:54.946 monotouchtest[80840:3775045] Xamarin : 345.784 ms
      • 2026-06-22 11:25:54.946 monotouchtest[80840:3775045] bindings-test : 345.8417 ms
      • 2026-06-22 11:25:54.951 monotouchtest[80840:3774013] Tests run: 3623 Passed: 3613 Inconclusive: 10 Failed: 0 Ignored: 148
      • 2026-06-22 11:25:54.951 monotouchtest[80840:3774013] AutoRun (): completed test run on main thread
      • 2026-06-22 11:25:54.951 monotouchtest[80840:3774013] Exiting test run with success
      • make: *** [exec-monotouch-test] Error 1

Html Report (VSDrops) Download

Successes

✅ assembly-processing: All 1 tests passed. Html Report (VSDrops) Download
✅ cecil: All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (iOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (MacCatalyst): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (macOS): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (Multiple platforms): All 1 tests passed. Html Report (VSDrops) Download
✅ dotnettests (tvOS): All 1 tests passed. Html Report (VSDrops) Download
✅ framework: All 2 tests passed. Html Report (VSDrops) Download
✅ fsharp: All 4 tests passed. Html Report (VSDrops) Download
✅ generator: All 5 tests passed. Html Report (VSDrops) Download
✅ interdependent-binding-projects: All 4 tests passed. Html Report (VSDrops) Download
✅ introspection: All 6 tests passed. Html Report (VSDrops) Download
✅ linker (iOS): All 15 tests passed. Html Report (VSDrops) Download
✅ linker (MacCatalyst): All 15 tests passed. Html Report (VSDrops) Download
✅ linker (macOS): All 21 tests passed. Html Report (VSDrops) Download
✅ linker (tvOS): All 15 tests passed. Html Report (VSDrops) Download
✅ monotouch (iOS): All 21 tests passed. Html Report (VSDrops) Download
✅ monotouch (MacCatalyst): All 24 tests passed. Html Report (VSDrops) Download
✅ monotouch (macOS): All 24 tests passed. Html Report (VSDrops) Download
✅ msbuild: All 2 tests passed. Html Report (VSDrops) Download
✅ sharpie: All 1 tests passed. Html Report (VSDrops) Download
✅ windows: All 3 tests passed. Html Report (VSDrops) Download
✅ xcframework: All 4 tests passed. Html Report (VSDrops) Download
✅ xtro: All 1 tests passed. Html Report (VSDrops) Download

macOS tests

✅ Tests on macOS Monterey (12): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Ventura (13): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Sonoma (14): All 5 tests passed. Html Report (VSDrops) Download
✅ Tests on macOS Sequoia (15): All 5 tests passed. Html Report (VSDrops) Download

Linux Build Verification

Linux build succeeded

Pipeline on Agent
Hash: 4ef4e4a0a321be013fd8bf94c4bfc794bfe11ca2 [PR build]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

NSUrlSessionHandler stream ReadAsync reports caller cancellation as TimeoutException on iOS/macOS

4 participants