CocoaAsyncSocket icon indicating copy to clipboard operation
CocoaAsyncSocket copied to clipboard

Crash on iOS 16(20A362):CFSocketInvalidate + 132

Open so898 opened this issue 2 years ago • 2 comments

Environment: iOS 16 RC Version: 7.6.5

We get crash report like this:

0   libsystem_platform.dylib      	       0x210a5e08c _os_unfair_lock_recursive_abort + 36
1   libsystem_platform.dylib      	       0x210a58898 _os_unfair_lock_lock_slow + 280
2   CoreFoundation                	       0x1c42953ec CFSocketInvalidate + 132
3   CFNetwork                     	       0x1c54a4e24 0x1c533f000 + 1465892
4   CoreFoundation                	       0x1c41db030 CFArrayApplyFunction + 72
5   CFNetwork                     	       0x1c54829a0 0x1c533f000 + 1325472
6   CoreFoundation                	       0x1c4242d20 _CFRelease + 316
7   CoreFoundation                	       0x1c4295724 CFSocketInvalidate + 956
8   CFNetwork                     	       0x1c548f478 0x1c533f000 + 1377400
9   CoreFoundation                	       0x1c420799c _CFStreamClose + 108
10  Test                   	               0x102ca5228 -[GCDAsyncSocket closeWithError:] + 452
11  Test                   	               0x102ca582c __28-[GCDAsyncSocket disconnect]_block_invoke + 80
12  libdispatch.dylib             	       0x1cb649fdc _dispatch_client_callout + 20
13  libdispatch.dylib             	       0x1cb6599a8 _dispatch_sync_invoke_and_complete_recurse + 64
14  libdispatch.dylib             	       0x1cb659428 _dispatch_sync_f_slow + 172
15  Test                   	               0x102ca57b0 -[GCDAsyncSocket disconnect] + 164
16  Test                   	               0x102db951c -[TestSocket forceDisconnect] + 312
17  Test                   	               0x102cdfa5c -[TestSocket forceDisconnect] + 396
18  Test                   	               0x102d6b748 __27-[TestSocketManager didConnectWith:]_block_invoke + 2004
19  libdispatch.dylib             	       0x1cb6484b4 _dispatch_call_block_and_release + 32
20  libdispatch.dylib             	       0x1cb649fdc _dispatch_client_callout + 20
21  libdispatch.dylib             	       0x1cb651694 _dispatch_lane_serial_drain + 672
22  libdispatch.dylib             	       0x1cb6521e0 _dispatch_lane_invoke + 384
23  libdispatch.dylib             	       0x1cb65ce10 _dispatch_workloop_worker_thread + 652
24  libsystem_pthread.dylib       	       0x210aecdf8 _pthread_wqthread + 288
25  libsystem_pthread.dylib       	       0x210aecb98 start_wqthread + 8

I think this is a problem inside iOS 16 Core Foundation framework. The source code of new Core Foundation is not released, so I just file a bug to apple(FB11489606).

There is a similar crash report on developer forum: Here

Is there anyone has the same issue?

so898 avatar Sep 11 '22 07:09 so898

same error + 1

xiaoshaogg avatar Sep 14 '22 02:09 xiaoshaogg

same error + 1

LionWY avatar Sep 14 '22 03:09 LionWY

Is there a solution?

d6638219 avatar Sep 22 '22 07:09 d6638219

same error + 1

herman602 avatar Oct 03 '22 08:10 herman602

same error

asim-aiondigital avatar Oct 11 '22 08:10 asim-aiondigital

一个临时的解决方案 https://juejin.cn/post/7153606703399829511

xuezhulian avatar Oct 19 '22 09:10 xuezhulian

@xuezhulian 可以私聊下吗,请教一下细节,我的qq 123016764

dustturtle avatar Nov 22 '22 07:11 dustturtle

@dustturtle 有不懂的地方直接在掘金评论吧,老年人没有 qq 了

