xcode 15.3 IOS 17 SDK NEPacketTunnelProvider Cannot send TCP packets in release mode but can in debug mode

After I upgraded xcode to 15.3 , Then VPN NetworkExtension NEPacketTunnelProvider cannot send TCP packets in release mode, but can send TCP packets in debug mode, please help me!!!

Replies

Problems like this are almost always related to optimisation. Consider code like this:

 1 import Network
 2 
 3 func main() {
 4     let connection = NWConnection(host: "example.com", port: 80, using: .tcp)
 5     // … set up state handler and so on …
 6     connection.start(queue: .main)
 7     dispatchMain()
 8 }
 9 
10 main()

This is likely to work in your Debug build but it’s possible that it might fail in your Release build. That’s because the last reference to connection is on line 6, so the optimiser might release that reference before the call to dispatchMain(). If it does, and nothing else is holding a reference, the underlying object will be deallocated and that’ll cancel the connection.

While this is a trivial example, and unlikely to crop up in anything except a test project, I’ve seen this sort of problem show up numerous times in real apps.

One good way to track this down is to put log point in your deinitialisers for your networking classes. If those log points fire unexpectedly, you know where to start.

Share and Enjoy

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

  • Thank you very much for your answer, but I use NEPacketTunnelProvider in the NetworkExtension for packet transmission. The code is as follows: import NetworkExtension class PacketTunnelProvider: NEPacketTunnelProvider { ... packetFlow.readPacketObjects packetFlow.writePacketObjects } The same code can send data packets when compiled in release mode in xcode14, but it can only be sent in debug mode in xcode15, not in release mode, so now I am very confused.

  • The debug model Vpn can take effect, but the release mode Vpn does not take effect. I suspect that the data packets written to the device are captured. ... let ip4Set = ... ip4Set.includedRoutes = [NEIPv4Route.default()] ... func readDevicePackets(){ ... packetFlow.readPacketObjects { (packetList) in ... let sendPackList: [NEPacket] = .. ... packetFlow.writePacketObjects(sendPackList) readDevicePackets() } }

Add a Comment

Things will go more smoothly if you reply as a reply, rather than in the comments. See Quinn’s Top Ten DevForums Tips for the details, and for lots more tips on how to use DevForums effectively.

but I use NEPacketTunnelProvider in the NetworkExtension for packet transmission

Right. My answer used NWConnection as a general example of this problem, not because I thought you were using that.

Having said that, a packet tunnel provider has a top half and a bottom half. The top half uses NEPacketTunnelFlow methods to receive outbound packets from the system and to deliver inbound ones. The bottom half typically uses a networking API, like Network framework or BSD Sockets, to talk to the VPN server. Given that, the specifics upthread may be more relevant than you think.

Regardless, there are two ways to debug problems like this:

  • With the debugger — The debugger doesn’t work well for releases builds, but it does work a bit.

  • With logging — I generally recommend the system log; see Your Friend the System Log for more on that.

Oh, one more thing: Make sure you’re hitting a Debug vs Release build configuration issue rather than a code signing issue. I have advice on that front in Isolating Code Signing Problems from Build Problems.

Share and Enjoy

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