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 data service (AT&T), are within 3 feet of their paired iPhones and connected to WiFi, as well).
My app registers with APNs to receive remote notifications. When I receive the token (in didRegisterForRemoteNotifications(withDeviceToken:)
), I create a URLSession data task to send it to the server that will generate and send the notifications.
This code works 100% of the time on the Apple Watch Ultra simulator but on a real Apple Watch Ultra, about 2/3 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 this post which describes what appears to be the same/similar problem and that person's code is obviously different than mine so I'm not sure that it's a code issue. When this error occurs, the URL request is definitely not completing.
For what it's worth, 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
}
By the way, urlSession in the above code is the URLSession shared singleton. Has anyone else ever run into this error message?