我们有一个在内部使用AFHTTPClient
and的类AFHTTPRequestOperation
。在开发中,我们连接到一个带有无效 SSL 证书的服务器,所以我们在返回请求操作的方法中有这段代码:
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;
}];
}
这实际上与定义相同_AFNETWORKING_ALLOW_INVALID_SSL_CERTIFICATES_
,但我们需要在运行时执行此操作。这在 Xcode 上完美运行,忽略了无效证书,并且我们所有的功能测试都通过了。当我们使用命令行从命令行运行它们时,问题就来了xcodebuild
。一旦它要运行第一个测试,它就会崩溃:
Run test case <testMethodName>
Test Case '-[<TestCaseClass> <testMethodName>]' 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).
如果我们去掉那个代码,并且不要忽略 SSL 证书无效,测试就不会通过,因为连接失败,但没有任何崩溃。
这是控制台中的崩溃日志:
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:
--> __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 -[<TestCaseClass> <TestMethodName>] + 1253 (<TestCaseClass>.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<XLoaderEvent, XLoaderEventParams>*, 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:
.....
谷歌搜索那次崩溃并没有显示任何类似的东西。CFNetwork
它在内部崩溃EXC_BAD_ACCESS
,并且我在 AFNetworking 或我的代码中看不到任何可能导致此问题的内存管理问题。而且这不会发生 100% 的时间(我会说大约 80%),所以我不禁想到存在某种竞争条件。也许我们在错误的线程中使用非线程安全对象做某事?同样,在这方面还没有看到任何特别的东西。
在 Mac OSX 10.7.4 上使用 Xcode 4.3.2