Bug 29746 - NetworkReachability TryGetFlags hangs when packet loss is 100 percent
Summary: NetworkReachability TryGetFlags hangs when packet loss is 100 percent
Alias: None
Product: iOS
Classification: Xamarin
Component: Xamarin.iOS.dll ()
Version: XI 10.12 (d15-3)
Hardware: PC Mac OS
: Normal normal
Target Milestone: Future Cycle (TBD)
Assignee: Bugzilla
Depends on:
Reported: 2015-05-05 09:50 UTC by John Miller [MSFT]
Modified: 2017-10-24 14:49 UTC (History)
10 users (show)

Tags: ac
Is this bug a regression?: No
Last known good build:

Test Case (18.65 KB, application/zip)
2015-05-05 09:50 UTC, John Miller [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 John Miller [MSFT] 2015-05-05 09:50:39 UTC
Created attachment 11068 [details]
Test Case


   NetworkReachability.TryGetFlags hangs when the network packet loss is 100%. 

**Steps to Reproduce:**

    1. Use the Network Link Conditioner to cause 100% packet loss (if you need that, http://nshipster.com/network-link-conditioner/)
    2. Run the attached sample on an iOS Simulator

**Actual Results:**

   It will hang when launching because of the IsHostReachable call of the Reachability.cs class

**Expected Results:**

   No hang, and it should return false. 

**Build Date & Platform:**

   XI 8.10
   Mono 4.0
   Xcode 6.3
   iOS 8.3

**Additional Information:**

   The code being used for the Reachability sample is

public static bool IsHostReachable(string host)
        if (string.IsNullOrEmpty(host))
            return false;

        using (var r = new NetworkReachability(host))
            NetworkReachabilityFlags flags;

            if (r.TryGetFlags(out flags)) // Hangs here
                return IsReachableWithoutRequiringConnection(flags);
        return false;
Comment 1 Sebastien Pouliot 2015-05-07 16:55:20 UTC
The call to `SCNetworkReachabilityGetFlags` takes a few seconds but it does get back and shows

2015-05-07 16:52:00.281 ReachabilityTest[90947:19574570] false
Comment 2 Sebastien Pouliot 2015-05-07 17:01:55 UTC
hmm... maybe it took more time than the simulator watchdog allowed ?

@John could you reproduce it ? is so where the call stack if you hit pause ? or does it eventually quit (watchdog) ?

Having undefined time functions calls in FinishedLaunching is a common issue [1] that can bite you back - generally seen on device (but there's now a watchdog on the simulator too).

[1] https://developer.apple.com/library/ios/qa/qa1693/_index.html
Comment 3 Marcin Kuptel 2015-07-06 10:40:39 UTC
I'm experiencing a similar issue. My app starts several image downloads. Before they are all finished, I use Network Link Conditioner to manipulate my connection so that all packets are lost. The requests that are still in progress never return.
Comment 4 John Miller [MSFT] 2015-08-12 13:45:21 UTC
I moved the code to the ViewDidLoad and it does not hang. Instead, IsHostReachable returns true, even with 100% packet loss - which seems wrong as the host is not reachable.
Comment 5 Joseph Hanna 2015-11-29 19:50:59 UTC
Hi guys,

I also have IsHostReachable returns true when I set the "Settings -> Developer -> Status -> 100% loss".
Comment 6 Michael 2016-01-08 14:14:22 UTC

Has anyone managed to confirm whether this is a defect yet?
Comment 7 kontakt 2016-04-10 23:03:43 UTC
I am experiencing exactly the same issue, inside Xamarin CrossConnectivity plugin. On iPhone 4 device it takes about 30 seconds, iOS 8 simulator about 20-30s, iOS 9 simulator about 5s. If called in FinishedLaunching, app crashes because of watchdog.
Comment 8 Alex Soto [MSFT] 2016-06-08 19:45:35 UTC
we will check if it is a bug on our side or apple is not giving us the right values
Comment 9 Prashant [MSFT] 2017-07-26 13:10:30 UTC
I can confirm using the latest Xamarin.iOS, the app no longer hangs but `IsHostReachable` returns `true`, even with 100% packet loss 

Marking this report as CONFIRMED.
Comment 10 Sebastien Pouliot 2017-10-24 14:49:03 UTC
SCNetworkReachabilityGetFlags (called from TryGetFlags) is a synchronous API [1] and should not be called in `FinishedLaunching` since the watchdog might kill the process if it takes too long.

Even with 100% packet loss iOS' SCNetworkReachabilityGetFlags returns 0 (Ok) and flags == Reachable. This _might_ be because the reachability status is cached (before being set to 100% packet loss). The internal of this code is not documented with enough details. 

However the meaning of reachable [2] does not mean a connection was done, only that the OS believe it can be done, i.e. reachable means the network configuration can be used to reach it [3]. IOW using reachability with packet loss does not work [4] like you expect.

note: The method `IsHostReachable` (mentioned in comment #9) is part of the user code (not XI).

[1] https://developer.apple.com/library/content/qa/qa1693/_index.html
[2] https://stackoverflow.com/a/728568/220643
[3] https://developer.apple.com/documentation/systemconfiguration/scnetworkreachabilityflags/kscnetworkreachabilityflagsreachable?language=objc
[4] https://stackoverflow.com/a/37190752/220643