Skip to content

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

Closed

Description

@tipa

Apple platform

macOS & iOS

Framework version

net10.0-*

Affected platform version

.NET SDK: 10.0.301

Description

NSUrlSessionHandler appears to convert caller-requested cancellation during response stream reads into a direct TimeoutException.

In the attached repro, HttpClient.Timeout is set to 5 minutes, so the request itself is not timing out. The only timeout-like event is a caller-owned CancellationTokenSource that cancels after 250 ms and is passed to Stream.ReadAsync.

Expected behavior:

  • Caller cancellation should surface as OperationCanceledException / TaskCanceledException.
  • TimeoutException should be reserved for an actual request timeout, such as HttpClient.Timeout or a handler-owned timeout.

Actual behavior:

  • NSUrlSessionHandler throws System.TimeoutException.
  • The inner exception is System.Threading.Tasks.TaskCanceledException.

For comparison, the same repro using SocketsHttpHandler surfaces TaskCanceledException, which is the behavior I would expect for caller cancellation.

This seems related to the behavior in `NSUrlSessionDataTaskStream.ReadAsync:

try {
await Task.Delay (50, cancellationToken).ConfigureAwait (false);
} catch (TaskCanceledException ex) {
// add a nicer exception for the user to catch, add the cancelation exception
// to have a decent stack
throw new TimeoutException ("The request timed out.", ex);
}

Possibly related issue: #20474

Steps to Reproduce

Please see the attached NSUrlSessionTimeoutRepro.zip project.

The repro starts a loopback HTTP server. The server sends response headers and one body byte, then stalls before sending the declared second byte. The client then calls Stream.ReadAsync(buffer, cancellationToken) with a caller-created token that cancels after 250 ms.

The output is displayed in a native text view in the app window.

Run on macOS:

dotnet run -f net10.0-macos

Run on iOS simulator:

dotnet run -f net10.0-ios -r iossimulator-arm64

Observed output

NSUrlSessionHandler: first read returned 1 byte(s): A
NSUrlSessionHandler: second read threw System.TimeoutException
NSUrlSessionHandler: message: The request timed out.
NSUrlSessionHandler: inner: System.Threading.Tasks.TaskCanceledException: A task was canceled.

SocketsHttpHandler: first read returned 1 byte(s): A
SocketsHttpHandler: second read threw System.Threading.Tasks.TaskCanceledException
SocketsHttpHandler: message: The operation was canceled.

Did you find any workaround?

In app code, I currently need to treat TimeoutException as a possible network/cancellation failure when using NSUrlSessionHandler.

That is not ideal because it makes caller cancellation indistinguishable from a real timeout.

Activity

  1. added
    networking
    on Jun 16, 2026
  2. rolfbjarne commented on Jun 16, 2026

    @rolfbjarne
    Member

    Thanks for the report! I'll have a look.

  3. added
    bug
    on Jun 16, 2026
  4. added this to the Future milestone on Jun 16, 2026
  5. added a commit that references this issue on Jun 24, 2026
    658bdd0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugnetworking

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions