Simulator unable to connect to localhost, working fine when opened directly in laptop

I am trying to connect to localhost:8081 from simulator, but it is unable to connect with following logs:

info	12:07:49.167248+0530	com.apple.WebKit.Networking	nw_resolver_host_resolve_callback [C8.1] flags=0x40000003 ifindex=0 error=NoSuchRecord(-65554) hostname=localhost. addr=IN6ADDR_ANY ttl=60

info	12:07:49.167310+0530	com.apple.WebKit.Networking	nw_resolver_host_resolve_callback [C8.1] flags=0x40000002 ifindex=0 error=NoSuchRecord(-65554) hostname=localhost. addr=INADDR_ANY ttl=108002

Macos 14.6.1

iOS simulator version 17.5

Som observations

  1. localhost:8081 does not load on simulator but 0.0.0.0:8081 loads fine, also 127.0.0.0:8081 loads fine on simulator.
  2. My laptop is a managed device with network filter
  3. Switching network sometimes fixes the issue. Restarting laptop sometimes fixes the issue.
  4. localhost:8081 opens find on laptop, but not on simulator.

Contents of my laptop's /etc/hosts:

##
# Host Database
#
# localhost is used to configure the loopback interface
# when the system is booting.  Do not change this entry.
##
127.0.0.1	localhost
255.255.255.255	broadcasthost
::1             localhost

I also see following in simulator error logs when issue occurs:

error	16:04:42.624021+0530	com.apple.WebKit.Networking	WebContent[7071]: dlsym cannot find symbol WKBundleInitialize in CFBundle 0x104888460 </Library/Developer/CoreSimulator/Volumes/iOS_21C62/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 17.2.simruntime/Contents/Resources/RuntimeRoot/System/Cryptexes/OS/System/Library/PrivateFrameworks/MobileSafari.framework/PlugIns/Safari.wkbundle> (framework, loaded): dlsym(0xf34511, WKBundleInitialize): symbol not found
error	16:04:42.624078+0530	com.apple.WebKit.Networking	WebContent[7071]: dlsym cannot find symbol WKBundleAdditionalClassesForParameterCoder in CFBundle 0x104888460 </Library/Developer/CoreSimulator/Volumes/iOS_21C62/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS 17.2.simruntime/Contents/Resources/RuntimeRoot/System/Cryptexes/OS/System/Library/PrivateFrameworks/MobileSafari.framework/PlugIns/Safari.wkbundle> (framework, loaded): dlsym(0xf34511, WKBundleAdditionalClassesForParameterCoder): symbol not found
error	16:04:42.644778+0530	com.apple.WebKit.Networking	Connection 56: received failure notification
error	16:04:42.644804+0530	com.apple.WebKit.Networking	Connection 56: failed to connect 12:8, reason -1
error	16:04:42.644818+0530	com.apple.WebKit.Networking	Connection 56: encountered error(12:8)
error	16:04:42.645241+0530	com.apple.WebKit.Networking	Task <6243E5E4-5B34-460B-B73A-7E12778ED6D3>.<58> HTTP load failed, 0/0 bytes (error code: -1003 [12:8])
error	16:04:42.645405+0530	com.apple.WebKit.Networking	Task <6243E5E4-5B34-460B-B73A-7E12778ED6D3>.<58> finished with error [-1003] <private>
My laptop is a managed device with network filter

Well, that’s the first thing to rule out. If you temporarily disable the network filter, does this work?

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

With additional help, I disabled the network filter. The issue still persists.

What other things I could try? Additional logging or tooling I can use to narrow down the places where the resolution is dropped?

I'd suggest filing a feedback report with a sysdiagnose collected in state and ideally a tcpdump collecting the network traffic when this occured.

I'd suggest filing a feedback report with a sysdiagnose collected in state and ideally a tcpdump collecting the network traffic when this occured.

Yep.

Also, my reading of your the logs you posted upthread suggest that you’re running Safari in the simulator. Is that right?

