CancelTunnelWithError crash

Recently we have got serveral crash report from Organizer. The crash is about cancelTunnelWithError API usage in network extension.

Last Exception Backtrace:
0   CoreFoundation                	0x1d879140c __exceptionPreprocess + 160 (NSException.m:202)
1   libobjc.A.dylib               	0x1d1a71c10 objc_exception_throw + 56 (objc-exception.mm:356)
2   Foundation                    	0x1d2b29448 -[NSXPCEncoder _checkObject:] + 288 (NSXPCCoder.m:0)
3   Foundation                    	0x1d2b29114 -[NSXPCEncoder _encodeUnkeyedObject:] + 36 (NSXPCCoder.m:396)
4   Foundation                    	0x1d2b28ddc -[NSXPCEncoder _encodeArrayOfObjects:forKey:] + 180 (NSXPCCoder.m:523)
5   Foundation                    	0x1d2b2ba24 -[NSDictionary(NSDictionary) encodeWithCoder:] + 568 (NSDictionary_Foundation.m:47)
6   Foundation                    	0x1d2b28ff8 -[NSXPCEncoder _encodeObject:] + 436 (NSXPCCoder.m:373)
7   Foundation                    	0x1d2b28ddc -[NSXPCEncoder _encodeArrayOfObjects:forKey:] + 180 (NSXPCCoder.m:523)
8   Foundation                    	0x1d2b2ba24 -[NSDictionary(NSDictionary) encodeWithCoder:] + 568 (NSDictionary_Foundation.m:47)
9   Foundation                    	0x1d2b28ff8 -[NSXPCEncoder _encodeObject:] + 436 (NSXPCCoder.m:373)
10  Foundation                    	0x1d2b28ddc -[NSXPCEncoder _encodeArrayOfObjects:forKey:] + 180 (NSXPCCoder.m:523)
11  Foundation                    	0x1d2b2ba24 -[NSDictionary(NSDictionary) encodeWithCoder:] + 568 (NSDictionary_Foundation.m:47)
12  Foundation                    	0x1d2b28ff8 -[NSXPCEncoder _encodeObject:] + 436 (NSXPCCoder.m:373)
13  Foundation                    	0x1d2b54378 _NSXPCSerializationAddInvocationWithOnlyObjectArgumentsArray + 112 (NSXPCCoder_InvocationSerialization.m:36)
14  Foundation                    	0x1d2b49c2c -[NSXPCEncoder _encodeInvocationObjectArgumentsOnly:count:typeString:selector:isReply:into:] + 208 (NSXPCCoder.m:498)
15  Foundation                    	0x1d2b472dc _sendReplyArgumentsOnly + 172 (NSXPCConnection.m:359)
16  Foundation                    	0x1d30649f8 __64-[NSXPCConnection _decodeAndInvokeMessageWithEvent:reply:flags:]_block_invoke_4 + 92 (NSXPCConnection.m:573)
17  Foundation                    	0x1d2baef90 -[_NSXPCConnectionRequestedReplies endTransactionForSequence:completionHandler:] + 192 (NSXPCConnectionHelpers.m:516)
18  Foundation                    	0x1d3064974 __64-[NSXPCConnection _decodeAndInvokeMessageWithEvent:reply:flags:]_block_invoke_3 + 148 (NSXPCConnection.m:569)
19  NetworkExtension              	0x1ee499f6c -[NEExtensionProviderContext cancelWithError:] + 196 (NEExtensionProviderContext.m:247)
20  NetworkExtension              	0x1ee49ccb8 -[NEExtensionTunnelProviderContext cancelWithError:] + 68 (NEExtensionTunnelProviderContext.m:185)
21  NetworkExtension              	0x1ee578bdc -[NEPacketTunnelProvider cancelTunnelWithError:] + 164 (NEPacketTunnelProvider.m:84)
22  *****PacketTunnelProvider        	0x100659318 *****PacketTunnelProvider.cancelTunnelWithError(_:) + 360 (*****PacketTunnelProvider.swift:162)
23  *****PacketTunnelProvider        	0x100659370 @objc *****PacketTunnelProvider.cancelTunnelWithError(_:) + 56 (<compiler-generated>:0)
24  *****                         	0x100778c70 *****PacketTunnelProviderImpl.cancelTunnelWithError(_:cancelingFunction:) + 720 (******PacketTunnelProviderImpl.swift)
25  *****                         	0x10077e434 closure #1 in *****PacketTunnelProviderImpl.******
26  MCKit                         	0x1007b9934 partial apply for closure #1 in *****PacketTunnelProviderImpl.****(****) + 32 (<compiler-generated>:0)
27  MCKit                         	0x100774e9c thunk for @escaping @callee_guaranteed @Sendable (@guaranteed Data?, @guaranteed NSURLResponse?, @guaranteed Error?) -> () + 148 (<compiler-generated>:0)
28  CFNetwork                     	0x1d9774e60 __40-[__NSURLSessionLocal taskForClassInfo:]_block_invoke + 476 (LocalSession.mm:718)
29  CFNetwork                     	0x1d9783da8 __49-[__NSCFLocalSessionTask _task_onqueue_didFinish]_block_invoke_2 + 156 (LocalSessionTask.mm:544)
30  libdispatch.dylib             	0x1df5347a8 _dispatch_call_block_and_release + 24 (init.c:1518)
31  libdispatch.dylib             	0x1df535780 _dispatch_client_callout + 16 (object.m:560)
32  libdispatch.dylib             	0x1df5106fc _dispatch_lane_serial_drain$VARIANT$armv81 + 600 (queue.c:3885)
33  libdispatch.dylib             	0x1df5111e4 _dispatch_lane_invoke$VARIANT$armv81 + 432 (queue.c:3976)
34  libdispatch.dylib             	0x1df51af14 _dispatch_workloop_worker_thread + 608 (queue.c:6507)
35  libsystem_pthread.dylib       	0x2227ddbd0 _pthread_wqthread + 284 (pthread.c:2618)
36  libsystem_pthread.dylib       	0x2227dd720 start_wqthread + 8

