Bug 45383 - After Airplane Mode turned off, web request fails for a couple of minutes with a NameResolutionFailure
Summary: After Airplane Mode turned off, web request fails for a couple of minutes wit...
Status: RESOLVED DUPLICATE of bug 45761
Alias: None
Product: Android
Classification: Xamarin
Component: General ()
Version: 7.0 (C8)
Hardware: Macintosh Mac OS
: Highest normal
Target Milestone: 7.1 (C9)
Assignee: Marek Habersack
Depends on:
Reported: 2016-10-13 21:39 UTC by Jon Goldberger [MSFT]
Modified: 2016-10-20 21:17 UTC (History)
6 users (show)

Tags: XATriaged
Is this bug a regression?: Yes
Last known good build:

Test Project (50.31 KB, application/zip)
2016-10-13 21:39 UTC, Jon Goldberger [MSFT]

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 Jon Goldberger [MSFT] 2016-10-13 21:39:14 UTC
Created attachment 18032 [details]
Test Project

## Description

This is a bit of an odd issue, so bear with me. 

If this sample app is launched with a network connection and the button is tapped to send a request, the request succeeds. If then Airplane mode is enabled, the request of course fails, but upon disabling Airplane mode the next request sent succeeds right away. 

In contrast, if after app launch the first request is sent with Airplane mode enabled then the request fails with a NameResolutionFailure exception for obvious reasons (no network). However if you then disable Airplane mode and send  new request, it still fails and will continue to fail with NameResolutionFailure  exception for a couple of minutes, but eventually will succeed. 

This is a regression. Using XA the above does not occur and the first request sent immediately after network connection is restored succeeds. 

## Steps to reproduce

1. Open the attached sample app.

2. Deploy to Android device or emulator. (Tested with Google emulator Nexus 5 marshmallow)

3. Stop the app from xamarin studio to detach the debugger (if you don't do this there are issues when you turn on airplane mode with the debugger and the app hangs)

4. Enable Airplane Mode (don't do this before deploying or the debugger won't be able to attach and the app won't launch)

5. Launch the app from the simulator.

6. Tap the button.

7. Button will show NameResolutionFailure message.

8. Disable Airplane mode

9. Tap the button.

Expected result: button text will display the ContentType of the response (text/HTML)

Actual result: button text still shows NameResolutionFailure exception. 

## Notes 

As stated, eventually if you keep tapping the button after step 9 above, the request will work after a couple of minutes. And again this does not happen with XA

I also tested that it was not an issue with building against Android SDK version 6.0 vs 7.0. And that the issue is not affected by which HttpClientHandler is used.  The  attached sample is using a HttpWebRequest however I have another customer who reported the same issue and they were using HttpClient and I confirmed that their test project also worked with  XA 

The only difference between a version of the app that works and the version of the app that does not is the Xamarin Android version, IOW I was able to only install  XA into the existing Cycle 8 install and then the attached test project and the other clients test project (not attached) worked as expected. 

As an additional note, if the first request is sent without Airplane mode enabled (i.e. there is network connection) then the request succeeds, of course, but if you then enable Airplane mode and send the request, it fails with a "No Network Connection" error and if you then turn off Airplane mode again, the next request succeeds right away. 

I initially I thought this was a DNS caching issue and was able to resolve in another customers test case by making sure no request was sent when the network was not available. I found some posts that Android caches bad DNS queries and thought that was the issue. But upon discovering that XA does not display this issue, I reconsidered. Also, upon further reading and investigation, I found out that the bad DNS query should only be cached for 10 seconds while the NameResolutionFailure exception persists for a couple of minutes after restoring network connection. 

## Environment

=== Xamarin Studio Enterprise ===

Version 6.1.1 (build 15)
Installation UUID: ceaba76c-db06-4fbd-b326-f69ea53c3e01
	Mono 4.6.1 (mono-4.6.0-branch-c8sr0/ef43c15) (64-bit)
	GTK+ 2.24.23 (Raleigh theme)

	Package version: 406010005

=== NuGet ===


=== Xamarin.Profiler ===

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

=== Apple Developer Tools ===

Xcode 8.0 (11246)
Build 8A218a

=== Xamarin.Android ===

Version: (Visual Studio Enterprise)
Android SDK: /Users/jongoldberger/Library/Developer/Xamarin/android-sdk-macosx
	Supported Android versions:
		4.0.3 (API level 15)
		4.1   (API level 16)
		4.2   (API level 17)
		4.3   (API level 18)
		4.4   (API level 19)
		5.0   (API level 21)
		5.1   (API level 22)
		6.0   (API level 23)
		7.0   (API level 24)

SDK Tools Version: 25.2.2
SDK Platform Tools Version: 24.0.3
SDK Build Tools Version: 24.0.3

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

Android Designer EPL code available here:

=== Xamarin Android Player ===

Version: 0.6.5
Location: /Applications/Xamarin Android Player.app

=== Xamarin.iOS ===

Version: (Visual Studio Enterprise)
Hash: ad1cd42
Branch: cycle8-sr0-xi
Build date: 2016-10-03 15:18:44-0400

=== Xamarin.Mac ===

Version: (Visual Studio Enterprise)

=== Xamarin Inspector ===

Hash: e931a52
Branch: master
Build date: Thu, 18 Aug 2016 17:46:46 GMT

=== Build Information ===

Release ID: 601010015
Git revision: fa52f02641726146e2589ed86ec4097fbe101888
Build date: 2016-09-22 08:03:02-04
Xamarin addins: 75d65712af93d54dc39ae4c42b21dfa574859fd6
Build lane: monodevelop-lion-cycle8-sr0

=== Operating System ===

Mac OS X 10.12.0
Darwin Jons-MacBook-Pro.local 16.0.0 Darwin Kernel Version 16.0.0
    Mon Aug 29 17:56:20 PDT 2016
    root:xnu-3789.1.32~3/RELEASE_X86_64 x86_64

=== Enabled user installed addins ===

Xamarin Inspector
Comment 2 Jason DeBoever 2016-10-20 17:47:44 UTC
I have a customer saying this is also reproducible on iOS.  Both are using native handlers for HttpClient
Comment 3 Jon Goldberger [MSFT] 2016-10-20 18:49:54 UTC

Upon further testing, this is an issue in mono. I could reproduce this issue in a Xamarin iOS app as well as a Mono console app. I will file new bug reports and link back to this one.
Comment 4 Jon Goldberger [MSFT] 2016-10-20 19:50:34 UTC

*** This bug has been marked as a duplicate of bug 45761 ***
Comment 5 Jon Goldberger [MSFT] 2016-10-20 20:06:27 UTC
The workaround for this would seem to be to test for network connectivity before sending your request, and to use a different url to test for connectivity than the one you need to send requests to.

You can use the Connectivity plugin for this: https://www.nuget.org/packages/Xam.Plugin.Connectivity/
Comment 6 NMackay 2016-10-20 21:17:31 UTC
That is not a workaround that is acceptable for an enterprise customer.