If so, and presuming that you have Xcode installed, can you write a small test project that use URLSession to fetch a resource from your server? I’d like to see if this is specific to Safari, or something that affects all apps in the simulator.

If you need help create a small test project for this, lemme know. Even if you’re not super familiar with Xcode, it should still be feasible to run this test.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Couple of observations:

  1. Nothing shows up in tcpdump when the issue occurs. When there is no issue, I see activity on tcpdump.
  2. I have seen this issue in my mobile app that uses URLSession, that is why I also verified in Safari to be sure, before raising this issue.
  3. Let me send a send a sysdiagnose report via Feedback Assistant

I have sent a sysdiagnose report via Feedback assistant no. FB15379770

I upgraded macos, xcode and iOS runtime. I updated to Xcode 16, iOS 18 simulator runtime and macOS 15,

I made a sample app and I see following error when trying to connect simple http server :

Task <8DD928B9-BF28-422A-A96C-E79E8569DB92>.<1> HTTP load failed, 412/0 bytes (error code: -1005 [4:-4])
Task <8DD928B9-BF28-422A-A96C-E79E8569DB92>.<1> finished with error [-1005] Error Domain=NSURLErrorDomain Code=-1005 "The network connection was lost." UserInfo={_kCFStreamErrorCodeKey=-4, NSUnderlyingError=0x600000c88c60 {Error Domain=kCFErrorDomainCFNetwork Code=-1005 "(null)" UserInfo={NSErrorPeerAddressKey=<CFData 0x600002154500 [0x1e007f658]>{length = 16, capacity = 16, bytes = 0x10021f407f0000010000000000000000}, _kCFStreamErrorCodeKey=-4, _kCFStreamErrorDomainKey=4}}, _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <8DD928B9-BF28-422A-A96C-E79E8569DB92>.<1>, _NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalDataTask <8DD928B9-BF28-422A-A96C-E79E8569DB92>.<1>"
), NSLocalizedDescription=The network connection was lost., NSErrorFailingURLStringKey=http://localhost:8000/, NSErrorFailingURLKey=http://localhost:8000/, _kCFStreamErrorDomainKey=4}

Also added Sysdiagnose via feedback assistant no. https://feedbackassistant.apple.com/feedback/15379770 Let me know if any additional information is needed.

Hmmm, there’s definitely something weird going on in your setup. Just for grins I tried this here in my office. Specifically:

  1. I created a new test project.

  2. I added a Test button that runs this code:

    Task {
        do {
            print("will run task")
            let url = URL(string: "http://localhost:8081")!
            let request = URLRequest(url: url, cachePolicy: .reloadIgnoringLocalCacheData, timeoutInterval: 60.0)
            let (data, response) = try await URLSession.shared.data(for: request)
            let httpResponse = response as! HTTPURLResponse
            print("did run task, status: \(httpResponse.statusCode), bytes: \(data.count)")
        } catch let error as NSError {
            print("did not run task, error: \(error.domain) / \(error.code)")
        }
    }
    
  3. I ran the app in the simulator.

  4. In Terminal, I ran this command:

    % nc -l 8081
    
  5. In the app in the simulator, I clicked by Test button.

In Terminal I saw this:

% nc -l 8081
GET / HTTP/1.1
Host: localhost:8081
Accept: */*
Accept-Language: en-GB,en;q=0.9
Connection: keep-alive
Accept-Encoding: gzip, deflate
User-Agent: Test762849/1 CFNetwork/1568.100.1 Darwin/23.6.0

^C

So URLSession is quite happy to make localhost connections in my environment.

I tried this on two different machines:

  • My main work machine, running Xcode 16.0 on macOS 14.7 targeting the iPhone 16.0 / iOS 18.0 simulator.

  • A ‘victim’ Mac, running Xcode 16.0 on macOS 15.0 targeting the iPhone 16.0 / iOS 18.0 simulator.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Simulator unable to connect to localhost, working fine when opened directly in laptop
 
 
Q