So we have some self defined error enums which conforms CustomNSError protocol. Not sure if there is something wrong from Error in swift to NSError in objective-c.

This issue is not existed for old iOS version before. And the same code works fine on macOS.

we have some self defined error enums which conforms CustomNSError protocol.

Your errors have to be XPC codable because the system will transport them across process boundaries. I’m not sure why that’s failing in this case, but this is pretty easy to isolate into a test. Set up an anonymous listener, per TN3113, and see if you can send your errors across it.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

Thanks for the reply.

The thing is this crash is only seen on iOS. I am thinking NSXPCConnection is not supported by developers on iOS.

Anothing thing is I am using network extension framework,not network system extension. So I guess the encoding/decoding is done in framework itself.

According to the doc,we just need to return NSError object in this function. In swift we return a enum error which conforms to customNSError. So I think it would be converted to NSError object implicitly.

Any thoughts?

The thing is this crash is only seen on iOS. I am thinking NSXPCConnection is not supported by developers on iOS.

Correct. But if this is the problem I think it is, I’ll reproduce on macOS as well.

Anothing thing is I am using network extension framework,not network system extension. So I guess the encoding/decoding is done in framework itself.

Right. But NE, regardless of what extension packaging you’re using, is heavily dependent on XPC under the covers.

According to the doc,we just need to return NSError object in this function. In swift we return a enum error which conforms to CustomNSError. So I think it would be converted to NSError object implicitly.

Right. But even with NSError it’s possible to create an object that’s not XPC codable. For example, this will fail in similar circumstances:

WaffleInfo * waffleInfo = …;
NSError * error = [NSError errorWithDomain:@"com.example.waffle-varnish" code:42 userInfo:@{
    @"waffleInfo": waffleInfo,
}];

because WaffleInfo is not XPC codable.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

CancelTunnelWithError crash
 
 
Q