xuezhulian avatar Nov 22 '22 08:11 xuezhulian

This issue has been marked as stale, it will be closed automatically if there is no further activity.

github-actions[bot] avatar Jan 22 '23 00:01 github-actions[bot]

same error + 1

syhouse avatar Feb 08 '23 02:02 syhouse

Same error as well. I highly recommend reading the blog post above (use Google Translate if needed but the relevant code is at the bottom) https://github.com/robbiehanson/CocoaAsyncSocket/issues/803#issuecomment-1283722562

tl;dr it's definitely a bug that Apple added to CoreFoundation in iOS 16. It is possible to hack around it by modifying your socket's struct to point at a custom release function. Personally I don't feel great about that so you can either hope that Apple fixes it or stop using stuff that relies on the old CFSocket type, like GCDAsyncSocket.

silverhammermba avatar Feb 13 '23 14:02 silverhammermba

@silverhammermba You are right, but It is very costly for us to deprecate GCDAsyncSocket.

xuezhulian avatar Apr 13 '23 03:04 xuezhulian

@xuezhulian I tried applying your fix but it did not resolve the crash for me. See PR #824. Could you take a look and see if there is some mistake in my implementation?

silverhammermba avatar Apr 19 '23 18:04 silverhammermba

same error + 1

zhuozhuo avatar Jun 16 '23 10:06 zhuozhuo

@silverhammermba 需要加到这个方法里面 closeWithError

xuezhulian avatar Jul 19 '23 07:07 xuezhulian

  • (void)closeWithError:(NSError *)error { LogTrace(); NSAssert(dispatch_get_specific(IsOnSocketQueueOrTargetQueueKey), @"Must be dispatched on socketQueue");

    [self endConnectTimeout];

    if (currentRead != nil) [self endCurrentRead]; if (currentWrite != nil) [self endCurrentWrite];

    [readQueue removeAllObjects]; [writeQueue removeAllObjects];

    [preBuffer reset];

    #if TARGET_OS_IPHONE { if (readStream || writeStream) { [self removeStreamsFromRunLoop];

      	if (readStream)
      	{
      		CFReadStreamSetClient(readStream, kCFStreamEventNone, NULL, NULL);
      		CFReadStreamClose(readStream);
      		CFRelease(readStream);
      		readStream = NULL;
      	}
          if (writeStream)
          {
              if (@available(iOS 16.0, *)) {
                  struct _ks_CFStream *cfstream  = (struct _ks_CFStream *)writeStream;
                  if (isMemoryReadable(cfstream, sizeof(*cfstream))
                     && isMemoryReadable(cfstream->info, sizeof(*(cfstream->info)))
                     && isMemoryReadable(cfstream->info->_socket, sizeof(*(cfstream->info->_socket)))
                     && isMemoryReadable(&(cfstream->info->_socket->_context), sizeof(cfstream->info->_socket->_context))
                     && isMemoryReadable(cfstream->info->_socket->_context.release, sizeof(*(cfstream->info->_socket->_context.release)))) {
                      if (cfstream->info != NULL && cfstream->info->_socket != NULL) {
                          if ((uintptr_t)cfstream->info->_socket->_context.release == (uintptr_t)CFRelease) {
                              origin_context_release = cfstream->info->_socket->_context.release;
                              cfstream->info->_socket->_context.release = new_context_release;
                          }
                      }
                  }
              }
    

xuezhulian avatar Jul 19 '23 07:07 xuezhulian

您好我已经收到您的邮件了,待查看后马上回复!

andyliuzs avatar Jul 19 '23 07:07 andyliuzs

After writing above mentioned piece of code it gives error like "Call to undeclared function 'isMemoryReadable'; ISO C99 and later do not support implicit function declarations" "Invalid application of 'sizeof' to an incomplete type 'struct __CFStream'" "Incomplete definition of type 'struct __CFStream'" @xuezhulian

Roman-Izaz avatar Jul 20 '23 10:07 Roman-Izaz