NWUDPSessionState Stuck at Preparing After Cancelling Session.

Hi There,

We found an issue that caused NWUDPSession stuck at NWUDPSessionStatePreparing forever:

When the user switches from Wifi to cellular network, we will reconnect them using:

[self.session cancel];
NEPacketTunnelNetworkSettings* networkSettings = // new settings
[self updateTunnelNetworkSettings:networkSettings];
self.session = [self createUDPSessionToEndpoint:hostEndpoint fromEndpoint:nil];
[self waitForSessionToBeReadyUsingKVO];
// This line never executed while self.session.state stuck 
// at NWUDPSessionStatePreparing

Any ideas on this issue?

Any ideas on this issue?

I suspect it's due to updating the tunnel network settings on the network path transition. Is there any reason you need to do this?

Matt Eaton
DTS Engineering, CoreOS
meaton3@apple.com

Sorry, the comment doesn't allow me to write the code with the pretty format, so I just copy and past here:

One thing that is worth mentioning, we do wait for the network settings to be updated before we create a new session:

- (NSError *)updateTunnelNetworkSettings:(NEPacketTunnelNetworkSettings *)settings {
  dispatch_semaphore_t semaphore = dispatch_semaphore_create(0);
  __block NSError *updateError;
  WEAKIFY(self);
  [self setTunnelNetworkSettings:settings
               completionHandler:^(NSError *_Nullable error) {
                 STRONGIFY_OR_RETURN(self);
                 if (error) {
                   updateError = error;
                 }
                 dispatch_async(self->_tunnelProviderQueue, ^{
                   if (self->_startCompletionHandler) {
                     self->_startCompletionHandler(nil);
                   }
                   self->_startCompletionHandler = nil;
                 });
                 dispatch_semaphore_signal(semaphore);
               }];
  dispatch_time_t timeout =
      dispatch_time(DISPATCH_TIME_NOW, kUpdateNetworkSettingsTimeout * NSEC_PER_SEC);
  if (dispatch_semaphore_wait(semaphore, timeout) != 0) {
    updateError = TIMEOUT_ERROR;
  }
  return updateError;
}
Accepted Answer

Okay, what happens if you use TCP here with NWTCPConnection? Does it get stuck in .preparing also? If so, then you may have path transition issues on the new network you have transitioned to that your NEPacketTunnelNetworkSettings are not adjusting to. If TCP works fine then there may be something else going on in your code.

Matt Eaton
DTS Engineering, CoreOS
meaton3@apple.com
NWUDPSessionState Stuck at Preparing After Cancelling Session.
 
 
Q