'Bad file descriptor' error from URL session data task on Apple Watch?

I'm working on an independent watchOS app and I'm testing on the Apple Watch Ultra simulator as well as a couple of real Apple Watch Ultra devices (both have active cellular subscriptions on AT&T, are within 3 feet of their paired iPhones and connected to WiFi, as well).

My app has an application delegate which implements the applicationDidFinishLaunching() method and in that method, I register with APNs for remote notifications. When I receive the token in didRegisterForRemoteNotifications(withDeviceToken:), I send the token on to the server that is going to send notifications to the app.

When I test this code in the Apple Watch Ultra simulator, it works 100% of the time. When I test the same code on a real Apple Watch Ultra, about 70% of the time, I get the following error message in the Xcode debug console:

2023-05-12 08:32:30.779560-0400 Watch App Prototype[569:586139] PDTask <5110B87C-28D7-48C9-9C68-121C7728FF68>.<2> finished with error [9] Error Domain=NSPOSIXErrorDomain Code=9 "Bad file descriptor" UserInfo={_kCFStreamErrorCodeKey=9, _kCFStreamErrorDomainKey=1, _NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalDataPDTask <5110B87C-28D7-48C9-9C68-121C7728FF68>.<2>",
    "LocalDataTask <5110B87C-28D7-48C9-9C68-121C7728FF68>.<2>"
), _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataPDTask <5110B87C-28D7-48C9-9C68-121C7728FF68>.<2>}
2023-05-12 08:32:30.780401-0400 Watch App Prototype[569:586139] Task <5110B87C-28D7-48C9-9C68-121C7728FF68>.<2> finished with error [9] Error Domain=NSPOSIXErrorDomain Code=9 "Bad file descriptor" UserInfo={_kCFStreamErrorCodeKey=9, _kCFStreamErrorDomainKey=1, _NSURLErrorRelatedURLSessionTaskErrorKey=(
    "LocalDataTask <5110B87C-28D7-48C9-9C68-121C7728FF68>.<2>",
    "LocalDataPDTask <5110B87C-28D7-48C9-9C68-121C7728FF68>.<2>",
    "LocalDataTask <5110B87C-28D7-48C9-9C68-121C7728FF68>.<2>"
), _NSURLErrorFailingURLSessionTaskErrorKey=LocalDataTask <5110B87C-28D7-48C9-9C68-121C7728FF68>.<2>}
The operation couldn’t be completed. Bad file descriptor

I ran across a post from someone else about the same problem on StackOverflow.

I'm not sure what to make of this error but here's the code that I'm using to send the URL request:

func perform(_ urlRequest: URLRequest) async throws -> Data {
    let (data, response) = try await urlSession.data(for: urlRequest)
    guard let httpResponse = response as? HTTPURLResponse else {
        throw NetworkError.serverSideError(response: nil)
    }        
    guard (200...299).contains(httpResponse.statusCode) else {
        throw NetworkError.serverSideError(response: httpResponse)
    }
    return data
}

For what it's worth, urlSession is the shared URLSession instance. Again, this error never occurs in the simulator but it happens on both of the Apple Watch Ultra devices and I would say it occurs about 70% of the time. Can anyone help me understand this error message?

Post not yet marked as solved Up vote post of bmt22033 Down vote post of bmt22033
1k views

Replies

I'm hunting the same bug. Didn't see it until recent versions of watchOS 9. I'll be sure to post something here if I figure it out.

I submitted feedback to Apple about this issue. They asked me for a sysdiagnose from the watch and the phone. I provided that and they said the sysdiagnose from the watch appeared to be 'incomplete' and should be a few hundred megabytes (the one I submitted was ~50 megabytes). They said I should try again and if the resulting sysdiagnose still wasn't the expected size, I should try taking the sysdiagnose using a command line and using the “-L” parameter. I wasn't able to figure out how to execute the sysdiagnose from the command line on the Mac to gather data from the watch but I followed the directions specified in https://download.developer.apple.com/iOS/watchOS_Logs/sysdiagnose_Logging_Instructions.pdf and resubmitted another sysdiagnose. A couple of days later, the feedback ticket resolution was updated to "Investigation complete - Works as currently designed" with no other information. Meanwhile, I continue to experience this issue.

I experience the same issue (no solution found yet).

As extra context in my case I use Keychain to sync an OAuth token between my iOS and watchOS apps.