xamarin / xamarin-macios

.NET for iOS, Mac Catalyst, macOS, and tvOS provide open-source bindings of the Apple SDKs for use with .NET managed languages such as C#
Other
2.44k stars 508 forks source link

NSURLErrorDomain 1005 - The network connection was lost - debug in physical device #8500

Closed Dimigergo closed 4 years ago

Dimigergo commented 4 years ago

Steps to Reproduce

  1. Start any HTTPS GET or POST request with physical iPhone 5S (12.4.4) or iPad (13.3) device. The iOS Simulator works fine, and the Xamarin.Android (Simulator and physical device) project is also works fine. Only occured in iOS physical devices.

My HttpClient code in Xamarin.iOS project:

private bool NSUrlSessionHandlerTrustOverride(NSUrlSessionHandler sender, SecTrust trust)
{
    return true;
}

public HttpClient PrepareHttpClient(HttpMessageHandler handler)
{
    return new HttpClient(
        new NSUrlSessionHandler(NSUrlSessionConfiguration.DefaultSessionConfiguration)
        {
            TrustOverride = this.NSUrlSessionHandlerTrustOverride,
            AllowsCellularAccess = true
        });
}

Expected Behavior

The connection is established.

Actual Behavior

Exception Type: System.Net.Http.HttpRequestException (Code: HResult: -2146233088)
Exception Message: The network connection was lost.
  at System.Net.Http.NSUrlSessionHandler.SendAsync (System.Net.Http.HttpRequestMessage request, System.Threading.CancellationToken cancellationToken) [0x001d4] in /Library/Frameworks/Xamarin.iOS.framework/Versions/13.16.0.13/src/Xamarin.iOS/Foundation/NSUrlSessionHandler.cs:462 
  at System.Net.Http.HttpClient.FinishSendAsyncBuffered (System.Threading.Tasks.Task`1[TResult] sendTask, System.Net.Http.HttpRequestMessage request, System.Threading.CancellationTokenSource cts, System.Boolean disposeCts) [0x0017e] in /Library/Frameworks/Xamarin.iOS.framework/Versions/Current/src/Xamarin.iOS/external/corefx/src/System.Net.Http/src/System/Net/Http/HttpClient.cs:506 
  at MLR.MoLaRi.UI.WebVUK.Mobile.MM.WebVUKApi.CApiCallHelper.PostRequest[T] (MLR.Components.FuncLog.CFL fL, System.String method, MLR.MoLaRi.UI.WebVUK.Shared.Api.IMethodParameters parameters) [0x000bb] in <2f969a191e1d477da78f5fcaf446e542>:0 
InnerException Type: Foundation.NSErrorException (Code: HResult: -2146233088)

InnerException Message: Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo={_kCFStreamErrorCodeKey=54, NSUnderlyingError=0x283273b10 {Error Domain=kCFErrorDomainCFNetwork Code=-1005 "(null)" UserInfo={_kCFStreamErrorCodeKey=54, _kCFStreamErrorDomainKey=1}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <3EC77204-455C-48B5-BFAB-6E2CB187DC47>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalDataTask <3EC77204-455C-48B5-BFAB-6E2CB187DC47>.<1>"
), NSLocalizedDescription=The network connection was lost., NSErrorFailingURLStringKey=https://10.42.42.8:444/Home/MobileLogin, NSErrorFailingURLKey=https://10.42.42.8:444/Home/MobileLogin, _kCFStreamErrorDomainKey=1}

On the server side with wireshark only find these five rows for this connection. wireshark1

The server immediately response with reset -> this is the reason with the 1005 error code. wireshark1

In the server no IIS log in %SystemDrive%\inetpub\logs\LogFiles, but when I test it with simulator or Android device the IIS logs are written fine.

Environment

Microsoft Visual Studio Professional 2019
Version 16.5.4
VisualStudio.16.Release/16.5.4+30011.22
Microsoft .NET Framework
Version 4.8.03752

Installed Version: Professional

Visual C++ 2019   00435-60000-00000-AA607
Microsoft Visual C++ 2019

.NET Portability Analyzer   1.1.10808.0
Evaluates portability of assemblies across .NET platforms.

ASP.NET and Web Tools 2019   16.5.236.49856
ASP.NET and Web Tools 2019

ASP.NET Web Frameworks and Tools 2019   16.5.236.49856
For additional information, visit https://www.asp.net/

Azure App Service Tools v3.0.0   16.5.236.49856
Azure App Service Tools v3.0.0

Azure Functions and Web Jobs Tools   16.5.236.49856
Azure Functions and Web Jobs Tools

C# Tools   3.5.0-beta4-20153-05+20b9af913f1b8ce0a62f72bea9e75e4aa3cf6b0e
C# components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Common Azure Tools   1.10
Provides common services for use by Azure Mobile Services and Microsoft Azure Tools.

Extensibility Message Bus   1.2.0 (d16-2@8b56e20)
Provides common messaging-based MEF services for loosely coupled Visual Studio extension components communication and integration.

GitHub.VisualStudio   2.11.96.24460
A Visual Studio Extension that brings the GitHub Flow into Visual Studio.

IntelliCode Extension   1.0
IntelliCode Visual Studio Extension Detailed Info

Language   1.0
Generate language cs file

License Header Manager   3.0.3
An extension for automatically inserting license information file headers.

Microsoft Azure Tools   2.9
Microsoft Azure Tools for Microsoft Visual Studio 2019 - v2.9.30207.1

Microsoft Continuous Delivery Tools for Visual Studio   0.4
Simplifying the configuration of Azure DevOps pipelines from within the Visual Studio IDE.

Microsoft JVM Debugger   1.0
Provides support for connecting the Visual Studio debugger to JDWP compatible Java Virtual Machines

Microsoft Library Manager   2.1.25+gdacdb9b7a1
Install client-side libraries easily to any web project

Microsoft MI-Based Debugger   1.0
Provides support for connecting Visual Studio to MI compatible debuggers

Microsoft Visual C++ Wizards   1.0
Microsoft Visual C++ Wizards

Microsoft Visual Studio Tools for Containers   1.1
Develop, run, validate your ASP.NET Core applications in the target environment. F5 your application directly into a container with debugging, or CTRL + F5 to edit & refresh your app without having to rebuild the container.

Microsoft Visual Studio VC Package   1.0
Microsoft Visual Studio VC Package

Mono Debugging for Visual Studio   16.5.514 (c4f36a9)
Support for debugging Mono processes with Visual Studio.

Node.js Tools   1.5.20122.1 Commit Hash:d74ac73e6e22f9e009948cab4535a26194b19b1b
Adds support for developing and debugging Node.js apps in Visual Studio

NuGet Package Manager   5.5.0
NuGet Package Manager in Visual Studio. For more information about NuGet, visit https://docs.nuget.org/

PHP Tools for Visual Studio   1.32.11706.2019
PHP Tools extend Visual Studio with a set of features to build PHP applications more efficiently. It makes code more readable, easier to navigate, and clean.

ProjectServicesPackage Extension   1.0
ProjectServicesPackage Visual Studio Extension Detailed Info

SQL Server Data Tools   16.0.62003.05170
Microsoft SQL Server Data Tools

StopOnFirstBuildError   1.0
StopOnFirstBuildError

Stops a solution build immediately after a project has failed to build.

http://tech.einaregilsson.com/2011/01/06/stop-build-on-first-error-in-visual-studio-2010/

Test Adapter for Boost.Test   1.0
Enables Visual Studio's testing tools with unit tests written for Boost.Test.  The use terms and Third Party Notices are available in the extension installation directory.

Test Adapter for Google Test   1.0
Enables Visual Studio's testing tools with unit tests written for Google Test.  The use terms and Third Party Notices are available in the extension installation directory.

TypeScript Tools   16.0.20225.2001
TypeScript Tools for Microsoft Visual Studio

Visual Basic Tools   3.5.0-beta4-20153-05+20b9af913f1b8ce0a62f72bea9e75e4aa3cf6b0e
Visual Basic components used in the IDE. Depending on your project type and settings, a different version of the compiler may be used.

Visual C++ for Linux Development   1.0.9.29814
Visual C++ for Linux Development

Visual F# Tools 10.8.0.0 for F# 4.7   16.5.0-beta.20181.6+85af456066acd4e76d2bc7821b44a325e46f2fca
Microsoft Visual F# Tools 10.8.0.0 for F# 4.7

Visual Studio Code Debug Adapter Host Package   1.0
Interop layer for hosting Visual Studio Code debug adapters in Visual Studio

Visual Studio Container Tools Extensions (Preview)   1.0
View, manage, and diagnose containers within Visual Studio.

Visual Studio Tools for CMake   1.0
Visual Studio Tools for CMake

Visual Studio Tools for Containers   1.0
Visual Studio Tools for Containers

VisualStudio.DeviceLog   1.0
Information about my package

VisualStudio.Foo   1.0
Information about my package

VisualStudio.Mac   1.0
Mac Extension for Visual Studio

WiX Toolset Visual Studio Extension   1.0.0.4
WiX Toolset Visual Studio Extension version 1.0.0.4
Copyright (c) .NET Foundation and contributors. All rights reserved.

Xamarin   16.5.000.533 (d16-5@9152e1b)
Visual Studio extension to enable development for Xamarin.iOS and Xamarin.Android.

Xamarin Designer   16.5.0.470 (remotes/origin/d16-5@681de3fd6)
Visual Studio extension to enable Xamarin Designer tools in Visual Studio.

Xamarin Templates   16.5.49 (0904f41)
Templates for building iOS, Android, and Windows apps with Xamarin and Xamarin.Forms.

Xamarin.Android SDK   10.2.0.100 (d16-5/988c811)
Xamarin.Android Reference Assemblies and MSBuild support.
    Mono: c0c5c78
    Java.Interop: xamarin/java.interop/d16-5@fc18c54
    ProGuard: xamarin/proguard/master@905836d
    SQLite: xamarin/sqlite/3.28.0@46204c4
    Xamarin.Android Tools: xamarin/xamarin-android-tools/d16-5@9f4ed4b

Xamarin.iOS and Xamarin.Mac SDK   13.16.0.13 (b75deaf)
Xamarin.iOS and Xamarin.Mac Reference Assemblies and MSBuild support.
chamons commented 4 years ago

@mandel-macaque - Can you triage this? It's deep networking stuff.

mandel-macaque commented 4 years ago

@Dimigergo I believe I know what is the root cause of the issue. But to confirm it I need some extra information. With the error and the wireshark screenshot I believe that the following is happening:

  1. Launch app, device is connected to WiFi.
  2. Perform request, device is connected to WiFi.
  3. Device switched to Cell, either done intentionally by the user, or by WiFi assist.
  4. Connection changes, you get the crash.

The main issue is that due to the configuration you are using, you have not set the multiPath property. Since you have not changed that, the default behaviour is that of NSURLSessionMultipathServiceTypeNone:

The default service type indicating that Multipath TCP should not be used.

There are two different ways to solve this:

  1. Create the configuration using the multipath property to NSURLSessionMultipathServiceTypeHandover
  2. Update you code taking into account that the connection can be lost when you switch to cell.

First approach is at the moment the easiest one for you to test, simple add the property BUT you have to make sure that the server side has been configured to support multipath tcp.

For the second approach, here is some information that can be useful:

The error 'Domain=NSURLErrorDomain Code=-1005 "The network connection was lost."' means that the connection was dropped, that can happen in c# and native code, there is no relation with the bindings. If this happens, you will need to recover (catch the exception) and retry if you consider it valid. If the connection is lost you might need to create a new httpclient.

Please test the first approach and let me know how far it gets (we might have issues in the server side).

Dimigergo commented 4 years ago

@mandel-macaque Thanks for your response!

First of all, sorry, my description is not so correct. Two main thing that I forgot to mention:

My situation is the following: openVPN

  1. The first point, you mentioned: I have set the MultipathServiceType property to Handover, this caused error code 1004 (Could not connect to server).

  2. I have made it (implement when exception occures, I reinitialize (new HttpClient) the connection), but the error (1005) is still alive.

  3. I have tested it with a valid SIM card with and without wifi and the error is the same as original 1005 error.

  4. Next Friday, I can go our office and I will try the whole test without OpenVPN.

Thanks for your advices!

mandel-macaque commented 4 years ago

Ah! the vpn does make a HUGE difference. I'll start looking into the possible causes for the VPN to be dropping the connection, that is a scenario we don't usually see, so we might find something interesting in that area. Is it possible for you to provide a small example app that I can take a look at?

There is also an other route we can take a look at. Can you try with the other handlers, the fully managed one and the CFNetwork based one? If will be interesting to see if they drop the connection or not. The implementations are completely different and would helps us to debug this (if the all are loosing the connection, we know there is something fishy with the VPN, else, something is wrong with the NSUrlSession implementation).

Dimigergo commented 4 years ago

Of course, I try to implement these things and make an example next week, and share it with you.

Dimigergo commented 4 years ago

Hello!

  1. I have implemented the fully managed HttpClient in ios and its works perfect with physical device and simulator also. So for the testing, I can use this Managed HttpClient.

  2. I have tried to use CFNetwork, but both of the simulator and the device throw HttpRequestException with InnerException: The operation couldn't be completed (OSStatus error -9806.) -> I googled it, and its a SSL handshake error, but I can't figured it out why.

With server side wireshark, there isn't any tcp pack in the output.

I have tried the Environment.SetEnvironmentVariable("CFNETWORK_DIAGNOSTICS", "3", EnvironmentVariableTarget.Process); In the AppDelegate.cs after LoadApplication(new App());, but I can't find any log or Logs folder in ~/Library/Developer/CoreSimulator/Devices/[UUID]/data/Containers/Data/Application/[FOLDER_GUID]/Library

So today I can tell you only that the Managed is ok.

Thanks in advance!

Dimigergo commented 4 years ago

Hello!

I have tried it without OpenVPN and its works well.

The overall is that only NSUrlSessionHandler with OpenVPN is raise the exception, but CFNetwork was not tested.

Is there any other test what I can make for you? Thanks in advance!

mandel-macaque commented 4 years ago

Ok, so that is a 'good step forward' we do know that the issue is the interaction between OpenVPN and NSUrlSession Handler. Is there a way you can create a small sample app that tries to do a request using the OpenVPN in the wya you are using it. Once I have that and I can reproduce it I will work to get you a fix ASAP.

PS: Sorry for the delay, got side tracked to other urgent issue :/

Dimigergo commented 4 years ago

Hello! :)

Today I have started to write a sample app, and when I try out an api call with NSUrlSessionHandler it was working. So I tried out with my normal application, and the error went away.

I don't know why but now it works perfectly. I didn't update Xamarin nuget packages. The only thing that was happened in the past two weeks, that we had a power outage in our server park so every machine was rebooted.

So in this case, sorry that I have robbed your time.

Thanks for your collaboration!