Bug 55709 - HttpClient (w/ NSUrlSessionHandler) hangs indefinitely when canceled
Summary: HttpClient (w/ NSUrlSessionHandler) hangs indefinitely when canceled
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: XI 10.4 (C9)
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: Future Cycle (TBD)
Assignee: Manuel de la Peña [MSFT]
Depends on:
Reported: 2017-04-28 00:49 UTC by Caleb Clarke
Modified: 2018-02-13 13:06 UTC (History)
5 users (show)

Is this bug a regression?: ---
Last known good build:

Sample project. Use with slow network, tap button and watch output window. (27.93 KB, application/zip)
2017-04-28 00:49 UTC, Caleb Clarke

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.

Please create a new report on Developer Community or GitHub with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:

Description Caleb Clarke 2017-04-28 00:49:59 UTC
Created attachment 21864 [details]
Sample project. Use with slow network, tap button and watch output window.

This does seem very similar to https://bugzilla.xamarin.com/show_bug.cgi?id=51423 , however I have tested with the latest Stable and Alpha as of 4/27/17 and it still reproduces. Also, in this case we're explicitly cancelling rather than disconnecting the network.

When calling HttpClient.SendAsync with a CancellationToken, cancelling that token after a certain time "causes no response and HttpClient will hang indefinitely (no exception or response back)" (quoted from the other bug because the resulting symptom is the same).

Still observed with this Alpha build, suggesting that the fix for the other bug did not fix this:
Version: (Visual Studio Enterprise)
Hash: 675645b0
Branch: d15-2
Build date: 2017-04-22 14:32:46-0400

Sample project attached. Uses CancelAfter to simulate a user canceling the request after a certain time while the request is in-progress. Use "Network Link Conditioner" or a proxy (e.g. Fiddler) to simulate slow network so that there is time for the cancellation to occur, then tap the Button and watch the debugger output.

(there is only a single function of interest in the ViewController of the project... probably could have done this as a unit test project or Workbook, but this seems more useful for debugging?)

This has only been seen with the current Xamarin NSUrlSessionHandler.
- The Managed handler cancels exactly as expected.
- CFNetwork "stops" when canceled but reports success? (I didn't look further into that, maybe examining the response object makes clear that it was canceled?)
- ModernHttpClient simply ignores cancellation attempts (not sure if that's better or worse....)

I believe that this specifically happens if the token is canceled while the response content is being read (via HttpClient.SendAsyncWorker's call to LoadIntoBufferAsync if the HttpCompletionOption is ResponseContentRead). Note however that I have not actually debugged into HttpClient or NSUrlSessionHandler. I believe that LoadIntoBufferAsync is related because I am unable to reproduce the hang if the token is canceled very quickly or if HttpCompletionOption is ResponseHeadersRead. (also this explains the ModernHttpClient behavior, as that simply passes CancellationToken.None into its Content)

(I tried using ResponseHeadersRead and calling LoadIntoBufferAsync separately to isolate the issue, however that winds up ignoring cancellation because HttpClient wraps the provided CancellationToken in a LinkedTokenSource which it declares in a "using" within SendAsyncWorker, so after SendAsync returns the original CancellationToken is no longer attached to the Content because the LinkedTokenSource was disposed...)
Comment 1 Manuel de la Peña [MSFT] 2017-04-28 09:11:24 UTC

I have been working lately in the HttpClient issues, let me take a look and I will get back to you as soon as possible. I'll test all of the handlers to make sure that we do have the expected behaviour which AFAIK should be the same as the one exposed by the "Managed" implementation.
Comment 2 Manuel de la Peña [MSFT] 2018-02-13 13:06:35 UTC
Re-testing the sample apple I have the following results:

Managed handler:

Starting request
Thread started: <Thread Pool> #4
Thread started: <Thread Pool> #5
Thread started: <Thread Pool> #6
Thread started: <Thread Pool> #7
Thread started: <Thread Pool> #8
Thread started: <Thread Pool> #9


Starting request
Thread started: <Thread Pool> #4
Thread started: <Thread Pool> #5
Thread started: <Thread Pool> #6
Thread started: <Thread Pool> #7


Starting request
Thread started:  #4
Thread started: <Thread Pool> #5
Thread started: <Thread Pool> #6
Thread started: <Thread Pool> #7
Thread started: <Thread Pool> #8

Closing the bug as we cannot longer reproduce the issue with the following environment:

=== Visual Studio Community 2017 for Mac (Preview) ===

Version Preview - Internal Dogfood (7.4 build 997)
Installation UUID: 8d12e55e-3489-463f-ac52-8cb4573c5a81
	Mono (2017-10/38502063cac) (64-bit)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 508000123

=== NuGet ===


=== .NET Core ===

Runtime: /usr/local/share/dotnet/dotnet
Runtime Versions:
SDK: /usr/local/share/dotnet/sdk/2.0.0/Sdks
SDK Versions:
MSBuild SDKs: /Library/Frameworks/Mono.framework/Versions/5.8.0/lib/mono/msbuild/15.0/bin/Sdks

=== Xamarin.Profiler ===

Version: 1.6.1
Location: /Applications/Xamarin Profiler.app/Contents/MacOS/Xamarin Profiler

=== Xamarin.Android ===

Version: (Visual Studio Community)
Android SDK: /Users/mandel/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		4.0.3 (API level 15)
		4.3   (API level 18)
		4.4   (API level 19)
		5.0   (API level 21)
		6.0   (API level 23)
		7.0   (API level 24)
		7.1   (API level 25)

SDK Tools Version: 25.2.5
SDK Platform Tools Version: 25.0.4
SDK Build Tools Version: 25.0.1

Java SDK: /usr
java version "1.8.0_101"
Java(TM) SE Runtime Environment (build 1.8.0_101-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.101-b13, mixed mode)

Android Designer EPL code available here:

=== Xamarin Inspector ===

Version: 1.4.0
Hash: b3f92f9
Branch: master
Build date: Fri, 19 Jan 2018 22:00:34 GMT
Client compatibility: 1

=== Apple Developer Tools ===

Xcode 9.2 (13768)
Build 9C34b

=== Xamarin.Mac ===

Version: (Visual Studio Community)

=== Xamarin.iOS ===

Version: (Visual Studio Community)
Hash: da7fbcb2
Branch: master
Build date: 2018-02-13 11:52:52+0100

=== Build Information ===

Release ID: 704000997
Git revision: ad2eb0e142d6c48fc59fb56baecc4588a303840e
Build date: 2018-02-07 08:33:55-05
Xamarin addins: 658075771858d324b05b1520eaf1544369414eef
Build lane: monodevelop-lion-dogfood-vNext

=== Operating System ===

Mac OS X 10.13.3
Darwin 17.4.0 Darwin Kernel Version 17.4.0
    Sun Dec 17 09:19:54 PST 2017
    root:xnu-4570.41.2~1/RELEASE_X86_64 x86_64

=== Enabled user installed addins ===

WakaTime 3.0.2
Internet of Things (IoT) development (Preview) 7.1