Reachability using hostname incorrectly reporting Not Reachable after device is locked/sleeps, and doesn't recover automatically

Number:rdar://40291350 Date Originated:May 16 2018
Status:Closed Resolved:May 18 2018
Product:iOS + SDK Product Version:iOS 11.3.1
Classification:Duplicate of 35688315 Reproducible:Sometimes
SystemConfiguration Framework

When Reachability (SCNetworkReachability) is configured with a hostname and callback monitoring is enabled, and the app goes to background, sometimes the reachability callback will report Not Reachable when the device is woken up, and doesn't recover.

Steps to Reproduce:
1 - ensure you have WiFi enabled on the device
2 - configure SCNetworkReachability using a hostname, e.g.
3 - enable monitoring of SCNetworkReachability via callback
4 - lock the device (manually, or waiting for auto lock)
5 - wait some seconds with the device locked (maybe a minute or two even)
6 - lift the device (to force it to wake up temporarily)
7 - repeat steps 5-6 until some `dnssd_clientstub` or `nw_resolver_create_dns_service_locked` logs appear and/or the reachability fires with empty flags (not reachable). If not able to reproduce, start from step 4, or wait some more time between waking the device by lifting it.
8 - the reachability will report Not reachable, when the WiFi connection is still active, and won't recover automatically

- Steps 2 and 3 are already done on the provided sample app `ReachabilityHostnameBGBug`.
- relevant app on the sysdiagnose is `ReachabilityHostnameBGBug` (test project, also attached).

My suspicion is that this is some problem with the DNS background service interfering with the Reachability (revealed by the `dnssd_clientstub`and `nw_resolver_create_dns_service_locked` logs), because this issue only seems to manifest itself when using a hostname to configure the Reachability. If one uses an address (e.g., the issue doesn't seem to happen (at least I wasn't able to).

Expected Results:
Reachability should always report the correct reachability state, according to the device/network's actual state.

Actual Results:
Reachability incorrectly reports Not Reachable, even though the WiFi connection has always been active and working

iPhone 6s (MKQJ2B/A), iOS 11.3 (15E216)
iPhone X (MQAC2QL/A), iOS 11.3.1 (15E302)
(probably others)

Xcode 9.3.1 (9E501), Swift 4.1
macOS 10.13.4


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!