open-telemetry / opentelemetry-swift

OpenTelemetry API for Swift
https://opentelemetry.io/docs/instrumentation/swift/
Apache License 2.0
207 stars 124 forks source link

Crash caused by automated URLSessionInstrumentation when used in combination with GoogleMaps SDK #522

Closed batuhansk closed 5 months ago

batuhansk commented 5 months ago

Issue Description:

When using the OpenTelemetry-Swift SDK in conjunction with the GoogleMaps SDK within our iOS application, we've encountered a critical issue resulting in crashes. The problem arises specifically due to the automated URLSessionInstrumentation feature of the OpenTelemetry-Swift SDK.

Problem Details:

The crash seems to be occurring during the swizzling process of URLSession's resume operations. It appears that some requests, particularly those invoked by the GoogleMaps SDK, when intercepted by the OpenTelemetry instrumentation, may carry a nil currentRequest. This leads to assertions and API misuse errors, causing the application to crash.

Error Logs:

API MISUSE: Resuming an NSURLSessionTask with nil URL.
httpCanHandleRequest(): Called for a nullptr request!
_canHandleRequest(): called with a NULL request
_canHandleRequest(): called with a NULL request
assertion failure
API MISUSE: Resuming an NSURLSessionTask with nil URL.

Steps to Reproduce:

  1. Integrate the OpenTelemetry-Swift SDK into an iOS application.
  2. Integrate the GoogleMaps SDK into the same application.
  3. Observe crashes occurring intermittently, particularly when requests are invoked by the GoogleMaps SDK, with the provided error logs.

Workaround:

A potential workaround for this issue is to implement defensive checks against requests carried by the anyTask object accessible in blocks. By ensuring that proper checks are in place to handle nil currentRequest instances, the likelihood of crashes caused by API misuse errors can be reduced.

Impact:

This issue severely impacts the stability of applications utilizing both OpenTelemetry-Swift and GoogleMaps SDKs, particularly when requests are invoked by the GoogleMaps SDK, potentially leading to crashes and user dissatisfaction. As such, resolving this problem is crucial for maintaining the reliability of affected applications.

Additional Information:

Crash report 1: CFURLRequestSetHTTPRequestBodyParts EXC_BAD_ACCESS (KERN_INVALID_ADDRESS)

Crashed: com.apple.NSURLSession-work
0  CFNetwork                      0x3aa94 CFURLRequestSetHTTPRequestBodyParts + 28
1  CFNetwork                      0x59978 CFURLRequestSetHTTPRequestBodyStream + 4460
2  CFNetwork                      0x16bd8 CFURLRequestSetURL + 12560
3  CFNetwork                      0x18c14 CFHTTPMessageCopyBody + 2332
4  libdispatch.dylib              0x637a8 _dispatch_call_block_and_release + 24
5  libdispatch.dylib              0x64780 _dispatch_client_callout + 16
6  libdispatch.dylib              0x3f6fc _dispatch_lane_serial_drain$VARIANT$armv81 + 600
7  libdispatch.dylib              0x401e4 _dispatch_lane_invoke$VARIANT$armv81 + 432
8  libdispatch.dylib              0x49f14 _dispatch_workloop_worker_thread + 608
9  libsystem_pthread.dylib        0x1bd0 _pthread_wqthread + 284
10 libsystem_pthread.dylib        0x1720 start_wqthread + 8

Crash report 2: CFURLConnectionInvalidateConnectionCache EXC_BAD_ACCESS (KERN_INVALID_ADDRESS)

Crashed: com.apple.NSURLSession-work
0  CFNetwork                      0xc0d70 CFURLConnectionInvalidateConnectionCache + 9036
1  CFNetwork                      0xe634 _CFNetworkSetHSTSStoragePath + 25460
2  CFNetwork                      0x2bcf4 CFHTTPMessageCopySerializedMessage + 8512
3  CFNetwork                      0x271a20 estimatedPropertyListSize + 134908
4  libdispatch.dylib              0x26a8 <redacted> + 32
5  libdispatch.dylib              0x4300 <redacted> + 20
6  libdispatch.dylib              0xb894 <redacted> + 748
7  libdispatch.dylib              0xc3f8 <redacted> + 432
8  libdispatch.dylib              0x17004 <redacted> + 288
9  libdispatch.dylib              0x16878 <redacted> + 404
10 libsystem_pthread.dylib        0x1964 _pthread_wqthread + 288
11 libsystem_pthread.dylib        0x1a04 start_wqthread + 8