Thanks for being a part of WWDC25!

How did we do? We’d love to know your thoughts on this year’s conference. Take the survey here

URLSessionWebSocketTask reports closeCode as invalid when state is completed

I am using a URLSessionWebSocketTask. When trying to receive messages while the app is backgrounded, the receive() method fails with an NSError where the domain is NSPOSIXErrorDomain and the code is ECONNABORTED. That behavior is good. However, when this happens, the URLSessionWebSocketTask reports a closeCode of invalid, which is supposed to denote that the connection is still open. However, the connection state property is reporting completed. I feel that the closeCode property should be reporting something like abnormalClosure in this case. Either way, this seems like a bug or the documentation is incorrect.

Answered by DTS Engineer in 835391022
Either way, this seems like a bug or the documentation is incorrect.

And the best path forward is to file a bug explaining your concern. The URLSession engineering team can make a determination as to what the expected behaviour is and, if necessary, route your bug to docs folks.

Please post your bug number, just for the record.

But in this case I suspect that your best path forward is actually to not use URLSession for WebSocket, but instead use Network framework. See TN3151 Choosing the right networking API.

Share and Enjoy

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

Either way, this seems like a bug or the documentation is incorrect.

And the best path forward is to file a bug explaining your concern. The URLSession engineering team can make a determination as to what the expected behaviour is and, if necessary, route your bug to docs folks.

Please post your bug number, just for the record.

But in this case I suspect that your best path forward is actually to not use URLSession for WebSocket, but instead use Network framework. See TN3151 Choosing the right networking API.

Share and Enjoy

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

FB17252543

URLSessionWebSocketTask reports closeCode as invalid when state is completed
 
 
Q