sentry-cocoa: Crash in CFNetwork due to HTTP instrumentation

Environment

How do you use Sentry? Sentry SaaS (sentry.io)

Which SDK and version? sentry-cocoa v7.2.8

Issue

After integrating the Sentry SDK into our iOS app, we’re sometimes seeing the following crash in CFNetwork when the app is making network requests.

We suspect that this is due to Sentry’s HTTP instrumentation using KVO to observe the NSURLSessionTask’s state property: https://github.com/getsentry/sentry-cocoa/blob/1fbaba4ee5ad5bc3919c83ab39f765017662a03a/Sources/Sentry/SentryNetworkTracker.m#L112-L116

Other frameworks have had the same issue, see e.g.:

OS Version: iOS 14.6 (18F72)
Report Version: 104

Exception Type: EXC_BAD_ACCESS (SIGBUS)
Exception Codes: BUS_NOOP at 0x003f836300000000
Crashed Thread: 21

Application Specific Information:
Network: > setState: >
Attempted to dereference garbage pointer 0x3f836300000000.

Thread 21 Crashed:
0   CoreFoundation                  0x312d5562c         CFDictionaryGetValue
1   Foundation                      0x3154f5cb0         [inlined] _NSSetLongLongValueAndNotify
2   Foundation                      0x3154f5cb0         _NSSetLongLongValueAndNotify
3   CFNetwork                       0x313d20358         _CFNetworkHTTPConnectionCacheSetLimit
4   Foundation                      0x31551ffa4         __NSBLOCKOPERATION_IS_CALLING_OUT_TO_A_BLOCK__
5   Foundation                      0x31540d874         -[NSBlockOperation main]
6   Foundation                      0x3155223cc         __NSOPERATION_IS_INVOKING_MAIN__
7   Foundation                      0x31540d4fc         -[NSOperation start]
8   Foundation                      0x315522e70         __NSOPERATIONQUEUE_IS_STARTING_AN_OPERATION__
9   Foundation                      0x3155228fc         __NSOQSchedule_f
10  libdispatch.dylib               0x312721480         _dispatch_block_async_invoke2
11  libdispatch.dylib               0x312712818         _dispatch_client_callout
12  libdispatch.dylib               0x312715cf0         _dispatch_continuation_pop
13  libdispatch.dylib               0x312715380         _dispatch_async_redirect_invoke
14  libdispatch.dylib               0x312723fdc         _dispatch_root_queue_drain
15  libdispatch.dylib               0x3127247d4         _dispatch_worker_thread2
16  libsystem_pthread.dylib         0x3aa601764         _pthread_wqthread

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 3
  • Comments: 18 (11 by maintainers)

Commits related to this issue

Most upvoted comments

@triplef and @JohannesNevels, I found an issue with removing the observer for KVO and fixing it with https://github.com/getsentry/sentry-cocoa/pull/1354. We plan to release this today. As removing KVO would be a lot of effort, this fix is a best-effort shot at this bug. I’m not very confident that this will solve the issue, but worth a try. At least it improves the current code a bit. Are you willing to try our new release with your projects and let us know if it solved the issues, please?