title: otest crash with -[id<NSURLAuthenticationChallengeSender> userCredential:]
body: <p>We have a class that uses <code>AFHTTPClient</code> and <code>AFHTTPRequestOperation</code>'s internally. In development, we connect to a server with an invalid SSL Certificate, so we have this code in the method that returns the request operations:</p>
  
  <pre><code>if (self.allowInvalidSSLCertificates)
      {
          [operation setAuthenticationChallengeBlock:^(NSURLConnection *connection, NSURLAuthenticationChallenge *challenge) {
              if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) {
                  [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];
              }
          }];
  
          [operation setAuthenticationAgainstProtectionSpaceBlock:^BOOL(NSURLConnection *connection, NSURLProtectionSpace *protectionSpace) {
              return YES;
          }];
      }
  </code></pre>
  
  <p>which effectively is the same as defining <code>_AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_</code>, but we needed to do this at run-time.
  This works perfectly on Xcode, ignores the invalid certificate, and all our functional tests pass. The problem comes when we run them from the command line using <code>xcodebuild</code>.  As soon as it's going to run the first test, it crashes:</p>
  
  <p><code>
  Run test case &lt;testMethodName&gt;
  Test Case '-[&lt;TestCaseClass&gt; &lt;testMethodName&gt;]' started.
  /Applications/Xcode.app/Contents/Developer/Tools/RunPlatformUnitTests.include: line 415: 47744 Bus error: 10           "${THIN_TEST_RIG}" "${OTHER_TEST_FLAGS}" "${TEST_BUNDLE_PATH}"
  /Applications/Xcode.app/Contents/Developer/Tools/RunPlatformUnitTests.include:451: error: Test rig '/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.1.sdk/Developer/usr/bin/otest' exited abnormally with code 138 (it may have crashed).
  </code></p>
  
  <p>If we get rid of that code, and don't ignore the SSL certificate being invalid, tests don't pass, because the connection fails, but nothing crashes.</p>
  
  <p>This is the crash log in Console:</p>
  
  <pre><code>Process:         otest [35957]
  Path:            /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.1.sdk/Developer/usr/bin/otest
  Identifier:      otest
  Version:         ??? (???)
  Code Type:       X86 (Native)
  Parent Process:  bash [35952]
  
  Date/Time:       2012-05-29 15:46:37.398 -0700
  OS Version:      Mac OS X 10.7.4 (11E53)
  Report Version:  9
  
  Crashed Thread:  7  com.apple.NSURLConnectionLoader
  
  Exception Type:  EXC_BAD_ACCESS (SIGBUS)
  Exception Codes: KERN_PROTECTION_FAILURE at 0x0000000000000000
  
  VM Regions Near 0:
  --&gt; __PAGEZERO             0000000000000000-0000000000001000 [    4K] ---/--- SM=NUL  /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.1.sdk/Developer/usr/bin/otest
      __TEXT                 0000000000001000-0000000000003000 [    8K] r-x/rwx SM=COW  /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator5.1.sdk/Developer/usr/bin/otest
  
  Thread 0:
  0   libsystem_kernel.dylib          0x98908c22 mach_msg_trap + 10
  1   libsystem_kernel.dylib          0x989081f6 mach_msg + 70
  2   CoreFoundation                  0x0045e10a __CFRunLoopServiceMachPort + 186
  3   CoreFoundation                  0x003c1550 __CFRunLoopRun + 1312
  4   CoreFoundation                  0x003c0d84 CFRunLoopRunSpecific + 212
  5   CoreFoundation                  0x003c0c9b CFRunLoopRunInMode + 123
  6   Foundation                      0x0004e40f -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 300
  7   Foundation                      0x000899e8 -[NSRunLoop(NSRunLoop) runUntilDate:] + 73
  8   Functional Tests                0x052f37d5 -[&lt;TestCaseClass&gt; &lt;TestMethodName&gt;] + 1253 (&lt;TestCaseClass&gt;.m:564)
  9   CoreFoundation                  0x003f04ed __invoking___ + 29
  10  CoreFoundation                  0x003f0407 -[NSInvocation invoke] + 167
  11  SenTestingKit                   0x201039c4 -[SenTestCase invokeTest] + 184
  12  SenTestingKit                   0x20103868 -[SenTestCase performTest:] + 183
  13  SenTestingKit                   0x201034a9 -[SenTest run] + 82
  14  SenTestingKit                   0x20106db2 -[SenTestSuite performTest:] + 106
  15  SenTestingKit                   0x201034a9 -[SenTest run] + 82
  16  SenTestingKit                   0x20106db2 -[SenTestSuite performTest:] + 106
  17  SenTestingKit                   0x201034a9 -[SenTest run] + 82
  18  SenTestingKit                   0x20105e97 +[SenTestProbe runTests:] + 174
  19  CoreFoundation                  0x0048bd51 +[NSObject performSelector:withObject:] + 65
  20  otest                           0x0000231c 0x1000 + 4892
  21  otest                           0x000025be 0x1000 + 5566
  22  otest                           0x00002203 0x1000 + 4611
  23  otest                           0x00001f8d 0x1000 + 3981
  24  otest                           0x00001f31 0x1000 + 3889
  
  Thread 1:
  0   libsystem_kernel.dylib          0x9890b90a kevent + 10
  1   libdispatch.dylib               0x01029372 _dispatch_mgr_invoke + 918
  2   libdispatch.dylib               0x01027be1 _dispatch_mgr_thread + 53
  
  Thread 2:
  0   libsystem_kernel.dylib          0x9890b02e __workq_kernreturn + 10
  1   libsystem_c.dylib               0x9aabcccf _pthread_wqthread + 773
  2   libsystem_c.dylib               0x9aabe6fe start_wqthread + 30
  
  Thread 3:
  0   libsystem_kernel.dylib          0x9890b02e __workq_kernreturn + 10
  1   libsystem_c.dylib               0x9aabcccf _pthread_wqthread + 773
  2   libsystem_c.dylib               0x9aabe6fe start_wqthread + 30
  
  Thread 4:
  0   libsystem_kernel.dylib          0x9890b02e __workq_kernreturn + 10
  1   libsystem_c.dylib               0x9aabcccf _pthread_wqthread + 773
  2   libsystem_c.dylib               0x9aabe6fe start_wqthread + 30
  
  Thread 5:
  0   libsystem_kernel.dylib          0x9890b02e __workq_kernreturn + 10
  1   libsystem_c.dylib               0x9aabcccf _pthread_wqthread + 773
  2   libsystem_c.dylib               0x9aabe6fe start_wqthread + 30
  
  Thread 6:
  0   libsystem_kernel.dylib          0x98908c22 mach_msg_trap + 10
  1   libsystem_kernel.dylib          0x989081f6 mach_msg + 70
  2   CoreFoundation                  0x0045e10a __CFRunLoopServiceMachPort + 186
  3   CoreFoundation                  0x003c15d5 __CFRunLoopRun + 1445
  4   CoreFoundation                  0x003c0d84 CFRunLoopRunSpecific + 212
  5   CoreFoundation                  0x003c0c9b CFRunLoopRunInMode + 123
  6   Foundation                      0x0004e40f -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 300
  7   Foundation                      0x0004e2d3 -[NSRunLoop(NSRunLoop) run] + 76
  8   Functional Tests                0x054fb7f5 +[AFURLConnectionOperation networkRequestThreadEntryPoint:] + 133 (AFURLConnectionOperation.m:146)
  9   Foundation                      0x0001a4d6 -[NSThread main] + 76
  10  Foundation                      0x0001a447 __NSThread__main__ + 1258
  11  libsystem_c.dylib               0x9aabaed9 _pthread_start + 335
  12  libsystem_c.dylib               0x9aabe6de thread_start + 34
  
  Thread 7 Crashed:: com.apple.NSURLConnectionLoader
  0   CoreFoundation                  0x0039a811 CFArrayGetCount + 17
  1   Security                        0x00dae356 SecTrustCopyExceptions + 35
  2   CFNetwork                       0x00b31764 SocketStream::securityAcceptPeerTrust_NoLock(unsigned char) + 150
  3   CFNetwork                       0x00b39fd4 SocketStream::setProperty(void const*, __CFString const*, void const*) + 2754
  4   CFNetwork                       0x00b39508 virtual thunk to SocketStream::setProperty(void const*, __CFString const*, void const*) + 43
  5   CFNetwork                       0x00b3abad ReadStreamCallbacks::_setProperty(__CFReadStream*, __CFString const*, void const*, void*) + 53
  6   CoreFoundation                  0x00429b10 CFReadStreamSetProperty + 160
  7   CFNetwork                       0x00b3ab60 HTTPReadFilter::streamSetProperty(__CFReadStream*, __CFString const*, void const*) + 266
  8   CFNetwork                       0x00bd4345 non-virtual thunk to HTTPReadFilter::streamSetProperty(__CFReadStream*, __CFString const*, void const*) + 41
  9   CFNetwork                       0x00b37308 CFNetworkReadStream::httpStreamSetProperty(__CFReadStream*, __CFString const*, void const*, void*) + 38
  10  CoreFoundation                  0x00429b10 CFReadStreamSetProperty + 160
  11  CFNetwork                       0x00b419f4 HTTPNetStreamInfo::streamSetProperty(__CFReadStream*, __CFString const*, void const*) + 404
  12  CFNetwork                       0x00b41858 non-virtual thunk to HTTPNetStreamInfo::streamSetProperty(__CFReadStream*, __CFString const*, void const*) + 41
  13  CFNetwork                       0x00b37308 CFNetworkReadStream::httpStreamSetProperty(__CFReadStream*, __CFString const*, void const*, void*) + 38
  14  CoreFoundation                  0x00429b10 CFReadStreamSetProperty + 160
  15  CFNetwork                       0x00be3e29 HTTPProtocol::useCredential(_CFURLCredential const*, _CFURLAuthChallenge*) + 125
  16  CFNetwork                       0x00bfa98f URLConnectionLoader::loaderUseCredential(_CFURLCredential const*) + 117
  17  CFNetwork                       0x00bfa47a URLConnectionLoader::LoaderConnectionEventQueue::processAllEventsAndConsumePayload(XConnectionEventInfo&lt;XLoaderEvent, XLoaderEventParams&gt;*, long) + 140
  18  CFNetwork                       0x00b2273b URLConnectionLoader::processEvents() + 107
  19  CFNetwork                       0x00bf7117 non-virtual thunk to URLConnectionInstanceData::multiplexerClientPerform() + 21
  20  CFNetwork                       0x00b20fbf MultiplexerSource::perform() + 259
  21  CoreFoundation                  0x0045e94f __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 15
  22  CoreFoundation                  0x003c1b43 __CFRunLoopDoSources0 + 243
  23  CoreFoundation                  0x003c1424 __CFRunLoopRun + 1012
  24  CoreFoundation                  0x003c0d84 CFRunLoopRunSpecific + 212
  25  CoreFoundation                  0x003c0c9b CFRunLoopRunInMode + 123
  26  Foundation                      0x00108e30 +[NSURLConnection(Loader) _resourceLoadLoop:] + 398
  27  Foundation                      0x0001a4d6 -[NSThread main] + 76
  28  Foundation                      0x0001a447 __NSThread__main__ + 1258
  29  libsystem_c.dylib               0x9aabaed9 _pthread_start + 335
  30  libsystem_c.dylib               0x9aabe6de thread_start + 34
  
  Thread 8:: com.apple.CFSocket.private
  0   libsystem_kernel.dylib          0x9890ab42 __select + 10
  1   CoreFoundation                  0x003f27cb __CFSocketManager + 939
  2   libsystem_c.dylib               0x9aabaed9 _pthread_start + 335
  3   libsystem_c.dylib               0x9aabe6de thread_start + 34
  
  Thread 7 crashed with X86 Thread State (32-bit):
    eax: 0x00000000  ebx: 0x00000002  ecx: 0x0d651da4  edx: 0x05012980
    edi: 0x0039a80d  esi: 0x00000000  ebp: 0xb038bbb8  esp: 0xb038bba0
     ss: 0x00000023  efl: 0x00010286  eip: 0x0039a811   cs: 0x0000001b
     ds: 0x00000023   es: 0x00000023   fs: 0x00000023   gs: 0x0000000f
    cr2: 0x00000000
  Logical CPU: 4
  
  Binary Images:
  .....
  </code></pre>
  
  <p>Googling for that crash doesn't show anything similar. It crashes inside <code>CFNetwork</code> with an <code>EXC_BAD_ACCESS</code>, and I can't see any memory management issue in AFNetworking or in my code that could be causing this. Also this doesn't happen the 100% of the times (I'd say around the 80%), so I can't help to think there's some kind of race condition. Perhaps we're doing something with a non thread-safe object in the wrong thread? Again, haven't seen anything in particular in this regard.</p>
  
  <p>Using Xcode 4.3.2 on Mac OSX 10.7.4</p>