Okay, I submitted an enhancement request to provide a high-level ping API: FB11989640
AFAICT CFSocketInvalidate is fundamentally broken.
I'm guessing from context (since you are saying that it should work and did not crash for you) that you mean "is NOT fundamentally broken"?
The crash is not something that happens with every use of the API. I do not understand the rhyme or reason behind it. I have been able to reproduce it but not in any reliable or consistent way, I just run through the code that uses it over and over again and sometimes it will crash.
To give you a better picture of the crash, this is an enterprise kiosk application running in stores for customers to use and is running on ~5600 iPads, of which ~2000 are on iOS 16. We are seeing over 200 crashes per day from this same CFSocket stack trace inside CFSocketInvalidate. It is crashing ONLY on the iOS 16 iPads. There have been 0 instances of this crash on the other iPads that are mostly on iOS 15 with a few on iOS 14 and iOS 13. The crashes are split between our 3 different usages of CFSockets. The one that is the focus of this post is the SimplePing usage. We also use CFSockets to do socket communication with a card reader device and we are transitioning this use case to use NWConnection instead. The final usage is inside of GCDAsyncSocket inside a logging library which gets used for some logging to Splunk, we have not identified how we will replace that one yet. I only list these other usages in case their presence somehow informs the issue but I will restate that this code with all 3 of these usages does not crash prior to iOS 16 and we have been using them years.
I have attached a crash report from one of the crashes using SimplePing.
crash_report.txt
Also submitted a feedback report for this CFSocket crash: FB11989660